【发布时间】:2020-10-30 22:47:56
【问题描述】:
在我的应用程序中,我在同一页面上有多个表格(也是多个页面)。每个表格的每一行都有“全选”选项和复选框。代码重用真的很有帮助,但我无法让它发挥作用。目前我有以下方法,但我总是在渲染中得到错误:“TypeError:无法读取未定义的属性'包含'”。现在这是代码在一个组件内,但应该可供另一个组件使用。如何正确地将其添加到独立组件中,然后在其他组件中使用?
在挂载方法中有一个字段被选中:{}。
Vue HTML 模板:
<input type="checkbox" v-bind:checked="isSelected(sts.id, 'country')"
@click="toggleSelected(sts.id, 'country')">
Vue 方法:
isSelected(id, group) {
return this.selected[group].includes(id);
},
toggleAll(event, group, items) {
let state = $(event.target).prop("checked");
for (let st of items) {
if (state === true) {
this.addSelected(st, group);
} else {
this.removeSelected(st, group);
}
}
},
addSelected(id, group) {
if (!this.isSelected(id, group)) {
this.selected[group].push(id);
}
},
removeSelected(id, group) {
this.selected[group] = this.selected[group].filter(item => item !== id);
},
toggleSelected(id, group) {
if (this.isSelected(id, group)) {
this.removeSelected(id, group);
} else {
this.addSelected(id, group);
}
},
【问题讨论】:
标签: javascript vue.js