【问题标题】:VSelect should allow displaying selected items in order per :itemsVSelect 应该允许按 :items 的顺序显示选定的项目
【发布时间】:2020-03-19 03:08:09
【问题描述】:

VSelect 保留用户选择值的顺序(根据 v-model),但我们需要它们始终按照 :items 的顺序排列

<div id="app">
  <v-app id="inspire">
    <v-card>
      <v-container fluid>
        <v-row
          align="center"
        >
          <v-col cols="12" sm="6">
            <v-select
              v-model="value"
              :items="items"
              chips
              label="Chips"
              multiple
              hide-selected
              menu-props="auto"
            ></v-select>
          </v-col>
        </v-row>
      </v-container>
    </v-card>
  </v-app>
</div>

我需要value 应该保持items 的顺序

如果我们以任意顺序选择threeonefour,则值必须为['one','three','four']

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    items: ['one', 'two', 'three', 'four'],
    value: [],
  }),
})

查看当前行为here。我想要旧的行为,在this 功能实现之前。

【问题讨论】:

    标签: vue.js vuetify.js v-select


    【解决方案1】:

    您可以通过过滤原始项目数组来重新排序列表:

    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data: () => ({
        items: ['foo', 'bar', 'fizz', 'buzz'],
        value : []
      }),
      computed : {
        _value : {
          get () {
            return this.value
          },
          set (val) {
            this.value = this.items.filter(x=>val.includes(x))
          }
        }
      }
    })
    

    并将 v-model 更新为计算出的_value

    <v-select v-model="_value"/>
    
    

    https://codepen.io/ellisdod/pen/ZEGjWPW

    或使用您的模板解决方案:

    <v-select
     v-model="value"
     :items="items"  
     @change="value=items.filter(x=>value.includes(x))"
     attach
     chips
     label="Chips"
     multiple
     ></v-select>
    
    

    【讨论】:

    • 不,我不想要这个,我有一个更好的solution 像这样。但我希望value 必须坚持items 的顺序。这是this 功能实现之前的实际行为。
    • 我现在明白了!更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2016-09-30
    • 2016-08-09
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多