Vue+Element-UI Table表格实现复选框单选效果(隐藏表头上的全选Checkbox)及当表格数据 只有一条时,默认为选中状态

实现效果

在这里插入图片描述

完整代码

<div class="box-pos">
      <el-table ref="table" 
           :header-cell-style="{ color: '#FFF', background: '#333' }"
           :cell-style="{ color: '#FFF', background: '#333' }" 
           :data="grListData" style="width: 100%"
           @selection-change="selectionChange">
          <template slot="empty">
             <span style="color: #969799">{{ $t("NeoLight.empty") }}</span>
          </template>
          <el-table-column type="selection" width="50" />
          <el-table-column prop="plantCode" label="厂区" />
          <el-table-column prop="grCode" label="单据号" />
          <el-table-column prop="grItem" label="单据行" />
          <el-table-column prop="grDate" label="单据日期" />
          <el-table-column prop="materialCode" label="料号" />
          //........
     </el-table>
</div>

js代码

  data() {
        return {
            multipleSelection: [],
        };
    },
  methods: {
        selectionChange(data) {
            this.multipleSelection = []
            if (data.length > 1) {
                this.$refs.table.clearSelection()
                this.$refs.table.toggleRowSelection(data[data.length - 1])
            }
            this.multipleSelection = data[data.length - 1] ? [data[data.length - 1]] : []
        },
 }

隐藏表头上的全选Checkbox

.box-pos {
    /**找到表头那一行,然后把里面的复选框隐藏掉**/
    ::v-deep .el-table__header-wrapper .el-checkbox {
        display: none !important;
    }

}

参考链接:https://blog.csdn.net/a772116804/article/details/131899825

需求

当表格数据 只有一条时,默认为选中状态


<template>
  <el-table
    :data="tableData"
    @selection-change="handleSelectionChange"
    ref="multipleTable"
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      isSingleData: false
    };
  },
  watch: {
    tableData(newData) {
      if (newData.length === 1 && !this.isSingleData) {
        this.isSingleData = true;
        this.$nextTick(() => {
          this.$refs.multipleTable.toggleRowSelection(newData[0], true);
        });
      }
    }
  },
  methods: {
    handleSelectionChange(selection) {
      this.isSingleData = selection.length === 1;
    },
  }
};
</script>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Element-UI的树形Table组件可以通过设置show-checkbox属性来显示复选框,但是默认情况下只会在非叶子节点上显示复选框,如果需要在最后一层也显示复选框,可以通过以下两种方式实现: 1. 使用slot-scope自定义单元格内容 在template中使用slot-scope自定义单元格内容,通过判断当前行是否为最后一层来显示复选框。 ```html <el-table :data="data" 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 label="操作"> <template slot-scope="{ row }"> <el-checkbox v-if="isLastLevel(row)" v-model="checkedNodes" :label="row.id"></el-checkbox> </template> </el-table-column> </el-table> ``` 在methods中定义isLastLevel方法来判断当前行是否为最后一层: ```javascript methods: { isLastLevel(row) { return !row.children || row.children.length === 0; } } ``` 2. 使用tree-node-key属性指定叶子节点的key值 在使用树形数据,可以通过tree-node-key属性指定叶子节点的key值,然后在show-checkbox属性中使用leaf-only来显示所有叶子节点的复选框。 ```html <el-table :data="data" :tree-props="{children: 'children', hasChildren: 'hasChildren', id: 'id', label: 'name', key: 'id'}" :tree-node-key="'id'" :show-checkbox="true" :leaf-only="true" style="width: 100%"> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column label="操作"> <template slot-scope="{ row }"> <el-checkbox v-model="checkedNodes" :label="row.id"></el-checkbox> </template> </el-table-column> </el-table> ``` 在这种方式下,只需要在template中直接显示复选框即可,不需要判断当前行是否为最后一层。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值