element-ui 使用自定义复选框

为什么不使用自带复选框?

自带复选框无法确定数据来源于回填还是用户手动选择,因为回填表格复选框时只能使用toggleRowSelection方法来遍历回填,而且会触发selection-change事件,而手动添加也会触发selection-change事件,无法判断某个选中数据来源于回填还是用户手动选择。

无法满足某些需要,

需求也挺奇葩,表格选择完后下方有个开关,每次提交开关状态需要独立,第一次选了1,2,5以及开关为关,再次打开时(第二次)回填1,2,5,且保存每一行的开关状态,再选3,4,6时开关打开,此时数据应该为1,2,5,关且3,4,6为开。如果第二次选择3,4,5.此时数据为:1,2为关,3,4,5为开

无法满足的需求简单讲就是:之前数据未变更保留原来的状态,有变更(取消/取消在选中)则使用新状态。

具体实现功能:

单选,多选,跨页选,半选,

详见代码入选

<template>
  <div class="main-content">
    <!-- 使用自己的表格多选系统 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column width="120">
        <!-- slot-scope="scope"这个不能少,不然checkbox会存在问题 -->
        <template slot="header" slot-scope="scope">
          <el-checkbox
            v-model="checkAll"
            @change="handleAllChange"
            :indeterminate="isIndeterminate"
            >全选</el-checkbox
          >
        </template>
        <template slot-scope="{ row }">
          <el-checkbox
            @change="handleSelectChange($event, row)"
            :value="checkIds.includes(row.shopId)"
            ></el-checkbox>
        </template>
      </el-table-column>
      <el-table-column label="shopId" width="120">
        <template slot-scope="scope">{{ scope.row.shopId }}</template>
      </el-table-column>
      <el-table-column prop="shopName" label="姓名" width="120">
      </el-table-column>
    </el-table>
    <div class="pagebox pall15">
      <div class="eboss-row mt20 justify-center">
        <div class="page-scroll">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageIndex"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import mock from "mockjs";
export default {
  name: "tableListDemo",
  desc:"不使用表格自带的勾选,使用自定义勾选",
  data() {
    return {
      totalCount: 0,
      pageIndex: 1,
      pageSize: 10,
      pageSizes: [10, 20, 30, 40],
      tableData: [],
      checkIds: [],//选中的id,可包含跨页数据
      checkAll: false,//当页的全选按钮状态
      isIndeterminate: false,//当页全选按钮的半选状态
    };
  },
  mounted() {
    this.queryList();
  },
  methods: {
    queryList() {
      let params = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      };
      const res = mock.mock({
        success: true,
        model: {
          "shopList|3": [
            {
              shopId: "@id",
              shopName: "@cname",
            },
          ],
        },
        pageIndex: 1,
        totalCount: 500,
        pageSize: 10,
      });
      res.model.shopList.unshift(
        ...[
          {
            shopId: 8888,
            shopName: 8888,
          },
          {
            shopId: 9999,
            shopName: 9999,
          },
        ]
      );
      console.log("mockData--", res);
      if (res.success) {
        this.totalCount = res.totalCount;
        this.tableData = res.model.shopList;
      } else {
        this.$alert(res.errorMessage, "提示");
      }
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.pageIndex = 1;
      this.queryList();
    },
    handleCurrentChange(pageIndex) {
      this.pageIndex = pageIndex;
      this.queryList();
    },
    // 行内单选选择
    handleSelectChange(e, row) {
      if (e == true) {
        // 从未选中==>选中
        this.checkIds.push(row.shopId);
      } else {
        let index = this.checkIds.findIndex((e) => e == row.shopId);
        this.checkIds.splice(index, 1);
      }
      const currentTableIds = this.tableData.map((e) => e.shopId);
      const currentTableCheckedIds = currentTableIds.filter((e) =>
        this.checkIds.includes(e)
      );
      this.checkAll = currentTableCheckedIds.length == currentTableIds.length;
      this.isIndeterminate =
        currentTableCheckedIds.length > 0 &&
        currentTableCheckedIds.length < currentTableIds.length;
    },
    // 页内全选选择
    handleAllChange(e) {
      const currentTableIds = this.tableData.map((e) => e.shopId);
      this.isIndeterminate = false;
      if (e == true) {
        // 从未选中==>选中
        let ids = this.checkIds.concat(currentTableIds);
        // id需去重
        this.checkIds = [...new Set(ids)];
      } else {
        // 取消勾选,从选中id列表里把当前页的所有id均移除
        this.checkIds = this.checkIds.filter((e) => {
          return !currentTableIds.includes(e);
        });
      }
    },
  },
};
</script>

<style scoped lang="less"></style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以提供以下解决方案: 1. 使用 Element UIel-checkbox 组件实现复选框功能; 2. 在复选框的下方添加一个搜索框,用于搜索需要选择的选项; 3. 在复选框的左上角添加一个全选按钮,用于全选或取消全选所有选项。 代码示例: ```html <template> <div> <el-checkbox v-model="checkAll" @change="handleCheckAll">{{checkAll ? '取消全选' : '全选'}}</el-checkbox> <el-input v-model="searchText" placeholder="请输入搜索内容"></el-input> <div v-for="item in filteredOptions" :key="item.value"> <el-checkbox :label="item.value" v-model="checkedValues">{{item.label}}</el-checkbox> </div> </div> </template> <script> export default { data() { return { options: [{ label: '选项1', value: 'option1' }, { label: '选项2', value: 'option2' }, { label: '选项3', value: 'option3' }], checkedValues: [], searchText: '', checkAll: false } }, computed: { filteredOptions() { return this.options.filter(item => item.label.includes(this.searchText)) } }, methods: { handleCheckAll() { if (this.checkAll) { this.checkedValues = this.options.map(item => item.value) } else { this.checkedValues = [] } } } } </script> ``` 在上面的代码中,我们使用Element UIel-checkbox 和 el-input 组件来实现复选框和搜索框。同时,在复选框的左上角添加了一个全选按钮,通过 computed 计算属性实现对搜索后的选项进行过滤。当用户勾选全选时,将所有选项的 value 值添加到 checkedValues 数组中;当用户取消全选时,将 checkedValues 数组清空。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值