🥳
WindFrame Blocks is here
New Contrast Pro UI Kit and template with 5 admin dashboards, 23 pages and 10000+ components for $49 ($50 off)🎉
postImage

How to create a beautiful datatable with icons using bootstrap and Contrast on React.

blogImage

By Emmanuel Chinonso

Web Developer

How to create a datatable in react using contrast

DataTables are components that mix tables with advanced options like searching, sorting and pagination. Today, we’ll be creating a DataTables in react using a react library knows as Contrast. Contrast also known as CDBReact is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites, and web apps.

Prerequisites

The DataTables would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:

  • JavaScript Knowledge
  • Basic React Knowledge
  • Basic Bootstrap Knowledge
  • JavaScript Knowledge
  • NPM installed

The DataTable we will be building is show below Datatable

Setup

First Check that you have node installed. To do this, run the following command in your terminal. node -v

This should show you the current version of node you have installed on your machine. If you don’t have node.js installed, download it here. Installing node also installs npm on your PC but you can still confirm using npm-v. Now that we have node installed, we can start up our React project by going to the directory of our choice and running npx create-react-app datatables-app

I named the project datatables-app but you can use whatever name of your choice.

Install CDBReact

Now, we have to install CDBReact in our project Run the following command to install CBDReact

Code: npm install --save ./path-to-the-cdbreact-pro-tgz-file

Or using Yarn

Code: yarn add ./path-to-the-cdbreact-pro-tgz-file

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation. Now run npm start to make sure that everything works well and there are no errors. DataTables The first thing we will do at this stage is to create a file that would house our datatable main codes. I named the file DataTable.js. you can of course name it anything you wish. In the file import some datatable component from contrast and react from react.

Code:

1import React from "react";
2import {
3 CDBCard, CDBCardBody, CDBDataTable, CDBRow, CDBCol, CDBContainer
4 } from "cdbreact-pro";

In the code above, we imported some component from contrast that is important in building the dataTable. These are the conmponents

  • CDBCard which is used to build a card structure for the table
  • CDBCardBody which is used to create the body of the card which holds the table.
  • CDBDataTable this component holds the date, the row and column
  • CDBRow this component is used to create the row of the data table
  • CDBCol this component is used to build the column of the datatable
  • CDBContainer this component houses the entire code for the dataTable.

Code:

