当前位置: 首页 > article >正文

ant-design-vue的table组件的首列复选框设置问题,包括设置默认选中,设置禁选条件

想要使用表格的复选框列并控制复选框是否可选,先上代码如下

html部分
  <a-table :rowKey="(record,index)=>{return record.id?record.id: index}"
      :columns="columns" :data-source="tableData"
             :row-selection="showSelection?rowSelection: null"
             :pagination="pagination">
      <template slot="isWarn" slot-scope="text">
        <span style="color: #D9001B;">{{ text==='1'?'预警': '未预警' }}</span>
      </template>
</a-table>

js部分
data() {
	return {
		showSelection: true,
		pagination: {
          total: 0,
          pageSize: 10, // 每页中显示10条数据
          showSizeChanger: true,
          showQuickJumper: true,
          current: 1,
          pageSizeOptions: ['10', '20', '50', '100'], // 每页中显示的数据
          // showTotal: (total) => `共有 ${total} 条数据`, // 分页中显示总的数据 this.$t('COMMON.INTER_TOTAL', { total }), //
          // showTotal: (total, range) => `共 ${total} 条记录,第${this.pagination.current}`,
          onChange: (pageNumber) => {
            this.pagination.current = pageNumber;
            this.getOrderListFn();
          },
          onShowSizeChange: (current, pageSize) => {
            this.pagination.pageSize = pageSize;
            this.pagination.current = 1;
            this.getOrderListFn();
          }
        },
        columns: [
	       	{
	           title: '是否预警',
	            dataIndex: 'isWarn',
	            key: 'isWarn',
	            scopedSlots: { customRender: 'isWarn' }
	          },
	          {
	           title: '状态',
	            dataIndex: 'checkState',
	            key: 'checkState'
	          }
        ],
        tableData: [
        	{
				id: 1,
				checkState: null,
				isWarn: '1'
			},
			{
				id: 2,
				checkState: 1,
				isWarn: '0'
			},
			{
				id: 3,
				checkState: null,
				isWarn: '2'
			}
		],
	}
},
computed: {
	rowSelection() {
        return {
          onChange: (selectedRowKeys, selectedRows) => {
            console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            this.selectedRowKeys = selectedRowKeys;
          },
          getCheckboxProps: (record) => ({
            props: {
              disabled: !record.checkState, // 禁选的条件
            }
          }),
          selectedRowKeys: this.selectedRowKeys,// 可以通过给this.selectedRowKeys赋值来设置默认选中项,注意,如果有禁选条件,赋值时要把禁选的给剔除掉
          // onSelectAll: (selected, selectedRows) => {
          //   // 重置全选状态,确保禁用的复选框不会被选中
          //   const newSelectedRows = selectedRows.filter(row => !row.checkState||!row.sendState);
          //   console.log('newSelectedRows', newSelectedRows);
          //   return newSelectedRows
          //   // 其余逻辑保持不变
          // },
        };
      },
	}
注意的点如下

1、给表格设置rowKey时,尽量不用索引来设置,使用索引来设置会出现禁选项虽然禁选了,但是点击表头全选框的时候依然能被选中的情况,如下图
在这里插入图片描述

2、rowSelection写在computed里
3、有禁选项时,给表格行设置默认选中时需要把禁选的项给剔除掉


http://www.kler.cn/a/280023.html

相关文章:

  • 【Flask 数据库 操作】数据库迁移
  • 基于大数据的水资源管理与调度优化研究【Web可视化、灰色预测、大屏设计】
  • TLB的刷新方式--linux 2.4
  • 五、OpenTK图形渲染基础
  • Navicat连接SqlServer
  • 一篇文章带你入门Golang
  • Mamba 2的发布是否可以撼动Transformer模型的AI大一统的江湖地位
  • 代码随想录算法训练营第五十八天 | 拓扑排序精讲、dijkstra(朴素版)精讲
  • 深度洞察:用PyTorch的torch.profiler解锁性能之谜
  • STM32学习记录-05 -1-TIM定时中断
  • IOS 17 基于UITabBarController实现首页TabBar
  • 电脑ip地址为什么会自己变更?电脑ip怎么改
  • 【精选】计算机毕业设计之:基于springboot超市进销存系统
  • streamlit+wordcloud使用pyinstaller打包遇到的一些坑
  • 使用Python+winreg修改/重命名Windows注册表的键
  • 安卓App开发 Ex篇:命令行工具
  • 学习日志28
  • Gauge使用体验
  • 小琳 AI 课堂:机器学习
  • 35岁程序员的4条出路!请提早布局!