【问题标题】:How to have dynamic multiple dropdown with vue-multiselect如何使用 vue-multiselect 进行动态多个下拉菜单
【发布时间】:2021-07-10 04:09:58
【问题描述】:

您好,我不知道通过更改特定组件的数据并自动加载来实现特定组件的 vue 是否可能实现

以下是我的期望(在 jquery 中试过)

var data = {country:{type:'dropdown',values:['india','usa']},money:{type:'input',placeholder:'enter amount'},india:['Bengaluru'],usa:['Silicon Valley']}


function getDropTemplate(dropDownList){
    var dropDownStr = '';
    for(var i = 0; i < dropDownList.length; i++){
       dropDownStr += `<option value="${dropDownList[i]}">${dropDownList[i]}</option>`
    }
   return `<select class="mainCountry">${dropDownStr}</select>`;
}

function getInputTemplate(inputObj){
   return `<input type="text" placeholder="${inputObj.placeholder}"/>`
}


$(function(){
    
   $('#dropdown').on('change',function(){
      var value = $(this).val(), template = '';
      if(data[value].type == 'dropdown'){
           template += getDropTemplate(data[value].values)
      }else{
          template += getInputTemplate(data[value])
      }

      $('#selectedResults').html(template);
   });

   $(document).on('change','.mainCountry',function(){
      var result = data[$(this).val()]
      $('#subResults').html(getDropTemplate(result));
   });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>



<select id="dropdown">
   <option value="">--select--</option>
   <option value="money">Money</option>
   <option value="country">Country</option>
</select>

<div id="selectedResults">

</div>

<div id="subResults">

</div>

从上面的 sn-p 中,您可以通过选择 country -&gt; india -&gt; Bengalurucountry -&gt; usa -&gt; Silicon Valley 观察到这一点

我想用vue-multiselect复制同样的东西

以下是我在vue中尝试过的

var app = new Vue({
  el: '#app',
  components: { Multiselect: window.VueMultiselect.default },
  data () {
    return {
      value: [],
       //data:{country:{type:'dropdown',values:['india','usa']},money:{type:'input',placeholder:'enter amount'},india:['Bengaluru'],usa:['Silicon Valley']}

       options:[{name:'money'},{name:'country'}]
    }
  }
})
  <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>
  
  


<div id="app">

     <multiselect
    v-model="value"
     track-by="name"
    :options="options"
    label="name"
     :multiple="false"
    :taggable="false"
  ></multiselect>
  

</div>

【问题讨论】:

    标签: javascript vue.js vuejs3 vue-multiselect


    【解决方案1】:

    您可以基于category.name 有条件地渲染inputmultiselects

    例如,当category.nameMoney 时,显示&lt;input&gt;

    <template v-if="category && category.name === 'Money'">
      <input type="text" v-model="moneyAmount" placeholder="Enter amount">
    </template>
    

    否则,当它是Country时,显示两个multiselects(一个用于选择国家,另一个用于区域):

    <template v-else-if="category && category.name === 'Country'">
      <multiselect
                   placeholder="Select a country"
                   v-model="country"
                   track-by="name"
                   :options="countryOptions"
                   label="name"
                   :multiple="false"
                   :taggable="false">
      </multiselect>
    
      <multiselect v-if="country && country.regions"
                   placeholder="Select a region"
                   v-model="region"
                   :options="country.regions"
                   :multiple="false"
                   :taggable="false">
      </multiselect>
    </template>
    

    国家multiselect 填充有countryOptions[](如下所示),每个都有regions[],用于填充区域multiselect。这可确保显示的区域仅适用于所选国家/地区。

    new Vue({
      data() {
        return {
          category: null,
          country: null,
          region: null,
          moneyAmount: null,
          categoryOptions: [{ name: 'Money' }, { name: 'Country' }],
          countryOptions: [
            {
              name: 'USA',
              regions: ['Silicon Valley', 'Midwest'],
            },
            {
              name: 'India',
              regions: ['Bengaluru'],
            }
          ],
        }
      },
    })
    

    demo

    【讨论】:

      【解决方案2】:

      您是否尝试根据您从下拉列表中选择的选项有条件地呈现组件?

      如果是这样,为什么不使用 v-if - 请参阅文档 https://vuejs.org/v2/guide/conditional.html 中如何有条件地渲染组件

      <template v-if="country.value === 'usa'">
          // render input for USA
      </template>
      <template v-else-if="country.value === 'india'">
          // render input for India
      </template>
      

      【讨论】:

      • 如果您复制 jquery 工作示例会更好
      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多