对支持全选的el-table进行筛选并且与另一个table进行数据关联

效果图

​​在这里插入图片描述
在这里插入图片描述

代码

弹窗代码

<!-- 弹窗组件 -->
          <el-dialog title="选择设备" :visible.sync="dialogVisible" width="50%">
            <div class="table-container">
              <div style="display: flex; gap: 20px; overflow: auto">
                <small style="width: 10%; margin-top: 17px; margin-left: 20px">
                  设备名称:</small
                >
                <el-input
                  v-model="query.searchQuery"
                  placeholder="搜索设备"
                  style="margin-bottom: 20px; margin-top: 10px; width: 30%"
                /><span style="margin-top: 20px"
                  >已选择{{ this.query.selectedData.length }}台设备</span
                >
              </div>

              <div
                style="display: flex; gap: 20px; height: 300px; overflow: auto"
              >
                <!-- 第一个表格,显示所有数据 -->
                <el-table
                  ref="myTable"
                  :data="filteredData"
                  @select-all="handleSelectAll"
                  @selection-change="handleSelectionChange"
                  style="width: 50%; margin-left: 50px"
                >
                  <el-table-column type="selection" width="55">
                  </el-table-column>
                  <el-table-column
                    prop="deviceName"
                    label="设备名称"
                    width="150"
                  >
                  </el-table-column>
                </el-table>

                <!-- 第二个表格,显示选中的数据 -->
                <el-table
                  :data="query.selectedData"
                  style="width: 50%; margin-left: 50px"
                >
                  <el-table-column
                    prop="deviceName"
                    label="设备名称"
                    width="150"
                  >
                  </el-table-column>
                  <el-table-column label="操作" min-width="50" >
                    <template slot-scope="{ row }">
                      <el-button
                        type="danger"
                        icon="el-icon-delete"
                        @click="removeDevice(row.deviceId)"
                        circle
                      ></el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>

            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="confirmSelection"
                >确定</el-button
              >
            </div>
          </el-dialog>

js代码

data() {
    return {
      dialogVisible: false,
      msg: "未选择设备,默认查询全部设备",
      query:{
              ids: [],
              count: 10,
              startTempTime: "",
              searchQuery: "",
              selectedData: [],      
              }
           }
      },
  computed: {
    filteredData() {
      return this.list.filter((item) =>item.deviceName.includes(this.query.searchQuery)
      );
    },
  },
  methods: {
      handleSelectAll(selection) {
      if (selection.length == 0 && this.query.searchQuery == "") {
        //  this.query.selectedData=[]
        this.query.selectedData = [];
        return;
      }
      console.log("全选操作,当前选中的所有行:", selection);
    },
    // 监听第一个表格的选择事件
    handleSelectionChange(selection) {
      if (selection.length == 0 && this.query.searchQuery == "") {
        if (this.query.selectedData.length == 1) {
          this.query.selectedData = [];
        }
        this.restoreSelection();
        return;
      }
      if (
        selection.length == 0 &&
        this.query.searchQuery != "" &&
        this.query.selectedData.length > 0
      ) {
        this.restoreSelection();
        return;
      }
      
      // 获取 filteredData 中不在 selection 中的元素
      const difference1 = this.filteredData.filter(
        (x) => !selection.includes(x)
      );
      const set = new Set(this.query.selectedData);
      // this.query.selectedData = selection;
      var ids = [];
      selection.forEach((element) => {
        ids.push(element.deviceId);
        set.add(element);
      });

      var newList = Array.from(set);
      this.query.selectedData = newList.filter((x) => !difference1.includes(x));
      this.restoreSelection();
      this.query.ids = ids;
    },
    // 确认选择
    confirmSelection() {
      if (this.query.selectedData.length != 0) {
        this.msg = "已选择" + this.query.selectedData.length + "台设备";
      }
      this.dialogVisible = false;
    },
     removeDevice(deviceId) {
      this.query.selectedData = this.query.selectedData.filter(
        (row) => row.deviceId !== deviceId
      );
      const row = this.filteredData.find((row) => row.deviceId === deviceId);
      if (row) {
        this.$refs.myTable.toggleRowSelection(row, false); // 取消选中
      }
    },
    restoreSelection() {
      this.$nextTick(() => {
        this.query.selectedData.forEach((row) => {
          this.$refs.myTable.toggleRowSelection(row, true);
        });
      });
    },            
}
(小小的说明一下,我不是专门搞前端的,前端水平有限,就是需要实现这个功能,但是没找到满足需求的,就只能自己写啦,可能写的一坨哈哈哈哈,但是还是满足需求了,需要的话可以参考一下下哈哈哈哈)
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,针对您的问题,您可以参考以下步骤来实现el-table分页组件支持全选: 1. 在el-table头部添加一个checkbox,用来全选当前页的数据,代码如下: ```html <el-table-column type="selection" width="55"></el-table-column> ``` 2. 在el-table上添加v-model,用来绑定当前选中的数据,代码如下: ```html <el-table v-model="selectedData" :data="tableData" :pagination="pagination"> ``` 3. 在el-pagination上添加@size-change和@current-change事件,用来监听分页变化事件,代码如下: ```html <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-sizes="[10, 20, 30, 50]" :page-size.sync="pagination.pageSize" :total="total"> </el-pagination> ``` 4. 在methods中添加handleSizeChange和handleCurrentChange方法,用来处理分页变化事件,代码如下: ```javascript methods: { handleSizeChange (val) { this.pagination.pageSize = val this.loadTableData() }, handleCurrentChange (val) { this.pagination.currentPage = val this.loadTableData() }, loadTableData () { // 加载当前页数据 } } ``` 5. 在computed中添加selectedData和total属性,分别用来计算当前选中的数据数据总数,代码如下: ```javascript computed: { selectedData () { return this.tableData.filter(item => this.selection.includes(item.id)) }, total () { return this.tableData.length } } ``` 这样就可以实现el-table分页组件支持全选了,希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值