前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue 2.0关于多选下拉框数据渲染的问题

Vue 2.0关于多选下拉框数据渲染的问题

作者头像
王小婷
发布2023-10-01 08:36:00
2710
发布2023-10-01 08:36:00
举报
文章被收录于专栏:编程微刊

json数据格式

代码语言:javascript
复制
"workFences":[{"staffId":0,"fenceId":"978f07559b2c434b90c58486c4238645","fenceName":"\u5B89\u68C0\u5BA4"},{"staffId":0,"fenceId":"8b360d6486fb41928f246db27dd8f107","fenceName":"\u623F\u95F4701"}]

要将数据渲染到代码中,可以按照以下步骤进行修改:

1:在组件的data中添加一个名为workFences的数组属性,并将其初始值设为你提供的数据:

代码语言:javascript
复制
data() {
  return {
    // ...
    workFences: [
      { staffId: 0, fenceId: "978f07559b2c434b90c58486c4238645", fenceName: "安检室" },
      { staffId: 0, fenceId: "8b360d6486fb41928f246db27dd8f107", fenceName: "房间701" }
    ],
  };
},

2:在模板中的options-list中,使用v-for指令遍历workFences数组,并将每个选项的fenceName作为显示的文本,例如:

代码语言:javascript
复制
<ul v-if="showDropdown" class="options-list">
  <li v-for="fence in workFences" :key="fence.fenceId" @click="toggleOption(fence.fenceId)">
    <input type="checkbox" :value="fence.fenceId" :checked="isSelected(fence.fenceId)">
    <label>{{ fence.fenceName }}</label>
  </li>
</ul>

3:在submitSelection方法中,将选中的选项的fenceName添加到selectedOptions数组中,例如:

代码语言:javascript
复制
submitSelection() {
  this.selectedOptions = this.workFences
    .filter(fence => this.selectedOptions.includes(fence.fenceId))
    .map(fence => fence.fenceName);
  // 重置选项并隐藏确认按钮
  this.selectedOptions = [];
  this.showConfirmButton = false;
},

这样,数据就会被渲染到下拉框的选项中,并且在用户点击确认按钮后,选中的选项的名称会被添加到selectedOptions数组中。

demo

代码语言:javascript
复制
<template>
  <div class="dropdown">
    <div class="selected-option" @click="toggleDropdown">
      <div class="selected-values">
        <span
          v-for="(option, index) in selectedOptions"
          :key="index"
          class="selected-value"
        >
          {{ option }}
          <span class="delete" @click="removeOption(option)"> &times; </span>
        </span>
      </div>
      <input
        type="text"
        v-model="searchText"
        placeholder="请选择选项"
        @input="filterOptions"
      />
      <span class="arrow"></span>
    </div>

    <button class="confirm-button" @click="submitSelection">确认</button>
    <!-- <ul v-if="showDropdown" class="options-list">
      <li
        v-for="option in filteredOptions"
        :key="option"
        @click="toggleOption(option)"
      >
        <input type="checkbox" :value="option" :checked="isSelected(option)" />
        <label>{{ option }}</label>
      </li>
    </ul> -->

    <ul v-if="showDropdown" class="options-list">
  <li v-for="fence in workFences" :key="fence.fenceId" @click="toggleOption(fence.fenceName)">
    <input type="checkbox" :value="fence.fenceId" :checked="isSelected(fence.fenceId)">
    <label>{{ fence.fenceName }}</label>
  </li>
</ul>

  </div>
</template>

<script>
export default {
  data() {
    return {
      options: ["选项1", "选项2", "选项3", "选项4"], // 下拉框的选项数据
      selectedOptions: [], // 当前选中的选项
      showDropdown: false, // 控制悬浮框的显示状态
      searchText: "", // 过滤选项的搜索文本
      // ...
      showConfirmButton: false,
       // ...
    workFences: [
      { staffId: 0, fenceId: "978f07559b2c434b90c58486c4238645", fenceName: "安检室" },
      { staffId: 0, fenceId: "8b360d6486fb41928f246db27dd8f107", fenceName: "房间701" }
    ],

    };
  },
  computed: {
    filteredOptions() {
      return this.options.filter((option) =>
        option.toLowerCase().includes(this.searchText.toLowerCase())
      );
    },
  },
  methods: {
    toggleDropdown() {
      this.showDropdown = !this.showDropdown;

      this.showConfirmButton = false;
    },
    toggleOption(option) {
      debugger
      this.showConfirmButton = true;
      if (this.isSelected(option)) {
        this.removeOption(option);
      } else {
        this.selectedOptions.push(option);
      }
    },
    removeOption(option) {
      const index = this.selectedOptions.indexOf(option);
      if (index !== -1) {
        this.selectedOptions.splice(index, 1);
      }
    },
    // submitSelection() {
    //   // 在这里处理提交选项的逻辑
    //   console.log("已选择的选项:", this.selectedOptions);
    //   // 重置选项并隐藏确认按钮
    //   this.selectedOptions = [];
    //   this.showConfirmButton = false;
    // },

    submitSelection() {
  this.selectedOptions = this.workFences
    .filter(fence => this.selectedOptions.includes(fence.fenceId))
    .map(fence => fence.fenceName);
  // 重置选项并隐藏确认按钮
  this.selectedOptions = [];
  this.showConfirmButton = false;
},

    isSelected(option) {
      return this.selectedOptions.includes(option);
    },
    filterOptions() {
      this.showDropdown = true;
    },
  },
};
</script>

<style>
.confirm-button {
  display: block;
  margin-top: 8px;
  padding: 8px 16px;
  background-color: #4caf50;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  position: absolute;
  right: -31%;
  top: -21%;
}
.dropdown {
  position: relative;
  display: inline-block;
}

.selected-option {
  display: flex;
  align-items: center;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
}

.selected-values {
  display: flex;
  flex-wrap: wrap;
}

.selected-value {
  display: flex;
  align-items: center;
  background-color: #f2f2f2;
  padding: 4px 8px;
  border-radius: 16px;
  margin-right: 4px;
  margin-bottom: 4px;
}

.selected-value .delete {
  margin-left: 4px;
  cursor: pointer;
}

.selected-option input {
  flex: 1;
  border: none;
  background-color: transparent;
  outline: none;
  cursor: pointer;
}

.arrow {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 5px 5px 0 5px;
  border-color: #999 transparent transparent transparent;
  margin-left: 4px;
}

.options-list {
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 4px;
  list-style: none;
  padding: 0;
  margin: 0;
}

.options-list li {
  display: flex;
  align-items: center;
  padding: 8px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.options-list li:hover {
  background-color: #f2f2f2;
}

.options-list li input[type="checkbox"] {
  margin-right: 8px;
  cursor: pointer;
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档