DataTables
Last updated
Last updated
DataTables is a plug-in for the jQuery library that provides additional features related to HTML tables. It provides a standardized layout that can be customized and interactive features in an easy-to-implement way.
A typical data table in our applications looks like this:
To be able to use the DataTables plug-in, you need to install the DataTables CSS file and the DataTables JavaScript file. This can be done in different ways. For our projects, we choose to install the files locally. If not already done yet, you can do it by following these steps:
Open the following link: https://datatables.net/download/
Check "DataTables" in both Step 1 and Step 2
In Step 3, click the "Download" tab and check "Minify" and "Concatenate"
Click the Button "Download files"
Store the downloaded files in the folder "Plugins" > "datatable" (Create the file if it does not exist yet)
Add the following two lines of code in the "Layout.cshtml" file of the project:
The content of the above table (without any features from DataTables) is coded like this:
To include the DataTables features, the id of the table is addressed in the following way:
The different features can be included within the "{ ... }" brackets. In the following, a couple of features are described.
The search bar feature can be turned off and on by the following code:
By default, it is true, so if not otherwise specified to false, there will always be a search bar in the table.
To order the content of the table according to one of the columns, you use the following code:
"3" in this example refers to the third column counting from left (where the first column is counted as "0"). So "3" here refers to the 4th column.
"asc" specifies that it should be in ascending order. "desc" would denote a descending order.
It is also possible to add the functionality of changing the order interactively by clicking on a column. The order will change to the order of the clicked column. By clicking on the same column, it will change the direction of the order, toggling between ascending and descending.
This feature is realized by specifying which columns should be "sortable". Sortable columns have an arrow icon on their head. By default, all columns are sortable. To specify which columns should not be sortable, add the following code:
"bSortable": false specifies that the following will be referring to the attribute of being not sortable.
The numbers after "aTargets" refer to the columns that should be affected by the previously specified attribute, so in this case, they are the columns that should not be sortable. This means, in this example, only the 3rd (denoted by "2") and the 4th (denoted by "3") column are sortable.
All the provided features and their implementation can be found in the DataTables website: https://datatables.net/