Element 多选表格前端分页 多选框回显

一、Element 多选表格前端分页 多选框回显

先上效果图:
在这里插入图片描述
功能拆分:
(1)先模拟100条数据在 tableData
(2)将tableData 分页切片处理:
this.tableData.slice( (this.pageNumber - 1) * this.pageSize, this.pageNumber * this.pageSize );
注意,这里使用 computed 计算属性 pageTableData 来存储切片后当前页的列表数据
(3)分页时选择数据回显并且保存选中的数据 this.getDataByPage()

完整代码实现:

<template>
  <div>
    <el-table
      id="fixTable"
      class="myt-table"
      header-cell-class-name="myt-table-header"
      border
      :data="pageTableData"  
      :row-key="getRowKeys"
      ref="multipleTable_1"
      @selection-change="handleSelectionChange"
      @select-all="selectAll"
    > <!-- :height="tableHeight || 'auto'" -->
      <el-table-column type="selection" width="40" > </el-table-column>
      <el-table-column type="index" label="序号" width="60" align="left" class-name="pollution-table-pd"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="name" min-width="200" label="企业名称"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="city" width="100" label="所属地市"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="county" width="100" label="所属区县"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="conType" width="120" label="污染物类型"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="responseLevel" width="100" label="应对级别"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="enterType" width="120" label="企业类型"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="industryType" width="100" label="行业类型"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="enterpriseLevel" width="100" label="企业等级"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="measuresType" width="160" label="措施类型"> </el-table-column>
      <el-table-column align="left" class-name="pollution-table-pd" prop="measuresDetail" min-width="400" label="措施详情"> </el-table-column>
    </el-table>

    <div class="pd-top20 pd-bottom20 pager_add" id="pageFooter" >
      <div class="select-con" >
        <span class="select-text"
          >已选中
          <span class="select-num">{{ EleSelectedNum }}</span></span
        >
        <el-button @click="allCheckEvent" class="common-btn">全部选中</el-button>
        <el-button @click="cancelSelect" class="common-btn" v-show="EleSelectedNum > 0">取消选中</el-button>
      </div>

      <el-pagination
        class="myt-pager"
        background
        layout="total, prev, pager, next,sizes"
        :total="totalCount"
        :page-sizes="[10, 20, 30, 40, 50]"
        :current-page="pageNumber"
        @current-change="pageChange"
        @size-change="sizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageNumber: 1,
      pageSize: 10,
      totalCount: 0,
      tableData: [],
      selectedIds_1: [], //当前页选中的数据
      sourceAllData: [],
      EleSelectedNum: 0,
      selectedIdsAll_1: [], //所有选中的数据包含跨页数据
    }
  },
  computed: {
    pageTableData() {
      let arr = this.tableData.slice((this.pageNumber - 1) * this.pageSize, this.pageNumber * this.pageSize)
      return arr
    },
  },
  watch: {
    // 监听全部页选中的值
    selectedIdsAll_1(val) {
      this.calculateVal(this.selectedIds_1, val) //当前值 currentVal,全部值allVal
      this.EleSelectedNum = val.length
    },
    //监听当前页选中
    selectedIds_1(val) {
      this.calculateVal(val, this.selectedIdsAll_1)
    },
  },
  methods: {
    // 分页获取数据
    getDataByPage() {
      let arr = []
      this.pageTableData.forEach((work) => {
        if (this.selectedIdsAll_1.indexOf(work.id) >= 0) {
          for (let i = 0; i < this.selectedIdsAll_1.length; i++) {
            if (this.selectedIdsAll_1[i] === work.id) {
              arr.push(work)
            }
          }
        }
      })

      setTimeout(() => {
        this.handleSelectionChange(arr)
        this.selectedIds_1 = arr
        this.pageTableData.forEach((row) => {
          for (let j = 0; j < arr.length; j++) {
            if (row.id === arr[j].id) {
              this.$refs.multipleTable_1.toggleRowSelection(row)
            }
          }
        })
      })
    },
    // 第几页
    pageChange(val) {
      this.pageNumber = val
      this.getDataByPage()
    },
    // 当前分页大小
    sizeChange(val) {
      this.pageSize = val
      this.getDataByPage()
    },
    // 计算当前页的值与总选择集合
    calculateVal(currentVal, allValue) {
      // 当前页所有列表id集合 copyData
      let copyData = [] 
      this.pageTableData.forEach((work) => {
        copyData.push(work.id)
      })

      // 当前页选中的id集合:currentVal , 总选择集合:allValue
      currentVal.forEach((id) => {
        // 如果总选择里面不包含当前页选中的数据,那么就加入到总选择集合里
        if (allValue.indexOf(id) < 0) {
          allValue.push(id)
        }
      })

      // 当前页没有选中的id集合 noSelectIds
      let noSelectIds = [] 
      copyData.forEach((item) => {
        if (copyData.includes(item) && !currentVal.includes(item)) {
          noSelectIds.push(item)
        }
      })

      // 总选择里面的id集合
      let selectAllIds = JSON.parse(JSON.stringify(allValue))

      //总选中集合
      if (noSelectIds.length > 0) {
        noSelectIds.forEach((id) => {
          if (selectAllIds.indexOf(id) >= 0) {
            for (let i = 0; i < allValue.length; i++) {
              if (allValue[i] === id) {
                // 如果总选择中集合中有未被选中的,那么就删除这条
                allValue.splice(i, 1)
              }
            }
          }
        })
      }

    },
    // 选中
    handleSelectionChange(list) {
      let currentSelected = []
      list.forEach((item) => {
        currentSelected.push(item.id)
      })
      this.selectedIds_1 = currentSelected
    },
    // 解决elemetUI Table表格多选无数据时,复选框显示disabled点击却可勾选
    selectAll(val) {
      if (!val.length && !this.tableData.length) {
        this.$refs.multipleTable_1.clearSelection()
      }
    },
    // 取消选中
    cancelSelect() {
      this.$refs.multipleTable_1.clearSelection()
      this.selectedIdsAll_1 = []
      this.selectedIds_1 = []
    },
    // 全部选中
    allCheckEvent() {
      // 全部选中的数组
      let arr = []
      this.sourceAllData.forEach((item) => {
        arr.push(item.id)
      })

      this.selectedIdsAll_1 = arr
      let currentArr = []
      this.pageTableData.forEach((row) => {
        this.$refs.multipleTable_1.toggleRowSelection(row, true)
        currentArr.push(row.id) // 选中id数组
        this.selectedIds_1 = currentArr
      })
    },
    getRowKeys(row) {
      return row.id;
    },
  },
  created() {
    for (let i = 0; i < 100; i++) {
      let item = {
        id: i + 1,
        index: i,
        name: 'xxxxxx有限公司',
        city: '广州市',
        county: '黄埔区',
        conType: 'xx',
        responseLevel: 'xxx级',
        enterType: 'xxxx',
        industryType: 'xxxx',
        enterpriseLevel: 'x级',
        measuresType: 'xxxxx',
        measuresDetail: 'xxxxxx',
      }

      if (i === 11 || i === 12 || i === 13 || i === 14) {
        item.name = '第二页'
      }
      this.tableData.push(item)
    }

    this.sourceAllData = JSON.parse(JSON.stringify(this.tableData))
    this.totalCount = this.tableData.length
  },
}
</script>
<style scoped>
.pager_add {
  display: flex;
  background-color: #04294b;
  justify-content: space-between;
}
</style>

二、el-tree 带竖线

在这里插入图片描述
参考:
https://blog.csdn.net/weixin_42490398/article/details/107942461

<el-tree
	class="tree-line"
	icon-class="el-icon-circle-plus-outline"
	:indent="0"
	:data="data"
></el-tree>
// 以下为scss,记得去掉scoped,或者使用/deep/
<style lang="scss">
.tree-line{
  .el-tree-node {
    position: relative;
    padding-left: 16px; // 缩进量
  }
  .el-tree-node__children {
    padding-left: 16px; // 缩进量
  }

  // 竖线
  .el-tree-node::before {
    content: "";
    height: 100%;
    width: 1px;
    position: absolute;
    left: -3px;
    top: -26px;
    border-width: 1px;
    border-left: 1px dashed #52627C;
  }
  // 当前层最后一个节点的竖线高度固定
  .el-tree-node:last-child::before {
    height: 38px; // 可以自己调节到合适数值
  }

  // 横线
  .el-tree-node::after {
    content: "";
    width: 24px;
    height: 20px;
    position: absolute;
    left: -3px;
    top: 12px;
    border-width: 1px;
    border-top: 1px dashed #52627C;
  }

  // 去掉最顶层的虚线,放最下面样式才不会被上面的覆盖了
  & > .el-tree-node::after {
    border-top: none;
  }
  & > .el-tree-node::before {
    border-left: none;
  }
	
  // 展开关闭的icon
  .el-tree-node__expand-icon{
    font-size: 16px;
    // 叶子节点(无子节点)
    &.is-leaf{
      color: transparent;
      // display: none; // 也可以去掉
    }
  }
}
</style>

elementui表格多选框中进行回显,需要先在数据源中添加一个`selected`字段,用来表示该行数据是否被选中,然后在多选框的`v-model`属性中绑定该字段。当需要进行回显时,只需要将该数据源中的`selected`字段设置为`true`即可。 例如: ``` <template> <el-table :data="tableData"> <el-table-column type="selection" width="55"> </el-table-column> <el-table-column prop="name" label="姓名"> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', selected: false }, { name: '李四', selected: true }, { name: '王五', selected: false }, ] }; } } </script> ``` 以上代码中,`tableData`中的每条数据都有一个`selected`字段来表示是否被选中。同时,在多选框的`v-model`中绑定该字段: ``` <el-table-column type="selection" width="55" v-model="tableDataSelection"></el-table-column> ``` 这样,在表格中选中某些行之后,`tableDataSelection`中就会保存这些行的数据。当需要进行回显时,只需要遍历`tableData`中的每条数据,将`selected`字段设置为与其在`tableDataSelection`中的对应项一致即可。 例如: ``` // 假设已经从接口获取到了选中行的数据 const selectedRows = [ { name: '李四' }, { name: '王五' }, ]; // 遍历数据源,将选中行的数据进行回显 this.tableData.forEach(row => { const selectedRow = selectedRows.find(item => item.name === row.name); row.selected = !!selectedRow; // 将布尔值转换为数字,可以使用 !! 操作符 }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Windyluna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值