简体   繁体   English

React-Table 固定第一行

[英]React-Table fixed first row

For starters, I've read this post on using footers to fix the last row , and also this whole thread on ignoring top-row when sorting .对于初学者,我已经阅读了这篇关于使用页脚修复最后一行的文章,以及关于在排序时忽略顶行的整个主题 In fact, I've read this thread several times over.事实上,我已经多次阅读这个线程。

The threads are inconclusive about how to pin a top-row with react-table, but it is majorly important for my current project and I am seeking a solution.这些线程关于如何用 react-table 固定顶行尚无定论,但这对我当前的项目非常重要,我正在寻求解决方案。 I created the following dummy table for help with this post:为了这篇文章的帮助,我创建了以下虚拟表:

 var ReactTable = ReactTable.default class App extends React.Component { constructor() { super(); this.state = { data: [ { firstName: 'joe', lastName: 'james', age: 18, status: 'real', visits: 14 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 } ] }; } render() { const { data } = this.state; return ( <div> <ReactTable data={data} columns={[ { Header: "Name", columns: [ { width: '100', Header: "First Name", accessor: "firstName" }, { width: '100', Header: "Last Name", id: "lastName", accessor: d => d.lastName } ] }, { Header: "Info", columns: [ { width: '100', Header: "Age", accessor: "age" }, { width: '100', Header: "Status", accessor: "status" } ] }, { Header: 'Stats', columns: [ { width: '100', Header: "Visits", accessor: "visits" } ] } ]} defaultPageSize={5} showPagination={false} className="-striped -highlight" /> <br /> </div> ); } } ReactDOM.render(<App />, document.getElementById("app"));
 <link href="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.js"></script> <div id="app"></div>

My goal currently is to create a fixed top-row in any way possible .我目前的目标是以任何可能的方式创建一个固定的顶行。 Whether that is by (a) having the desired first-row data as the first object in the data array, or (b) having the desired first-row data in an object that's entirely separate from the data array, and coding that into the header of the column, or (c) some other approach, I don't mind if it's a bit hacky.这是否是由(a)具有所需的第一行的数据作为在所述第一对象data阵列,或(b)具有在一个对象,它是完全从分离期望的第一行的数据data阵列,和编码到列的header ,或 (c) 一些其他方法,我不介意它是否有点 hacky。 It appears at least that there's no non-hacky approach this anyway... Feel free to create dummy data outside of data if you're able to pin it as the top row.看来,至少,有没有非哈克方法,这无论如何...随意创建的虚拟数据之外的data ,如果你能够将它作为脚顶行。

I'd like to style the fixed first-row differently from both the headers above it, and the rows below it.我想将固定的第一行与其上方的标题和下方的行设置为不同的样式。 For the sake of this post, any getting simple styling on this row (eg a different background color) would be sufficient!为了这篇文章,在这一行上任何简单的样式(例如不同的背景颜色)就足够了!

This website here (not built in React tho) has a great example of a fixed first row.这个网站(不是用 React 构建的)有一个固定第一行的很好的例子。 When you sort the table, the Totals row remains on top.对表格进行排序时, Totals行仍位于顶部。 It is also styled separately from other rows.它的样式也与其他行分开。

Will bounty this post in 2 days, and any answers beforehand as well, as I could really really really use the help.将在 2 天内赏金这篇文章,以及任何事先的答案,因为我真的可以真正真正地使用帮助。 Any thoughts / assistance is appreciated!任何想法/帮助表示赞赏!

Edit: - another suggestion (although I dont know how it could be done) is to simply reposition React-Table's Footer as a first row, between the headers and the data.编辑: - 另一个建议(虽然我不知道如何实现)是简单地将 React-Table 的Footer重新定位为标题和数据之间的第一行。 Footer is immune to sorting, although its on the bottom, not the top. Footer不受排序的影响,尽管它在底部,而不是顶部。

Edit 2: - per this thread , it is not possible to have a 3rd headerGroup, which would be needed since my websites tables already have a headerGroup and another main header.编辑 2: - 根据这个线程,不可能有第三个 headerGroup,因为我的网站表已经有一个 headerGroup 和另一个主标题,所以这是必需的。

Add style option with height添加带有高度的样式选项

style={{
    height: "200px"
}}

 var ReactTable = ReactTable.default class App extends React.Component { constructor() { super(); this.state = { data: [ { firstName: 'joe', lastName: 'james', age: 18, status: 'real', visits: 14 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 }, { firstName: 'tom', lastName: 'smith', age: 15, status: 'okay', visits: 24 } ] }; } render() { const { data } = this.state; return ( <div> <ReactTable data={data} columns={[ { Header: "Name", columns: [ { width: '100', Header: "First Name", accessor: "firstName" }, { width: '100', Header: "Last Name", id: "lastName", accessor: d => d.lastName } ] }, { Header: "Info", columns: [ { width: '100', Header: "Age", accessor: "age" }, { width: '100', Header: "Status", accessor: "status" } ] }, { Header: 'Stats', columns: [ { width: '100', Header: "Visits", accessor: "visits" } ] } ]} defaultPageSize={5} showPagination={false} style={{ height: "200px" }} className="-striped -highlight" /> <br /> </div> ); } } ReactDOM.render(<App />, document.getElementById("app"));
 <link href="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.js"></script> <div id="app"></div>

If you don't need the Footer you can render the first row within it and then reorder the html:如果您不需要页脚,您可以渲染其中的第一行,然后重新排序 html:

.rt-table > .rt-thead { order: -2 }
.rt-table > .rt-tbody { order: -1 }
.rt-table > .rt-tfoot { order: -2 }

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

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