复选框数据列表回显问题(el-table数据回显翻页问题)

        <el-table
          v-show="contactList.length !== 0"
          ref="multipleTable"
          :data="contactList"
          :row-key="getRowKeys"
          tooltip-effect="dark"
          height="336px"
          class="general-table"
          @selection-change="onContactSelectChange"
        >
          <el-table-column type="selection" :reserve-selection="true" />
          <el-table-column label="姓名" prop="name" width="80" />
          <el-table-column prop="email" label="邮箱" show-overflow-tooltip align="center" />
          <el-table-column prop="mobile" label="电话" align="center" />
          <el-table-column label="微信状态" align="center">
            <template slot-scope="scope">
              <span :class="getWechatStatus(scope.row)">
                {{ scope.row.state }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="desc" label="备注" align="center" show-overflow-tooltip />
        </el-table>
    getRowKeys(row) {
      return row.id
    },

table属性有个row-key是作为切换页面还会存在的作用

    echoTable(echoTable) {
      debugger
      this.echoDataList = []
      echoTable.map(item => {
        this.contactList.map(it => {
          if (item.key === it.id) {
            this.$refs.multipleTable.toggleRowSelection(it, true)
          }
        })
        const obj = {
          id: item.key,
          name: item.label,
          state: item.state
        }
        this.echoDataList.push(obj)
      })
    },

上面这个代码块是数据回显处理的业务逻辑

    onContactSelectChange(val) {
      this.newSelection = val
      setTimeout(() => {
        this.echoSelectChange()
      }, 20)
    },
    echoSelectChange() {
      if (this.echoDataList.length <= 0) {
        this.echoDataList = this.newSelection
        return
      }
      const selectAllIds = this.echoDataList.map(item => item.id)
      const selectIds = []
      this.newSelection.map(item => {
        selectIds.push(item.id)
        if (selectAllIds.indexOf(item.id) < 0) {
          this.echoDataList.push(item)
        }
      })
      const noSelectId = this.contactList.map(item => {
        if (selectIds.indexOf(item.id) < 0) {
          return item.id
        }
      })
      noSelectId.forEach(id => {
        if (selectAllIds.indexOf(id) >= 0) {
          for (let i = 0; i < this.echoDataList.length; i++) {
            if (this.echoDataList[i].id === id) {
              // 如果总选择中有未被选中的,那么就删除这条
              this.echoDataList.splice(i, 1)
              break
            }
          }
        }
      })
    },
    selectRow() {
      if (this.echoDataList.length <= 0) return
      const selectAllIds = this.echoDataList.map(item => item.id)
      this.$refs.multipleTable.clearSelection()
      for (let i = 0; i < this.contactList.length; i++) {
        if (selectAllIds.indexOf(this.contactList[i].id) >= 0) {
          // 设置选中,记住table组件需要使用ref="table"
          this.$refs.multipleTable.toggleRowSelection(this.contactList[i], true)
        }
      }
    },

上面的代码是选择复选框的时候处理的业务逻辑

      getContactAPI((status, message, dataList, total) => {
        if (status) {
          this.contactList = dataList
          this.total = total
          // 主要做回显处理 每次便利请求要查询时候存在回显的数据
          this.$nextTick(() => {
            this.selectRow()
          })
        }
      }, this.pageIndex, this.pageSize, selectedGroupId, this.searchText)
    },

上面是接口请求数据防止覆盖处理的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tableElement UI 中的一个表格组件,而复选框回显是指在 el-table 中使用复选框进行多选操作后,再次打开表格时,已选中的项能够显示为选中状态。 要实现 el-table 复选框回显,可以按照以下步骤进行操作: 1. 在 el-table 的列定义中,添加一个带有 type 属性为 "selection" 的列,用于显示复选框。 2. 在 el-table数据源中,为每一条数据添加一个属性,用于表示该项是否被选中。 3. 在 el-table 的模板中,使用 v-model 指令绑定每一行数据的选中状态。 4. 在 el-table 的 mounted 钩子函数中,根据数据源中的选中状态,将已选中的项设置为选中状态。 下面是一个示例代码: ```html <template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, selected: true }, { name: '李四', age: 25, selected: false }, { name: '王五', age: 30, selected: true }, ], }; }, mounted() { this.tableData.forEach((item) => { if (item.selected) { this.$refs.multipleTable.toggleRowSelection(item, true); } }); }, }; </script> ``` 在上述示例中,tableData 数组中的每一项都添加了一个 selected 属性,用于表示该项是否被选中。在 mounted 钩子函数中,通过遍历 tableData 数组,将 selected 为 true 的项设置为选中状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值