Skip to content

Commit 8110e37

Browse files
author
Anthony Duhaime
committed
Added resolver if dataset is not to array format. Will try to get the first object if the dataset is not an array. Refactored some methods and variable's names
1 parent 8e6f5e3 commit 8110e37

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

demo/index.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,29 @@ import '../style.css';
55

66
class Example extends Component {
77
render() {
8-
let data = [
9-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
10-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
11-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
12-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
13-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
14-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
15-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
16-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
17-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
18-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
19-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
20-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
21-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
22-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
23-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
24-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
25-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
26-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
27-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
28-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
29-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
30-
{ id: 1, firstname: 'John', lastname: 'Doe', email: '[email protected]' },
31-
{ id: 2, firstname: 'Johnny', lastname: 'Doewy', email: '[email protected]' },
32-
{ id: 3, firstname: 'Johanne', lastname: 'Doewesse', email: '[email protected]' },
33-
];
8+
let data = [{ 'id': 1, 'firstname': 'Lari', 'lastname': 'Leach', 'email': '[email protected]' },
9+
{ 'id': 2, 'firstname': 'Teddie', 'lastname': 'Thorne', 'email': '[email protected]' },
10+
{ 'id': 3, 'firstname': 'Kendall', 'lastname': 'Ossenna', 'email': '[email protected]' },
11+
{ 'id': 4, 'firstname': 'Merv', 'lastname': 'Heffron', 'email': '[email protected]' },
12+
{ 'id': 5, 'firstname': 'Stanwood', 'lastname': 'Boundy', 'email': '[email protected]' },
13+
{ 'id': 6, 'firstname': 'Ikey', 'lastname': 'Laurenz', 'email': '[email protected]' },
14+
{ 'id': 7, 'firstname': 'Lilli', 'lastname': 'Peniello', 'email': '[email protected]' },
15+
{ 'id': 8, 'firstname': 'Anica', 'lastname': 'Brimham', 'email': '[email protected]' },
16+
{ 'id': 9, 'firstname': 'Frankie', 'lastname': 'Bremner', 'email': '[email protected]' },
17+
{ 'id': 10, 'firstname': 'Kristos', 'lastname': 'Verecker', 'email': '[email protected]' },
18+
{ 'id': 11, 'firstname': 'Olympe', 'lastname': 'Garred', 'email': '[email protected]' },
19+
{ 'id': 12, 'firstname': 'Ellissa', 'lastname': 'Derrett', 'email': '[email protected]' },
20+
{ 'id': 13, 'firstname': 'Aguistin', 'lastname': 'Oolahan', 'email': '[email protected]' },
21+
{ 'id': 14, 'firstname': 'Emili', 'lastname': 'Lunny', 'email': '[email protected]' },
22+
{ 'id': 15, 'firstname': 'Mikol', 'lastname': 'Scarrisbrick', 'email': '[email protected]' },
23+
{ 'id': 16, 'firstname': 'Boycey', 'lastname': 'Bockin', 'email': '[email protected]' },
24+
{ 'id': 17, 'firstname': 'Ezechiel', 'lastname': 'Pinchin', 'email': '[email protected]' },
25+
{ 'id': 18, 'firstname': 'Flore', 'lastname': 'Chesterfield', 'email': '[email protected]' },
26+
{ 'id': 19, 'firstname': 'Brnaby', 'lastname': 'Beentjes', 'email': '[email protected]' },
27+
{ 'id': 20, 'firstname': 'Ronny', 'lastname': 'Gelderd', 'email': '[email protected]' }];
3428
return (
3529
<div>
36-
<Tabledata datas={data}>
30+
<Tabledata datas={data} rowsPerPage={8}>
3731
<Tableheader attribute={'firstname'}>Firstname</Tableheader>
3832
<Tableheader attribute={'lastname'}>Lastname</Tableheader>
3933
<Tableheader renderCell={(_content, _index, row) => (row.firstname + ' ' + row.lastname)}>Fullname</Tableheader>

src/components/Tabledata.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const DEFAULT_ROWS_PER_PAGE = 10;
77
export default class Tabledata extends Component {
88
constructor(props) {
99
super(props);
10-
this._rowsToDisplay = this.props.rowsToDisplay ? this.props.rowsToDisplay : DEFAULT_ROWS_PER_PAGE;
10+
this._rowsToDisplay = this.props.rowsPerPage ? this.props.rowsPerPage : DEFAULT_ROWS_PER_PAGE;
11+
this._datas = (Array.isArray(this.props.datas)) ? this.props.datas : this.props.datas[Object.keys(this.props.datas)[0]];
1112
this.cells = [];
1213
this.setupCells();
1314
this.state = {
@@ -47,16 +48,16 @@ export default class Tabledata extends Component {
4748
}
4849

4950
requiresPagination() {
50-
return this.props.datas.length > this._rowsToDisplay;
51+
return this._datas.length > this._rowsToDisplay;
5152
}
5253

5354
pageCount() {
54-
return Math.ceil(this.props.datas.length / this._rowsToDisplay);
55+
return Math.ceil(this._datas.length / this._rowsToDisplay);
5556
}
5657

57-
getPaginatedFiles() {
58+
getPaginatedData() {
5859
const start = this.getPaginationStart();
59-
return Slice(this.props.datas, start, start + this._rowsToDisplay);
60+
return Slice(this._datas, start, start + this._rowsToDisplay);
6061
}
6162

6263
getPaginationStart() {
@@ -70,7 +71,7 @@ export default class Tabledata extends Component {
7071
}
7172

7273
renderRows() {
73-
return this.getPaginatedFiles().map((row, rowIndex) => {
74+
return this.getPaginatedData().map((row, rowIndex) => {
7475
let cells = this.prepareCells(row);
7576
if (this.props.renderRow) {
7677
return this.props.renderRow(cells, rowIndex);

0 commit comments

Comments
 (0)