使用 vue-multiselect 添加更多动态下拉列表

add more dynamic drop-down with vue-multiselect

我正在尝试使用 vue-multi 构建添加更多选项select

我面临的以下问题是:

  1. 无法 select states
  2. 的多个标签
  3. 无法删除 select 为 states
  4. 编辑的标签
  5. 单击加号按钮会返回旧值
  6. 动态 v-model="" 工作不正常

这里有问题giffhttps://imgur.com/CS31b6w

这里是代码笔 link 以提高可见性:https://codepen.io/eabangalore/pen/WNjRXym?editors=1010

这是我尝试过的:

var app = new Vue({
  el: '#app',
  components: { Multiselect: window.VueMultiselect.default },
  data () {
    return {
      addMoreOptionsList:[],
      dynamicAddedOptionsHashMap:{},
      value: [],
      options: [
                 {
                   "country": "America",
                   "states":['CA','MA'],
                   id:'111',
                 },
                 {
                   "country": "India",
                   "states":['KA','MH'],
                   id:'22222'
                 },
        
                  {
                   "country": "West Indies",
                   "states":['West Indies1','West Indies2'],
                    id:'3333'
                 },
        ]
    }
  },
  methods:{
        addMoreItems(){
          this.addMoreOptionsList.push('1 more added');
        },
        removeAll(){
          this.addMoreOptionsList = [];
        },
        countrySelected(options){
           //console.log('options',options);
          if(!Object.hasOwnProperty.call(this.dynamicAddedOptionsHashMap,options.id))
                    this.dynamicAddedOptionsHashMap[options.id] = {options:[],values:[]};
                    
         
         
           this.dynamicAddedOptionsHashMap[options.id]['options'] = options.states;
        }
    }
})
body { font-family: 'Arial' } 

.dropdown-item{
  display:flex;justify-content:space-between;margin-left:20px;
  margin-top:20px;
}
<!DOCTYPE HTML>
<html>
<head>
  <title>Timeline</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-multiselect@2.1.0"></script>
  <link rel="stylesheet" href="https://unpkg.com/vue-multiselect@2.1.0/dist/vue-multiselect.min.css">
  <script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>

<body>
<div id="app">

  <div style="display: flex; margin-bottom: 20px; text-align: end; justify-content: flex-end;"><button style="margin-right:20px;" @click="removeAll()">X</button><button @click="addMoreItems()">+</button></div>
  
<div class="dropdown-item" v-if="addMoreOptionsList.length" v-for="(item,index) in addMoreOptionsList">
  <multiselect
    v-model="value[index]"
    placeholder="Country?"
    label="country"
    :options="options"
    :multiple="false"
    :taggable="false"
    @select="countrySelected($event)"
  ></multiselect>
 
  
   <multiselect style="margin-left:20px;"
    v-if="value[index] && dynamicAddedOptionsHashMap[value[index].id]"
    v-model="dynamicAddedOptionsHashMap[value[index].id].values"
placeholder="States?"    
   :options="dynamicAddedOptionsHashMap[value[index].id].options"
    :multiple="true"
    :taggable="false"
    @select="countrySelected($event)"
  ></multiselect>
</div>

</div>
</body>

问题 1:您可以将所选状态保存在单独的变量中,就像我将其保存在 selectedStates.

中一样

问题 2-3:您需要初始化您的变量,如下面的演示所示。

问题 4:动态值适用于 selectedStates[index]

var app = new Vue({
  el: "#app",
  components: {
    Multiselect: window.VueMultiselect.default
  },
  data() {
    return {
      addMoreOptionsList: [],
      dynamicAddedOptionsHashMap: {},
      value: [],
      selectedStates: [],
      options: [{
          country: "America",
          states: ["CA", "MA"],
          id: "111"
        },
        {
          country: "India",
          states: ["KA", "MH"],
          id: "22222"
        },

        {
          country: "West Indies",
          states: ["West Indies1", "West Indies2"],
          id: "3333"
        }
      ]
    };
  },
  methods: {
    addMoreItems() {
      this.addMoreOptionsList.push("1 more added");
      this.selectedStates.push({
        id: null,
        values: []
      })
    },
    removeAll() {
      this.addMoreOptionsList = [];
      this.dynamicAddedOptionsHashMap = {};
      this.value = [];
      this.selectedStates = [];
    },

    getOptions(id) {
      const option = this.options.find((item) => item.id == id);
      console.log(option)
      if (option) return option.states;
      return []
    },
    selectTheState(index) {
      console.log(this.selectedStates);
      this.selectedStates[index].id = this.value[index].id

    }
  }
});
body {
  font-family: 'Arial'
}

.dropdown-item {
  display: flex;
  justify-content: space-between;
  margin-left: 20px;
  margin-top: 20px;
}
<!DOCTYPE HTML>
<html>

<head>
  <title>Timeline</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-multiselect@2.1.0"></script>
  <link rel="stylesheet" href="https://unpkg.com/vue-multiselect@2.1.0/dist/vue-multiselect.min.css">
  <script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>

<body>
  <div id="app">

    <div style="display: flex; margin-bottom: 20px; text-align: end; justify-content: flex-end;"><button style="margin-right:20px;" @click="removeAll()">X</button><button @click="addMoreItems()">+</button></div>

    <div class="dropdown-item" v-for="(item,index) in addMoreOptionsList">
      <multiselect v-model="value[index]" placeholder="Country?" label="country" :options="options" :multiple="false" :taggable="false"></multiselect>


      <multiselect style="margin-left:20px;" v-if="value[index]" v-model="selectedStates[index].values" placeholder="States?" :options="getOptions(value[index].id)" :multiple="true" :taggable="false" @select="selectTheState(index)"></multiselect>
    </div>

  </div>
</body>