【问题标题】:How to correctly remove a vue-select multiselect item如何正确删除 vue-select 多选项目
【发布时间】:2017-11-26 00:43:21
【问题描述】:

我有一个 vue-select 多选 (http://sagalbot.github.io/vue-select/) 定义为

<v-Select label="label" multiple :on-change="updateCities" :options="cities"></v-Select>

方法updateCities定义为

'updateCities': function (menuItem) {
            var name = ''
            // iterate thru the elements in the multiselect
            menuItem.forEach(function (elem, i) {
                name = elem.label
                return name
            })
            // push the selected element to an array in the data model
            this.state.city.push(name)
        }

数据模型定义为

'data' () {
        return {
            'state': {
                'city': []
            }
        }
    }

当我单击多选并选择一个城市时,菜单和数据模型数组city 都会正确更新,但是当我单击菜单项右上角的 X 以从菜单中删除元素时来自数据模型数组city。如下所示

该元素已从菜单中正确删除,但未从数据模型数组 city 中删除。

我该如何解决这个问题?

【问题讨论】:

  • 你能用小提琴/codepen 来演示这个问题吗?
  • 你应该在推送之前清除this.state.city 吗?我说不出来。请注意,forEach 中的 return 是没有意义的。

标签: javascript vue.js vuejs2


【解决方案1】:

这是通过将 this.state.city 的值分配给菜单项来解决的:

'updateAdvertisers': function (menuItem) {
            this.state.city= menuItem.map(elem => elem.label)
        }

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2021-02-17
    • 2013-10-10
    • 2015-12-08
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多