首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在表React中添加更多视图

在表React中添加更多视图
EN

Stack Overflow用户
提问于 2021-08-21 23:48:43
回答 1查看 35关注 0票数 0

我有一种情况,我在frontend上有像50 records一样的complete data

我必须在Table中显示记录。

有一次我只能show 10记录。然后我有了view more按钮。click查看更多. It will load the next 10记录等等...

我能得到一些帮助来解决这个问题吗?

表代码:-4\f25 components TableWrapper TableBody TableHead TableRow -4\f6(-4\f25 components-4\f6组件-4\f25 Code-4\f6组件

TableWrapper.js

代码语言:javascript
复制
const TableWrapper = ({...props}) => {
  const { tableData, columns } = props;
  const addNewRows = () => {
   console.log("Code for adding extra rows");
  }
  return (
    <table>
      <TableHead headers={columns} />
      <TableBody rows={tableData} tableData={tableData} headers={columns} />
    </table>
    <fragment onClick=addNewRows>View more</fragment>
  );
};

export default TableWrapper;

TableBody.js

代码语言:javascript
复制
const TableBody = ({...props}) => {
  const { rows, tableData, headers } = props;

  const [columnHeaderKeys, setColumnHeaderKeys] = useState([]);

  useEffect(()=>{
    let keysArray = [];
    headers.map((headKeys) =>{
      keysArray.push(headKeys.field)
    })
    {keysArray.length !== 0 ? setColumnHeaderKeys(keysArray) : setColumnHeaderKeys([])}
  },[])

  return (
    <>
    {tableData.length !== 0 ?
      tableData.data.firstResponse.map((row, index)=>{
        return (<tr key={index}><TableRow key={index} data={row} keys={columnHeaderKeys}/></tr>)
      }) : <div>{"No Data is there"}</div>
    }
    </>
  );
};

export default TableBody;

TableHead.js

代码语言:javascript
复制
const TableHead = ({...props}) => {
  const { headers } = props;
  return (
    <>
        {headers.map(header => (
          <th
            key={`table-header-${header.key}`}
            style={header.style}>
            {header.header}
          </th>
        ))}
    </>
  );
};

export default TableHead;

TableRow.js

代码语言:javascript
复制
const TableRow = ({...props}) => {
  const { keys, data } = props;

  return (
    <>
    {keys.map((key)=>{
        return <td key={props.data[key]}>{props.data[key]}</td>
    })}
    </>
  );
};

export default TableRow;

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-08-22 00:34:30

假设tableData包含所有行(50条记录),您可以跟踪使用useState钩子应该显示多少条记录。然后,只需根据该值提取所需的行,并将其传递到表中。

如下所示:

代码语言:javascript
复制
const PAGE_SIZE = 10;

const TableWrapper = ({...props}) => {
  const { tableData, columns } = props;

  const [ numToDisplay, setNumToDisplay ] = useState(PAGE_SIZE);
  
  const addNewRows = () => {
    console.log("Code for adding extra rows");
    let newCount = numToDisplay + PAGE_SIZE;
    if (numToDisplay > tableData.length) {
      newCount = tableData.length;
    }
    setNumToDisplay(newCount)
  }

  const visibleData = tableData.slice(0, numToDisplay);
  
  return (
    <table>
    <TableHead headers={columns} />
    <TableBody tableData={visibleData} headers={columns} />
    </table>
    <fragment onClick=addNewRows>View more</fragment>
  );
};

我也不认为你需要TableBody上的rows属性,因为tableDatarows是一样的(从你的描述中我可以看出)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68877525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档