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

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

javascript 复制代码
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、有禁选项时,给表格行设置默认选中时需要把禁选的项给剔除掉

相关推荐
梦帮科技9 小时前
GRAVIS v4.0:基于Web的极速套利架构设计与实时数据流实现
前端·人工智能·rust·自动化·区块链·智能合约·数字货币
爱勇宝9 小时前
办公资料反复修改、补传、交接混乱,我做了个桌面工具来解决这件事
前端·后端·程序员
微信开发api-视频号协议10 小时前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
甲维斯10 小时前
Fable5:20美金的顶级设计师!
前端·人工智能
kyriewen10 小时前
我同时用了 Claude Code、Cursor 和 Codex,说说三个工具的真实差距——2026 年选错工具比不用 AI 更浪费时间
前端·ai编程·claude
Jackson__11 小时前
为了拯救我的腰椎和颈椎,我做了款浏览器插件!
前端·javascript·开源
闲猫11 小时前
Python FastAPI + SQLAlchemy 入门教程:从零搭建你的第一个 Web 应用
前端·python·fastapi
林小帅11 小时前
NestJS v11 + Prisma v7 ESM 迁移配置解析
前端·javascript·后端
IT_陈寒11 小时前
React的setState竟然不是立刻生效的,害我调试半天
前端·人工智能·后端
简简单单就是我_hehe11 小时前
前端埋点日志解决方案:完整 User-Agent 解析工具实现,爬虫识别、设备品牌、操作系统、浏览器版本全解析(附完整源码)
前端·爬虫