Skip to content

Commit 870b474

Browse files
author
Kamran Asif
committed
Adding jump to row example
1 parent 9f6e19d commit 870b474

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

examples/ScrollToExample.js

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/**
2+
* This file provided by Facebook is for non-commercial testing and evaluation
3+
* purposes only. Facebook reserves all rights not expressly granted.
4+
*
5+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
9+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
10+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
"use strict";
14+
15+
var ExampleImage = require('./helpers/ExampleImage');
16+
var FakeObjectDataListStore = require('./helpers/FakeObjectDataListStore');
17+
var FixedDataTable = require('fixed-data-table');
18+
var React = require('react');
19+
20+
const {Table, Column, Cell} = FixedDataTable;
21+
22+
const ImageCell = ({rowIndex, data, col, ...props}) => (
23+
<ExampleImage
24+
src={data.getObjectAt(rowIndex)[col]}
25+
/>
26+
);
27+
28+
const TextCell = ({rowIndex, data, col, query, ...props}) => (
29+
<Cell {...props}>
30+
{data.getObjectAt(rowIndex)[col]}
31+
</Cell>
32+
);
33+
34+
class DataListWrapper {
35+
constructor(indexMap, data) {
36+
this._indexMap = indexMap;
37+
this._data = data;
38+
}
39+
40+
getSize() {
41+
return this._indexMap.length;
42+
}
43+
44+
getObjectAt(index) {
45+
return this._data.getObjectAt(
46+
this._indexMap[index],
47+
);
48+
}
49+
}
50+
51+
class ScrollToExample extends React.Component {
52+
constructor(props) {
53+
super(props);
54+
55+
this._dataList = new FakeObjectDataListStore(2000);
56+
var data = this._dataList.getAll();
57+
58+
this.state = {
59+
filteredDataList: this._dataList,
60+
currentIndex: 0,
61+
matchedRows: [],
62+
};
63+
64+
this._onFilterChange = this._onFilterChange.bind(this);
65+
this._rowClassNameGetter= this._rowClassNameGetter.bind(this);
66+
this._prevSearch = this._prevSearch.bind(this);
67+
this._nextSearch = this._nextSearch.bind(this);
68+
}
69+
70+
_onFilterChange(e) {
71+
if (!e.target.value) {
72+
this.setState({
73+
query: null,
74+
matchedRows: [],
75+
});
76+
}
77+
78+
var filterBy = e.target.value.toLowerCase();
79+
var size = this._dataList.getSize();
80+
var filteredIndexes = [];
81+
for (var index = 0; index < size; index++) {
82+
var {firstName} = this._dataList.getObjectAt(index);
83+
if (firstName.toLowerCase().indexOf(filterBy) !== -1) {
84+
filteredIndexes.push(index);
85+
}
86+
}
87+
88+
this.setState({
89+
query: filterBy,
90+
matchedRows: filteredIndexes,
91+
});
92+
}
93+
94+
_prevSearch() {
95+
this.setState({
96+
currentIndex: this.state.currentIndex - 1
97+
});
98+
}
99+
100+
_nextSearch() {
101+
this.setState({
102+
currentIndex: this.state.currentIndex + 1
103+
});
104+
}
105+
106+
_rowClassNameGetter(rowIndex) {
107+
if (rowIndex === this.state.matchedRows[this.state.currentIndex]) {
108+
return 'active-row';
109+
}
110+
if (this.state.matchedRows.includes(rowIndex)) {
111+
return 'highlight-row';
112+
}
113+
}
114+
115+
render() {
116+
var {filteredDataList, index, currentIndex, matchedRows, query} = this.state;
117+
118+
var search = matchedRows.length ? (
119+
<span>
120+
<button
121+
disabled={currentIndex === 0}
122+
onClick={this._prevSearch}
123+
>
124+
{'<'}
125+
</button>
126+
{currentIndex + 1} / {matchedRows.length}
127+
<button
128+
disabled={currentIndex === matchedRows.length - 1}
129+
onClick={this._nextSearch}
130+
>
131+
{'>'}
132+
</button>
133+
<br />
134+
</span>
135+
) : null;
136+
137+
return (
138+
<div>
139+
<input
140+
onChange={this._onFilterChange}
141+
placeholder="Filter by First Name"
142+
/>
143+
<br />
144+
{search}
145+
<Table
146+
rowHeight={50}
147+
rowsCount={filteredDataList.getSize()}
148+
rowClassNameGetter={this._rowClassNameGetter}
149+
scrollToRow={matchedRows[currentIndex]}
150+
rowClass
151+
headerHeight={50}
152+
width={1000}
153+
height={500}
154+
{...this.props}>
155+
<Column
156+
cell={<ImageCell data={filteredDataList} col="avartar" />}
157+
fixed={true}
158+
width={50}
159+
/>
160+
<Column
161+
header={<Cell>First Name</Cell>}
162+
cell={<TextCell query={query} data={filteredDataList} col="firstName" />}
163+
fixed={true}
164+
width={100}
165+
/>
166+
<Column
167+
header={<Cell>Last Name</Cell>}
168+
cell={<TextCell data={filteredDataList} col="lastName" />}
169+
fixed={true}
170+
width={100}
171+
/>
172+
<Column
173+
header={<Cell>City</Cell>}
174+
cell={<TextCell data={filteredDataList} col="city" />}
175+
width={100}
176+
/>
177+
<Column
178+
header={<Cell>Street</Cell>}
179+
cell={<TextCell data={filteredDataList} col="street" />}
180+
width={200}
181+
/>
182+
<Column
183+
header={<Cell>Zip Code</Cell>}
184+
cell={<TextCell data={filteredDataList} col="zipCode" />}
185+
width={200}
186+
/>
187+
</Table>
188+
</div>
189+
);
190+
}
191+
}
192+
193+
module.exports = ScrollToExample;

site/Constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ exports.ExamplePages = {
5252
title: 'Reorderable columns',
5353
description: 'Table with drag and drop column reordering and a dummy "store" for persistence.',
5454
},
55+
SCROLL_TO_EXAMPLE: {
56+
location: 'example-scroll-to.html',
57+
file: EXAMPLES_LOCATION_BASE + 'ScollToExample.js',
58+
title: 'Jump to row',
59+
description: 'A table example that will list search matches and jump to specified row',
60+
},
5561
COLUMN_GROUPS_EXAMPLE: {
5662
location: 'example-column-groups.html',
5763
file: EXAMPLES_LOCATION_BASE + 'ColumnGroupsExample.js',

site/base.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ blockquote > :last-child {
137137
color: #998;
138138
font-style: italic;
139139
}
140+
141+
.active-row .public_fixedDataTableCell_main {
142+
background: yellow;
143+
}
144+
145+
.highlight-row .public_fixedDataTableCell_main {
146+
background: lightyellow;
147+
}

site/examples/ExamplesPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var EXAMPLE_COMPONENTS = {
3838
[ExamplePages.OBJECT_DATA_EXAMPLE.location]: require('../../examples/ObjectDataExample'),
3939
[ExamplePages.RESIZE_EXAMPLE.location]: require('../../examples/ResizeExample'),
4040
[ExamplePages.REORDER_EXAMPLE.location]: require('../../examples/ReorderExample'),
41+
[ExamplePages.SCROLL_TO_EXAMPLE.location]: require('../../examples/ScrollToExample'),
4142
[ExamplePages.FLEXGROW_EXAMPLE.location]: require('../../examples/FlexGrowExample'),
4243
[ExamplePages.COLUMN_GROUPS_EXAMPLE.location]: require('../../examples/ColumnGroupsExample'),
4344
[ExamplePages.FILTER_EXAMPLE.location]: require('../../examples/FilterExample'),

0 commit comments

Comments
 (0)