el-table 自定义表头

el-table 自定义表头

在这里插入图片描述

利用复选框进行自定义表头设置
//示例 
    <el-checkbox
      v-model="stcheckAll"
      :indeterminate="stisIndeterminate"
      @change="sthandleCheckAllChange"
    >
      全选
    </el-checkbox>
    <el-checkbox-group v-model="selectedColumns" @change="handleColumnsChange">
      <el-checkbox
        v-for="column in columns"
        :key="column.field"
        :label="column.field"
        :disabled="column.disabled"
      >
        {{ column.fieldName }}
      </el-checkbox>
    </el-checkbox-group>
    <el-table :border="true" :data="tableData" :style="{ width: '100%' }">
      <el-table-column
        v-for="column in elTableitem"
        :key="column.field"
        :prop="column.field"
        :label="column.fieldName"
        :width="column.width"
        :fixed="column.fixed"
        show-overflow-tooltip
        align="center"
      />
    </el-table>
// 示例
const stcheckAll = ref(false);
const stisIndeterminate = ref(true);
const tableData = ref([
  {
    name: "宋江",
    age: 18,
    code: 23434564575867,
    stature: 2000,
    color: "red",
  },
  {
    name: "李逵",
    age: 18,
    code: 23434564575867,
    stature: 2000,
    color: "red",
  },
  {
    name: "林冲",
    age: 18,
    code: 23434564575867,
    stature: 2000,
    color: "red",
  },
  {
    name: "武大郎",
    age: 18,
    code: 23434564575867,
    stature: 2000,
    color: "red",
  },
]);
//初始选中所有列
const selectedColumns = ref(["name", "age"]);
const columns = ref([
  {
    field: "name",
    fieldName: "姓名",
    disabled: true,
  },
  {
    field: "age",
    fieldName: "年龄",
    disabled: true,
  },
  {
    field: "code",
    fieldName: "身份证号",
    width: "180",
    fixed: true,
    disabled: false,
  },
  {
    field: "stature",
    fieldName: "身高",
    disabled: false,
  },
  {
    field: "color",
    fieldName: "肤色",
    disabled: false,
  },
]);
//循环表格列表
const elTableitem = computed(() => {
  return columns.value.filter((column) =>
    selectedColumns.value.includes(column.field)
  );
});
// 全选/反选
const sthandleCheckAllChange = (val) => {
  selectedColumns.value = val
    ? columns.value.map((column) => column.field)
    : //默认禁用选中表头
      ["idCard", "name"];
  stisIndeterminate.value = false;
};
// 处理列选择变化
const handleColumnsChange = (value) => {
  const checkedCount = value.length;
  stcheckAll.value = checkedCount === columns.value.length;
  stisIndeterminate.value =
    checkedCount > 0 && checkedCount < columns.value.length;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值