简体   繁体   English

如何在 React Js 的 for react-table 中添加 class?

[英]How to add class in the <tr> for react-table in React Js?

I am working in React js and I already implemented the common component for the react-table我在 React js 工作,我已经实现了 react-table 的通用组件

I am using react-table library for the table.我正在为表格使用react-table库。

I need to add the custom class for some specific rows.我需要为某些特定行添加自定义 class。

I already try to add couple of row props as well but i didn't get success in that.我已经尝试添加几个行道具,但我没有成功。

Here is my table example这是我的表格示例

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>
);

} }

Pass the className inside the PropsMethods.在 PropsMethods 中传递 className。

<tr {...row.getRowProps({className: 'row'})}>
      {row.cells.map(cell => {
       return <td {...cell.getCellProps({className: 'cell'})}> 
           {cell.render('Cell')}</td>
            })}
   </tr>

Here is an example of what you would possibly like to achieve.是您可能希望实现的示例。

I am guessing that you are trying to add a class or multiple classes to some specific rows in your table based on some criteria.我猜您正在尝试根据某些条件向表中的某些特定行添加 class 或多个类。

Let's say you have to apply a class to every row with an even index.假设您必须将 class 应用于具有偶数索引的每一行。 This can be achieved like so:这可以像这样实现:

<tbody {...getTableBodyProps()}>
            {rows.map((row, i) => {
                prepareRow(row);
                return (
                    <tr 
                      className = {i%2 === 0? "your-classname": null} 
                      {...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>

If you want to avoid writing logic inside your tags then you can also use the classnames package. It will make your code look cleaner and easy to read.如果你想避免在你的标签中编写逻辑,那么你也可以使用名 package。这将使你的代码看起来更清晰易读。

My answer is only valid if you are conditionally trying to add classes to your rows.仅当您有条件地尝试向行中添加类时,我的回答才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM