Skip to content

Commit a4a1caf

Browse files
committed
pagination now only one functioin to handle page changes, still needs refining but now has less code.
1 parent 7548269 commit a4a1caf

5 files changed

Lines changed: 55 additions & 76 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- __Fixed__ for any bug fixes.
1212
- __Security__ in case of vulnerabilities.
1313

14+
## [0.1.1] - 16-06-2019
15+
###Changed
16+
- pagination now only one functioin to handle page changes, still needs refining but now has less code.
17+
1418
## [0.1.0] - 16-06-2019
1519
###Removed
1620
- infinite scroll

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "data-table",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"dependencies": {
66
"react": "^16.8.6",

src/Components/data-table.js

Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,26 @@ class RecordTable extends PureComponent {
8585
],
8686
data: [],
8787
checked: false,
88+
currentPage: 1,
89+
recordsPerPage: 30,
8890

8991
sortOrder: {
9092
key: "asc",
9193
},
92-
currentPage: 1,
93-
recordsPerPage: 15,
9494

9595
query: "",
9696
PlaceHolder: "Table filter",
9797
};
9898

99-
componentWillMount() {
99+
componentDidMount() {
100100
this.loadDataFromServer();
101101
}
102102
// Initial call to the server for records
103103
loadDataFromServer = () => {
104104
const { checked } = this.state;
105105

106106
const xmlhr = new XMLHttpRequest();
107-
const url = !checked ? `https://jsonplaceholder.typicode.com/comments` :
108-
`https://jsonplaceholder.typicode.com/users`
109-
;
107+
const url = !checked ? `https://jsonplaceholder.typicode.com/comments` : `https://jsonplaceholder.typicode.com/users`;
110108
this.setState({ isLoading: true });
111109

112110
xmlhr.open("GET", url, true);
@@ -133,18 +131,11 @@ class RecordTable extends PureComponent {
133131
const { data, sortOrder } = this.state;
134132
const tableData = data
135133

136-
let newData;
137-
if (key === "id") {
138-
newData = tableData.sort((a, b) => (sortOrder[key] === "asc" ?
139-
a[key] - b[key] :
140-
b[key] - a[key])
141-
);
142-
} else {
143-
newData = tableData.sort((a, b) => (sortOrder[key] === "asc" ?
144-
a[key].localeCompare(b[key]) :
145-
b[key].localeCompare(a[key]))
146-
);
147-
}
134+
const newData = tableData.sort((a, b) => (sortOrder[key] === "asc" ?
135+
a[key] > b[key] :
136+
b[key] > a[key])
137+
);
138+
148139
this.setState({
149140
data: newData,
150141
sortOrder: {
@@ -161,40 +152,6 @@ class RecordTable extends PureComponent {
161152
query: query,
162153
});
163154
}
164-
//Pagination
165-
handlePageChange = (event) => {
166-
const currentPage = Number(event.target.id);
167-
this.setState({
168-
currentPage: currentPage,
169-
});
170-
}
171-
// Logic for pagination next page (pages go forward 1 at a time)
172-
handleIncrement = () => {
173-
const { currentPage } = this.state;
174-
this.setState({
175-
currentPage: currentPage + 1,
176-
});
177-
}
178-
// Logic for pagination previous page (pages go back 1 at a time)
179-
handleDecrement = () => {
180-
const { currentPage } = this.state;
181-
this.setState({
182-
currentPage: currentPage - 1,
183-
});
184-
}
185-
// Logic for pagination first page
186-
handleFirst = () => {
187-
this.setState({
188-
currentPage: 1,
189-
});
190-
}
191-
// Logic for pagination last page
192-
handleLast = () => {
193-
const { recordsPerPage, data } = this.state;
194-
this.setState({
195-
currentPage: Math.ceil(data.length / recordsPerPage),
196-
});
197-
}
198155
handleCheckBox = () => {
199156
const { checked } = this.state;
200157
this.setState({
@@ -204,8 +161,36 @@ class RecordTable extends PureComponent {
204161
});
205162
this.loadDataFromServer();
206163
}
164+
//Pagination
165+
handlePageChange = (event) => {
166+
const currentPage = Number(event.target.id);
167+
const type = event.target.textContent;
168+
console.log("TCL: RecordTable -> handlePageChange -> type", type)
169+
170+
this.setState({
171+
currentPage: currentPage,
172+
});
173+
174+
if (type === "Next") {
175+
this.setState({
176+
currentPage: this.state.currentPage + 1
177+
});
178+
} else if (type === "Previous") {
179+
this.setState({
180+
currentPage: this.state.currentPage - 1
181+
});
182+
} else if (type === "〉〉") { //Last
183+
this.setState({
184+
currentPage: Math.ceil(this.state.data.length / this.state.recordsPerPage)
185+
});
186+
} else if (type === "〈〈") { //First
187+
this.setState({
188+
currentPage: 1
189+
});
190+
}
191+
}
207192
render() {
208-
const { error, hasMore, isLoading, data, columns, query, PlaceHolder,
193+
const { error, isLoading, data, columns, query, PlaceHolder,
209194
currentPage, recordsPerPage, checked
210195
} = this.state;
211196

@@ -270,18 +255,9 @@ class RecordTable extends PureComponent {
270255
})
271256
}
272257
</table>
273-
<Pagination
274-
data={data}
275-
recordsPerPage={recordsPerPage}
276-
currentPage={currentPage}
277-
handlePageChange={this.handlePageChange}
278-
handleDecrement={this.handleDecrement}
279-
handleFirst={this.handleFirst}
280-
handleLast={this.handleLast}
281-
handleIncrement={this.handleIncrement}
282-
/>
258+
<Pagination data={data} currentPage={currentPage} recordsPerPage={recordsPerPage} handlePageChange={this.handlePageChange} />
283259
{isLoading && <Preloader />}
284-
{!hasMore && <ScrollButton />}
260+
<ScrollButton />
285261
</div>
286262
)
287263
}

src/Components/pagination.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { PureComponent } from "react";
22

33
class Pagination extends PureComponent {
4+
handlePageChange = (event) => {
5+
this.props.handlePageChange(event)
6+
}
47
render() {
5-
const { data, currentPage, recordsPerPage, handlePageChange,
6-
handleDecrement, handleFirst, handleLast, handleIncrement
7-
} = this.props;
8+
const { currentPage, recordsPerPage } = this.props;
89

9-
// pagination, mapping and logic.
10-
const totalRecords = Math.ceil(data.length / recordsPerPage);
10+
const totalRecords = Math.ceil(this.props.data.length / recordsPerPage);
1111
const pageNumbers = [];
1212
for (let i = 1; i <= totalRecords; i++) {
1313
pageNumbers.push(i);
@@ -18,7 +18,7 @@ class Pagination extends PureComponent {
1818
<li
1919
key={number}
2020
id={number}
21-
onClick={handlePageChange}
21+
onClick={this.handlePageChange}
2222
className={currentPage === number ? 'active' : ''}
2323
>
2424
{number}
@@ -36,23 +36,23 @@ class Pagination extends PureComponent {
3636

3737
const previousPage = currentPage === startPage ?
3838
null :
39-
<li key="Previous" onClick={handleDecrement}>Previous</li>;
39+
<li title="previous" onClick={this.handlePageChange}>Previous</li>;
4040

4141
const firstPage = currentPage === startPage ?
4242
null :
43-
<li key="first" onClick={handleFirst}>&lang;&lang;</li>;
43+
<li title="first" onClick={this.handlePageChange}>&lang;&lang;</li>;
4444

4545
const lastSepPage = currentPage !== endPage ?
4646
<li className="disabled">...</li> :
4747
null;
4848

4949
const nextPage = currentPage === endPage ?
5050
null :
51-
<li key="Next" onClick={handleIncrement}>Next</li>;
51+
<li title="next" onClick={this.handlePageChange}>Next</li>;
5252

5353
const lastPage = currentPage === endPage ?
5454
null :
55-
<li key="last" onClick={handleLast}>&rang;&rang;</li>;
55+
<li title="last" onClick={this.handlePageChange}>&rang;&rang;</li>;
5656

5757
return (
5858
<div className="pagination-wrapper">

src/Components/table-rows.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class TableRows extends PureComponent {
2323
render() {
2424
const { expand } = this.state;
2525
const { columns, data, checked } = this.props;
26-
console.log("TCL: TableRows -> render -> data", data)
2726

2827
let expandVal;
2928
if (!expand) {

0 commit comments

Comments
 (0)