vue中elementUI 表格多选框参数设置方法toggleSelection无效解决方法

4,791 阅读1分钟

官方文档写法(不生效)

toggleSelection(rows) {
    if (rows) {
      rows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
    } else {
      this.$refs.multipleTable.clearSelection();
    }
}

解决方法

toggleSelection(rows) {
  this.$nextTick(() => {
    if (rows) {
      rows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
    } else {
      this.$refs.multipleTable.clearSelection();
    }
  })
}

原因:vue中的数据是异步更新的到dom的,也就是说我们在改变之后数据后,还没有立即更新,使用this.$nextTick 这个方法 在下次 DOM 更新后再进行数据的处理。