@@ -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 }
0 commit comments