Introduction
Jquery Datatables free plugin offers many useful features to
integrate and customize a table with many web development
platforms. Today, I shall be
demonstrating the dynamic number of columns loading using server-side
Ajax Jquery Datatables plugin with ASP.NET MVC5 platform.
Prerequisites
The following are some prerequisites before you proceed any further in this tutorial:
- Installation of Jquery Datatebles plugin.
- Understanding Jquery Datatebles plugin Integration with ASP.NET MVC5
- Installation of CSVLibraryAK.
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of JavaScript.
- Knowledge of Bootstrap.
- Knowledge of Jquery.
- Knowledge of C# Programming.
The example code is
being developed in Microsoft Visual Studio 2017 Professional. I have taken the data sample from AdventureWorks for SQL Server 2014 and Iris data-set from UCI Machine Learning Repository.
Let's begin now.
Step 1
Create a new MVC web project and name it "MVCAjaxDatatablesDynamicCol".
Step 2
Now, create a controller file and name it "Controllers\HomeController.cs"
and write a simple POST Index(...) method which will uploaded the
provided CSV file to the server so that Jquery Datatables server-side
can process the uploaded file and load th data.
Know that jquery data tables support the following JSON format, i.e.:
- [{
- "columns": [{
- "title": "col1_name",
- "data": "col1_name"
- }, {
- "title": "col2_name",
- "data": "col2_name"
- }],
- "data": [{
- "col1_name": "col1 data",
- "col2_name": "col2 data"
- }, {
- "col1_name": "col1 data",
- "col2_name": "col2 data"
- }]
- }]
In "Controllers\HomeController.cs" file for server-side, Jquery Datatables is needed to work for a dynamic number of columns. We need to write necessary Ajax method i.e.
- ...
- public ActionResult GetData()
- {
- // Initialization.
- JsonResult result = new JsonResult();
- try
- {
- // Initialization Jquery Datatables.
- string search = Request.Form.GetValues("search[value]")[0];
- string draw = Request.Form.GetValues("draw")[0];
- string order = Request.Form.GetValues("order[0][column]")[0];
- string orderDir = Request.Form.GetValues("order[0][dir]")[0];
- int startRec = Convert.ToInt32(Request.Form.GetValues("start")[0]);
- int pageSize = Convert.ToInt32(Request.Form.GetValues("length")[0]);
- ...
- // Initialization.
- JqDataObj dataObj = new JqDataObj();
- ...
- // Load uploaded file Data.
- ...
- // Set Jquery Datatable Total record count.
- int totalRecords = data.Rows.Count;
- // Apply Jquery Datatables Search.
- ...
- // Apply Jquery Datatable Sorting.
- ...
- // Set Jquery Datatable Filter record count.
- int recFilter = data.Rows.Count;
- // Apply Jquery Datatable Pagination.
- ...
- // Set Jquery Datatable Dynamic Header Column Names.
- dataObj.columns = this.LoadColumnHeader(data);
- ...
- // Set Jquery Datatable Data Rows.
- dataObj.data = data;
- ...
- // Set Jquery Datatable resultant information.
- result = this.Json(new { draw = Convert.ToInt32(draw), recordsTotal = totalRecords, recordsFiltered = recFilter, data = JsonConvert.SerializeObject(dataObj) }, JsonRequestBehavior.AllowGet);
- }
- catch (Exception ex)
- {
- // Info
- Console.Write(ex);
- }
- // Return info.
- return result;
- }
- ...






Angelo MagnonePosted Sep 12, 2021, 10:50 PM
Where is the source code for this tutorial? I didn't find it on GitHub