Why choose Grid.js? 🤨
What makes it better than others is the fact that it is:
Ridiculously easy to start with! I mean, I made the following demo in a matter of a few minutes. It already comes with some really useful tools which can be easily plugged-in to an existing app. So, you don't need to worry about actually learn another new library.
Use it with or without any framework: It has only one external dependency already baked in. Hence, you don't have to worry about managing dependencies if you don't use a package manager like NPM. Use it with Angular, React, Vue, or with just vanilla JS!
React Native support (UPCOMING): It is designed to be independent of the web browser context, and the library developer has stated that support for RN is coming!
Fast! Grid.js has an internal pipeline that takes care of caching and processing data. This helps in loading, searching, sorting, or displaying the data really quickly.
Powerful CSS integration: You can style your tables whichever way you want. It doesn't matter if you're using plain old CSS in a separate stylesheet or inside JS 😏.
What will we make using Grid.js? 😀
As you see, we'll cover the following features:
Adding the "Loading..." state.
Displaying the data.
Sorting each or multiple rows.
Searching the contents.
Using custom CSS styles.
Let's make this powerful table in a matter of minutes! ⚡
Step 1: Grab the CDN
For this demo, we'll be installing Grid.js using the quick grab-the-cdn-and-go! method. Also, this is a vanilla JS project where we'll be using this library.
Start with the HTML. It's just 22 characters! (yes, I did count that number 🥴) as we're only displaying a `div` where our table will be placed.
BOOM! Onto next...
Step 3: Code the table
Instantiate the `gridjs` object in your JS file as:
This `gridjs` object holds all the configurations related to what we need in our table:
`data`: where we define the rows and columns of the table. In this demo, let's choose a sample data from mockaroo.com. But before we add the data, we need to tell Grid.js on which HTML element we need this data to display. Hence, we use `render()` method where we select the `table` element using the conventional `document.getElementById()` call.
Grid.js stores the data in the form of both `[key: string]` pairs as well as in usual array form `[string, string]`. Here we're using the latter approach. Just add the sample data as a string here. For us to explicitly define the title of columns, just add the next config...
`columns`: these are optional and are defined as `string` types. Therefore, we add `columns: ["Name", "Email", "Phone Number","Gender"]`.
`Promise` to resolve data asynchronously: this is useful for any external HTTP calls and loading data from a server. Also, if you add a `setTimeout()` function inside this async call, you will get the desired shimmer (loading...) effect, as seen in the demo. Here, I went for a 2-second delay so as to mock data fetching.
You should now have this data displayed on your webpage on a beautiful table. Time to add the rest of the features now!
Adding the searching and sorting capability is fairly easy. Just set the Boolean value to the following two configs to `true`:
If the data is large, we definitely need some pagination. For that, Grid.js provides us with the `pagination` object where we can set the `limit` of how many data rows to display at once. The cool thing is it automatically add the pagination controls for us!
To further control the default height of your table, set the [`height`](https://gridjs.io/docs/config/height/) config to the value in pixels. Ex: `height: '200px'`
All the messages you see on the table like the search placeholder with emojis, "Displaying 1 to 3 of 9 Records," etc., are possible due to the language support of the library. It provides us with the [`language`](https://gridjs.io/docs/config/language/) config where we can pass on which text data to change where.
Hence, if you want to change the placeholder text of the `'search'` box, simply add it as a String object to `language` and the same for the `'pagination'`. Here's our `language` object now: