Now, create a new script file under the "Scripts" folder, name it "custom-datatable.js" and place the following code in it.
$(document).ready(function()
{
$('#TableId').DataTable
({
"columnDefs": [
{
"width": "5%",
"targets": [0]
},
{
"className": "text-center custom-middle-align",
"targets": [0, 1, 2, 3, 4, 5, 6]
}, ],
"language":
{
"processing": "<div class='overlay custom-loader-background'><i class='fa fa-cog fa-spin custom-loader-color'></i></div>"
},
"processing": true,
"serverSide": true,
"ajax":
{
"url": "/Plugin/GetData",
"type": "POST",
"dataType": "JSON"
},
"columns": [
{
"data": "Sr"
},
{
"data": "OrderTrackNumber"
},
{
"data": "Quantity"
},
{
"data": "ProductName"
},
{
"data": "SpecialOffer"
},
{
"data": "UnitPrice"
},
{
"data": "UnitPriceDiscount"
}]
});
});
Now, this is the fun part which will display the server-side data in the table that we have created earlier into our partial view "_ViewListPartial.cshtml." This is how the Datatables plugin integrates server-side data with the underlying web programming language. Let’s see each piece of information here chunk by chunk.
"columnDefs": [
{ "width": "5%", "targets": [0] },
{ "className": "text-center custom-middle-align", "targets": [0, 1, 2, 3, 4, 5, 6] },
],
This chunk of code provides styling, and enables/disables information for sorting, searching, etc, for the number of columns that are being used in the table, which is why this chunk of code defines columns definition for our table.
"language": {
"processing": "<div class='overlay custom-loader-background'><i class='fa fa-cog fa-spin custom-loader-color'></i></div>"
},
This chunk of code allows us to customize the processing message that will appear when data is being loaded. I have used the following custom styling here.
.custom-loader-color {
color: #fff!important;
font-size: 50px!important;
}
.custom-loader-background {
background-color: crimson!important;
}
.custom-middle-align {
vertical-align: middle!important;
}
Below is a snippet of what the processing loader will look like:
Below piece of code below will enable the data loading from the server side.
"processing": true,
"serverSide": true,
"ajax": {
"url": "/Plugin/GetData",
"type": "POST",
"dataType": "JSON"
},
"columns": [
{
"data": "Sr"
},
{
"data": "OrderTrackNumber"
},
{
"data": "Quantity"
},
{
"data": "ProductName"
},
{
"data": "SpecialOffer"
},
{
"data": "UnitPrice"
},
{
"data": "UnitPriceDiscount"
}
]
The columns here are the exact names of the properties that we have created in the "SalesOrderDetail.cs" file and the path "/Plugin/GetData" is the function that will be returning data from the server side.