Data tables are frequently asked during front end interviews as displaying paginated data with filtering functionality is a UI built at almost every company.
Since the table skeleton has been provided, we can focus on the state and data manipulation aspects of the data table.
State is straightforward. Only two state values are needed, the current page and the page size. Since the data does not change in this case, there's no need for the users data to be part of state.
These state values are manipulated by the page size <select>
and prev/next buttons.
The maximum number of pages can be derived from the number of users divided by the page size, hence it does not need to be part of state.
We implement a function paginateUsers
that takes in the the list of users, the page number, and the page size. It will return the list of users for the current page and the total number of pages.
To determine the list of users for the current page, we can determine the start and end indices, then use Array.prototype.slice()
to extract the appropriate slice out of the users list:
Array.prototype.slice()
, it doesn't matter if the end index exceeds the size of the list.paginateUsers()
will be called in the render path and the returned users pageUsers
is the current page of users to be rendered. The rendering code doesn't need to be changed much.
Some other user experience improvements we can make:
Given a list of users, build a users data table that displays users in a paginated format.
Data tables are frequently asked during front end interviews as displaying paginated data with filtering functionality is a UI built at almost every company.
Since the table skeleton has been provided, we can focus on the state and data manipulation aspects of the data table.
State is straightforward. Only two state values are needed, the current page and the page size. Since the data does not change in this case, there's no need for the users data to be part of state.
These state values are manipulated by the page size <select>
and prev/next buttons.
The maximum number of pages can be derived from the number of users divided by the page size, hence it does not need to be part of state.
We implement a function paginateUsers
that takes in the the list of users, the page number, and the page size. It will return the list of users for the current page and the total number of pages.
To determine the list of users for the current page, we can determine the start and end indices, then use Array.prototype.slice()
to extract the appropriate slice out of the users list:
Array.prototype.slice()
, it doesn't matter if the end index exceeds the size of the list.paginateUsers()
will be called in the render path and the returned users pageUsers
is the current page of users to be rendered. The rendering code doesn't need to be changed much.
Some other user experience improvements we can make:
console.log()
语句将显示在此处。