react antd table expandable defaultExpandAllRows 不生效问题

原因:defaultExpandAllRows只会在第一次渲染时触发

解决方案:渲染前判断table 的datasource 数据是否已准备好

{
   
   pageList.length > 0 ? (
        <Table
          rowSelection={
   
   rowSelection}
          columns={
   
   columns}
          dataSource={
   
   
### 实现 React 中 Ant Design Table 组件的自定义行 在 React 应用程序中使用 Ant Design 的 `Table` 组件时,可以通过设置 `rowClassName` 或者 `components` 属性来自定义表格中的每一行[^1]。 #### 使用 `rowClassName` 通过传递给 `Table` 组件一个函数作为 `rowClassName` 参数,可以根据特定条件返回不同的 CSS 类名。这允许基于数据动态应用样式: ```jsx import React from 'react'; import { Table } from 'antd'; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name' }, // ...其他列配置... ]; // 行类名定制逻辑 const rowClassName = (record, index) => { if (index % 2 === 0) return 'custom-row-even'; // 偶数行 return 'custom-row-odd'; // 奇数行 }; const App = () => ( <Table columns={columns} dataSource={data} rowClassName={rowClassName} /> ); ``` 此方法适用于简单的样式调整需求[^1]。 #### 自定义渲染整个 `<tr>` 元素 对于更复杂的场景,比如想要完全控制每行的内容结构或行为,则可以利用 `components` 属性来替换默认的表体部分 (`body`) 和单元格 (`cell`) 渲染方式: ```jsx import React from 'react'; import { Table } from 'antd'; class CustomRow extends React.Component { render() { const { record, rowIndex, children, ...restProps } = this.props; return ( <tr {...restProps}> {/* 可在此处添加额外的操作按钮或其他交互元素 */} {children} <td> <button onClick={() => console.log('Edit', record)}>编辑</button> </td> </tr> ); } } const components = { body: { row: CustomRow, }, }; const App = () => ( <Table columns={columns} dataSource={data} components={components} /> ); ``` 这种方式提供了更大的灵活性去修改 HTML 结构以及增强用户体验[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十方来财

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值