简体   繁体   English

Vuetify动态创建v-select

[英]Vuetify dynamically create v-select

I am trying to create v-selects dynamically. 我正在尝试动态创建v选择。 As the user selects an option from the first v-select, another one should appear. 当用户从第一个v-选择中选择一个选项时,另一个应该出现。 Since v-selects are not html components I am having a lot of trouble appending new ones to the DOM. 由于v-selects不是html组件,因此在将新组件添加到DOM时遇到很多麻烦。 Here is the function that creates the new v-select. 这是创建新v选择的功能。

makeNewSelect () {
  let newVSelect = document.createElement('div')
  let html = ` <div role="combobox" class="v-input ma-2 v-text-field v-text-field--enclosed v-text-field--outline v-select theme--light">
                <div class="v-input__control"><div class="v-input__slot">
                  <div class="v-select__slot"><label aria-hidden="true" class="v-label theme--light" style="left: 0px; right: auto; position: absolute;">Select</label>
                    <div class="v-select__selections"><input aria-label="Select" readonly="readonly" type="text" autocomplete="on" aria-readonly="false"></div>
                      <div class="v-input__append-inner">
                        <div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon material-icons theme--light">arrow_drop_down</i></div>
                      </div>
                    </div>
                    <div class="v-menu"></div>
                  </div>
                  <div class="v-text-field__details">
                    <div class="v-messages theme--light">
                      <div class="v-messages__wrapper"></div>
                    </div>
                  </div>
                </div>
              </div>`
  newVSelect.innerHTML = html
  document.getElementById('selectLayout').appendChild(newVSelect)
},

I got this from inspecting the element of a v-select that already exists on the page. 我是通过检查页面上已经存在的v-select元素获得的。 However, when you click to open the select, the classes change which I don't know how to do (or if it's possible) with the v-select that I created. 但是,当您单击以打开选择项时,这些类会更改,而我不知道如何(或者如果可能)对我创建的v-select项进行更改。 Also, I need each v-select's v-model to be distinct so they don't all change when one does. 另外,我需要每个v-select的v-model都不同,这样它们就不会全部更改。 Is any of this possible? 有可能吗? Please help. 请帮忙。

EDIT: The data that will be used for the selects will be the same data for each dropdown. 编辑:将用于选择的数据将是每个下拉列表相同的数据。 The data is retrieved on the beforeMount() and is stored in an array. 数据在beforeMount()上检索并存储在数组中。

This is a simple way to do what you describe. 这是做您描述的简单方法。 I have an array for selected values. 我有一个用于选择值的数组。 I make a select for each of the selected values and another for the next value to be selected ( v-for="index in selections.length + 1" ). 我为每个选择的值进行选择,并为要选择的下一个值进行另一个选择( v-for="index in selections.length + 1" )。 When the new value is selected, that increases the length of the array, which puts up a new select. 选择新值时,这将增加数组的长度,从而提出新的选择。

I'm not using Vuetify v-select, but it doesn't matter what the widget is. 我没有使用Vuetify v-select,但是小部件是什么都没有关系。 v-for will make the correct number of them. v-for可以正确输入数量。

 new Vue({ el: '#app', data: { options: ['one', 'two', 'three'], selections: [] } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <select v-for="index in selections.length + 1" v-model="selections[index - 1]"> <option disabled :value="undefined">please select</option> <option v-for="opt in options">{{opt}}</option> </select> <div v-for="chosen in selections">{{chosen}}</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM