javascript - 如何在 React Js 的 for react-table 中添加类?
问题描述
我正在使用 React js,并且我已经实现了 react-table 的通用组件
我正在为表使用react-table库。
我需要为某些特定行添加自定义类。
我已经尝试添加几个行道具,但我没有成功。
这是我的表格示例
import React from 'react';
import { useTable, useExpanded } from 'react-table';
function Table({ className, colSpan, columns, data }) {
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state: { expanded },
} = useTable(
{
className,
colSpan,
columns,
data,
},
useExpanded // Use the useExpanded plugin hook
);
// Render the UI for your table
return (
<table className={className} {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>
{cell.render('Cell')}
</td>
);
})}
</tr>
);
})}
{rows.length === 0 && (
<tr>
<td colSpan={colSpan}>No Record Found</td>
</tr>
)}
</tbody>
</table>
);
}
解决方案
在 PropsMethods 中传递 className。
<tr {...row.getRowProps({className: 'row'})}>
{row.cells.map(cell => {
return <td {...cell.getCellProps({className: 'cell'})}>
{cell.render('Cell')}</td>
})}
</tr>
这是您可能想要实现的目标的示例。
推荐阅读
- javascript - 在 JavaScript 中生成长字符串的最快方法(性能)
- c++ - C ++:包含彼此的标题会导致错误“尚未声明类”
- javascript - 部署在 GitHub 页面上的 ReactJS 应用程序不适用于自定义域集
- c++ - 从邻接表 C++ 中删除元素
- sql - 按不同列值(时间戳)的 SQL 累积平均值
- laravel - 带有承载认证的 Laravel Json 测试
- javascript - 如何从 Firebase 以及 React Native 中的数组中删除数据
- audio - FFMPEG - 多轨、多通道文件到离散单声道文件
- python - 卡住未定义的变量,但找不到任何拼写错误或其他可能导致它的原因?
- postman - 从 Postman 调用受护照推特策略保护的 Passport API 端点