【问题标题】:Vue select options based on conditionVue根据条件选择选项
【发布时间】:2018-10-26 02:43:12
【问题描述】:

我有以下下拉模板:

<select v-model="selectedClient" class="stat-select text-u-c">
  <option disabled value="">Please select a Client</option>
  <option>{{}}</option>
</select>

...我有一个按钮click-handler,我想根据某些条件填充&lt;option&gt;s:

if (Department == 'IT') {
  // select option values should be A,B,C
} else (Department == 'Finance') {
  // select option values should be X,Y,Z
}

我怎样才能做到这一点?

【问题讨论】:

    标签: typescript vue.js vuejs2


    【解决方案1】:

    你可以使用 Vue 的 list rendering syntaxv-for

    <ELEMENT v-for="VARIABLE in ARRAY" :key="ITERATOR_ID">
    

    在你使用&lt;option&gt;s 的情况下,你会得到这样的结果:

    <option v-for="item in options" :key="item.id">{{item.label}}</option>
    

    ...其中options 是一个数据属性,包含这样的数组:

    [
      { id: 1, label: 'A' },
      { id: 2, label: 'B' },
      { id: 3, label: 'C' },
    ]
    

    如果您想要基于Department 的不同的&lt;option&gt;s 集合,您可以相应地将this.options 设置为不同的数组,数据绑定将自动更新&lt;option&gt;s:

    methods: {
      getOptions() {
        const dept = this.Department;
        if (dept === 'IT') {
          this.options = [
            { id: 1, label: 'A' },
            { id: 2, label: 'B' },
            { id: 3, label: 'C' },
          ];
        } else if (dept === 'Finance') {
          this.options = [
            { id: 4, label: 'X' },
            { id: 5, label: 'Y' },
            { id: 6, label: 'Z' },
          ];
        }
      }
    }
    

    new Vue({
      el: '#app',
      data: () => ({
        options: null,
        Department: null,
        selectedClient: null,
      }),
      methods: {
        getOptions() {
          this.selectedClient = null;
          if (this.Department === 'IT') {
            this.options = [
              { id: 1, label: 'A' },
              { id: 2, label: 'B' },
              { id: 3, label: 'C' },
            ];
          } else if (this.Department === 'Finance') {
            this.options = [
              { id: 4, label: 'X' },
              { id: 5, label: 'Y' },
              { id: 6, label: 'Z' },
            ];
          }
        }
      },
    })
    <script src="https://unpkg.com/vue@2.5.17"></script>
    
    <div id="app">
      <div>
        <span>Department:</span>
        <input id="dept" type="radio" v-model="Department" value="IT">
        <label for="dept">IT</label>
        <input id="fin" type="radio" v-model="Department" value="Finance">
        <label for="fin">Finance</label>
        <button @click="getOptions">Get options</button>
      </div>
    
      <select v-model="selectedClient"  class="stat-select text-u-c">
        <option disabled value="">Please select a Client</option>
        <option v-for="item in options" :key="item.id">{{item.label}}</option>
      </select>
      
      {{selectedClient}}
    </div>

    【讨论】:

    • @kevin 没问题 :)
    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 2019-05-18
    • 2020-10-09
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多