1const DataTable = () => {
2
3 function testClickEvent(param) {
4 alert("Row Click Event");
5 }
6
7 const data = () => {
8 return ({
9 columns: [
10 {
11 label: "Name",
12 field: "name",
13 width: 150,
14 attributes: {
15 "aria-controls": "DataTable",
16 "aria-label": "Name",
17 },
18 },
19 {
20 label: "Position",
21 field: "position",
22 width: 270,
23 },
24 {
25 label: "Office",
26 field: "office",
27 width: 200,
28 },
29 {
30 label: "Age",
31 field: "age",
32 sort: "asc",
33 width: 100,
34 },
35 {
36 label: "Start date",
37 field: "date",
38 sort: "disabled",
39 width: 150,
40 },
41 {
42 label: "Salary",
43 field: "salary",
44 sort: "disabled",
45 width: 100,
46 },
47 ],
48
49 rows: [
50 {
51 name: "Tiger Nixon",
52 position: "System Architect",
53 office: "Edinburgh",
54 age: "61",
55 date: "2011/04/25",
56 salary: "$320",
57 clickEvent: () => testClickEvent(1),
58 },
59 {
60 name: "Garrett Winters",
61 position: "Accountant",
62 office: "Tokyo",
63 age: "63",
64 date: "2011/07/25",
65 salary: "$170",
66 },
67 {
68 name: "Ashton Cox",
69 position: "Junior Technical Author",
70 office: "San Francisco",
71 age: "66",
72 date: "2009/01/12",
73 salary: "$86",
74 },
75 {
76 name: "Cedric Kelly",
77 position: "Senior Javascript Developer",
78 office: "Edinburgh",
79 age: "22",
80 date: "2012/03/29",
81 salary: "$433",
82 },
83 {
84 name: "Airi Satou",
85 position: "Accountant",
86 office: "Tokyo",
87 age: "33",
88 date: "2008/11/28",
89 salary: "$162",
90 },
91 {
92 name: "Brielle Williamson",
93 position: "Integration Specialist",
94 office: "New York",
95 age: "61",
96 date: "2012/12/02",
97 salary: "$372",
98 },
99 {
100 name: "Herrod Chandler",
101 position: "Sales Assistant",
102 office: "San Francisco",
103 age: "59",
104 date: "2012/08/06",
105 salary: "$137",
106 },
107 {
108 name: "Rhona Davidson",
109 position: "Integration Specialist",
110 office: "Tokyo",
111 age: "55",
112 date: "2010/10/14",
113 salary: "$327",
114 },
115 {
116 name: "Colleen Hurst",
117 position: "Javascript Developer",
118 office: "San Francisco",
119 age: "39",
120 date: "2009/09/15",
121 salary: "$205",
122 },
123 {
124 name: "Sonya Frost",
125 position: "Software Engineer",
126 office: "Edinburgh",
127 age: "23",
128 date: "2008/12/13",
129 salary: "$103",
130 },
131 {
132 name: "Jena Gaines",
133 position: "Office Manager",
134 office: "London",
135 age: "30",
136 date: "2008/12/19",
137 salary: "$90",
138 },
139 {
140 name: "Quinn Flynn",
141 position: "Support Lead",
142 office: "Edinburgh",
143 age: "22",
144 date: "2013/03/03",
145 salary: "$342",
146 },
147 {
148 name: "Charde Marshall",
149 position: "Regional Director",
150 office: "San Francisco",
151 age: "36",
152 date: "2008/10/16",
153 salary: "$470",
154 },
155 {
156 name: "Haley Kennedy",
157 position: "Senior Marketing Designer",
158 office: "London",
159 age: "43",
160 date: "2012/12/18",
161 salary: "$313",
162 },
163 {
164 name: "Tatyana Fitzpatrick",
165 position: "Regional Director",
166 office: "London",
167 age: "19",
168 date: "2010/03/17",
169 salary: "$385",
170 },
171 {
172 name: "Michael Silva",
173 position: "Marketing Designer",
174 office: "London",
175 age: "66",
176 date: "2012/11/27",
177 salary: "$198",
178 },
179 {
180 name: "Paul Byrd",
181 position: "Chief Financial Officer (CFO)",
182 office: "New York",
183 age: "64",
184 date: "2010/06/09",
185 salary: "$725",
186 },
187 {
188 name: "Gloria Little",
189 position: "Systems Administrator",
190 office: "New York",
191 age: "59",
192 date: "2009/04/10",
193 salary: "$237",
194 },
195 {
196 name: "Bradley Greer",
197 position: "Software Engineer",
198 office: "London",
199 age: "41",
200 date: "2012/10/13",
201 salary: "$132",
202 },
203 {
204 name: "Dai Rios",
205 position: "Personnel Lead",
206 office: "Edinburgh",
207 age: "35",
208 date: "2012/09/26",
209 salary: "$217",
210 },
211 {
212 name: "Jenette Caldwell",
213 position: "Development Lead",
214 office: "New York",
215 age: "30",
216 date: "2011/09/03",
217 salary: "$345",
218 },
219 {
220 name: "Yuri Berry",
221 position: "Chief Marketing Officer (CMO)",
222 office: "New York",
223 age: "40",
224 date: "2009/06/25",
225 salary: "$675",
226 },
227 {
228 name: "Caesar Vance",
229 position: "Pre-Sales Support",
230 office: "New York",
231 age: "21",
232 date: "2011/12/12",
233 salary: "$106",
234 },
235 {
236 name: "Doris Wilder",
237 position: "Sales Assistant",
238 office: "Sidney",
239 age: "23",
240 date: "2010/09/20",
241 salary: "$85",
242 },
243 {
244 name: "Angelica Ramos",
245 position: "Chief Executive Officer (CEO)",
246 office: "London",
247 age: "47",
248 date: "2009/10/09",
249 salary: "$1",
250 },
251 {
252 name: "Gavin Joyce",
253 position: "Developer",
254 office: "Edinburgh",
255 age: "42",
256 date: "2010/12/22",
257 salary: "$92",
258 },
259 {
260 name: "Jennifer Chang",
261 position: "Regional Director",
262 office: "Singapore",
263 age: "28",
264 date: "2010/11/14",
265 salary: "$357",
266 },
267 {
268 name: "Brenden Wagner",
269 position: "Software Engineer",
270 office: "San Francisco",
271 age: "28",
272 date: "2011/06/07",
273 salary: "$206",
274 },
275 {
276 name: "Fiona Green",
277 position: "Chief Operating Officer (COO)",
278 office: "San Francisco",
279 age: "48",
280 date: "2010/03/11",
281 salary: "$850",
282 },
283 {
284 name: "Shou Itou",
285 position: "Regional Marketing",
286 office: "Tokyo",
287 age: "20",
288 date: "2011/08/14",
289 salary: "$163",
290 },
291 {
292 name: "Michelle House",
293 position: "Integration Specialist",
294 office: "Sidney",
295 age: "37",
296 date: "2011/06/02",
297 salary: "$95",
298 },
299 {
300 name: "Suki Burks",
301 position: "Developer",
302 office: "London",
303 age: "53",
304 date: "2009/10/22",
305 salary: "$114",
306 },
307 {
308 name: "Prescott Bartlett",
309 position: "Technical Author",
310 office: "London",
311 age: "27",
312 date: "2011/05/07",
313 salary: "$145",
314 },
315
316 {
317 name: "Donna Snider",
318 position: "Customer Support",
319 office: "New York",
320 age: "27",
321 date: "2011/01/25",
322 salary: "$112",
323 },
324 ],
325 });
326 };
327
328 return (
329 <CDBContainer>
330 <CDBCard>
331 <CDBCardBody>
332 <CDBDataTable
333 striped
334 bordered
335 hover
336 entriesOptions={[5, 20, 25]}
337 entries={5}
338 pagesAmount={4}
339 data={data()}
340 materialSearch={true}
341 />
342 </CDBCardBody>
343 </CDBCard>
344 </CDBContainer>
345 );
346};
347export default DataTable;

We used the different components from contrast to create the following table above. We also added some styling to the components to help make the table more presentable. In the code below, we are going to render the DataTable component on the app.js

Code:

1import './App.css';
2import DataTable from './DataTable';
3import Reactdom from "react-dom";
4
5function App() {
6 return (
7 <div className="App">
8 <DataTable />
9 </div>
10 );
11}
12
13export default App;

Our page should look like the image below: Datatable

Conclusion Datatable are easy to build using contrast which you have seen. You can also use several tools including bootstrap styling without installing bootstrap to create your datatable and make it to suit you. You can also check out some other features you can use in CDBReact DataTable Docs.

Resources

CDBReact DataTable Docs

Link to code on github

Get Contrast Pro

Create Stunning websites and web apps

Building different custom components in react for your web apps or websites can get very stressful. Thats why we decided to build contrast. We have put together a UI kit with over 10000+ components, 5 admin dashboards and 23 additional different pages template for building almost any type of web app or webpage into a single product called Contrast Pro.Try contrast pro

ad-banner

Contrast React Bootstrap PRO is a Multipurpose pro template, UI kit to build your next landing, admin, SAAS, prelaunch, etc project with a clean, well documented, well crafted template and UI components.Learn more about Contrast

Share Post:

Comments

...