Building controllable Table component in React

Tram Ho

Introduction

Today I would like to introduce a problem that I think you will encounter in real projects, or simply discuss how to analyze a problem, how to use react custom hooks, how to write unit tests for custom. hook, … and more ?

It is to build a controllable table with a react hook includes the following functions:

  • Search data according to all columns in the table
  • Sort data in ascending / descending order by column by clicking on the header
  • Pagination
  • Change the number of rows shown on each page

This topic is a bit too much, so I divided into 3 parts:

  • Part 1: Introduction and writing of pure javascript util functions ( search , sort , paging )
  • Part 2: Use the react hook to build a complete table with the util functions in Part 1
  • Part 3: Use jest to write Unit Test

After completing the above 3 sections, the result will be as follows:

Start part 1

Question:

Most of the admin apps for admin have a display data section that uses the table , in which the table uses some functions like searching for a certain row in the table, sort data ascending and descending by 1 column by Click on the header of that column, paginate, change the number of rows displayed on the table, …

And with the above problem, if implemented according to Single Page Application using React with the requirement that: when loading the page for the first time, the server will return 1000 (depending on the limit) records in the database, then the client side will search, sort, Paging relies on that list data without having to request it on the server again.

With such a request, the client will implement the Table component as follows:

  • Props to be passed to the Table: headers – list of data headers; dataSource – list of 1000 records data from server
  • The table component will render: input search, paging, button sort on column header, list of rows after performing search / sort / paging.

In this section, I just raised the problem for the problem and wrote the util functions related to processing search, sort, paging in javascript without using react .

Util functions in Table:

Assume the input data of the Table as follows:

Search

Description : After the user enters textbox search and press enter, he will search for items of list data match with text search. Thus the input of the search function consists of an array of objects and text search, which results in an array list of data after it has been searched.

Implement:

Sort

Description : If you click on the header of any column, sort by the value of that column, each click will toggle in ascending order (ASC) / decrease (DESC) and default is ascending. Thus, the input of the sort function includes an array of objects, the key (which column to sort by) and the sort order, the result returns the array list data after sorted.

Implement:

Paging

1. Get total page:

The function get total page is used to know when to enable / disable prev / next button in UI. Pass the length of the total array and the limit number of rows will display per page; The result is the total page number.

1. Get page items:

The get page items function is used to limit the number of items displayed on the table of the current page. Pass the array total, the current page number and the limit number of rows will display on each page; The result is the number of items to display of the current page.

Conclude

Those are all utils functions used to control the display data on the Table, let me finish Part 1 here, in the next part I will init react app and create a complete Table component based on the above utils functions.

Thank you for reading the article!

Share the news now

Source : Viblo