el-table通过数据回显表格选中

     
        
       

首先要在表格上绑定 refrow-key ,在多选框绑定 :reserve-selection="true"

data() {
    return { 
      tableList:[],
      // 存放选中的数据
      selectedList: []
    }
  },
methods: {
// rows  将存放选中的数据( selectedList )传给setTabelSelected方法
 setTabelSelected(rows) {
      if (rows.length > 0) {
        this.$nextTick(() => {
          for (let i = 0; i < this.tableList.length; i++) {
            let item = this.tableList[i]
            for (let j = 0; j < rows.length; j++) {
              let row = rows[j]
              if (row.id == item.id) {
                this.$refs.table.toggleRowSelection(item, true)
                break
              } else {
                this.$refs.table.toggleRowSelection(item, false)
              }
            }
          }
        })
      } else {
        this.$nextTick(() => {
          this.$refs.table.clearSelection()
        })
      }
    },
  }
}

将存放选中的数据传给 setTabelSelected 方法

你可能感兴趣的:(vue.js,javascript,前端)