Introduction
In this session, we will learn how to implement server-side processing with custom range filtering in jQuery DataTables. I am going to show you a DataTable's server-side paging, sorting, and filtering in ASP.NET MVC application. Server-side means using C# code behind the file in the Controller section. We can erase the global search box of jQuery DataTable and use our custom filter area with input fields like textbox and dropdown for implementing custom multicolumn server-side filtering in jQuery DataTables. By using that way, we can implement our own filter areas anywhere on our page, to work with jQuery DataTables.
Description
DataTable is a most powerful and easy-to-use jQuery plugin for displaying tabular data with features like pagination, searching, state saving, and multi-column sorting with data type detection and lots more, with ZERO or minimal configuration.
The prerequisites of this article include knowledge of the following technologies.
- ASP.NET MVC
- HTML
- JavaScript
- AJAX
- CSS
- Bootstrap
- C# Programming
- C# LINQ
- jQuery
Note. Before going through the session, I suggest you first visit my previous articles with a back-end section.
- Retrieve Records In jQuery Datatable Using Entity Framework And Bootstrap
- Performance Issue In jQuery DataTable About Fetching Records And Steps To Fix It
Steps to be followed
Step 1. Add a new action into the Controller to get the View where we will implement the jQuery DataTable with server-side paging and sorting.
Code
public ActionResult Filter()
{
return View();
}
Step 2. Add a View for the action (here "filter") and design.
Code
@{
ViewBag.Title = "Satyaprakash - Filter Records";
}
<h2 style="color: blue">Satyaprakash-Server Side Processing With Custom Range Filtering</h2>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
.custom-loader-color {
color: #fff !important;
font-size: 40px !important;
}
.custom-loader-background {
background-color: #f60 !important;
}
.custom-middle-align {
vertical-align: middle !important;
}
</style>
<div style="width:90%; margin:0 auto">
<div style="background-color:#f5f5f5; padding:20px">
<h2 style="color: blue">Filter Records</h2>
<table>
<tbody>
<tr>
<td style="color: blue">City</td>
<td><input type="text" class="form-control" id="txtCity" /></td>
<td style="color: blue">State</td>
<td>
<select id="ddState" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
<option value="">All States</option>
<option value="Karnataka">Karnataka</option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Georgia">Georgia</option>
<option value="Uttar Pradesh">Uttar Pradesh</option>
<option value="Odisha">Odisha</option>
</select>
</td>
<td>
<input type="button" class="btn btn-success btn-md" value="Filter" id="btnFilter" />
</td>
</tr>
</tbody>
</table>
</div>
@* jQuery DataTables *@
<div style="width:90%; margin:0 auto;">
<table id="myTable" class="table table-responsive table-striped">
<thead>
<tr>
<th style="background-color: Yellow;color: blue">First Name</th>
<th style="background-color: Yellow;color: blue">Last Name</th>
<th style="background-color: Yellow;color: blue">Age</th>
<th style="background-color: Yellow;color: blue">Address</th>
<th style="background-color: Yellow;color: blue">City</th>
<th style="background-color: Yellow;color: blue">State</th>
</tr>
</thead>
</table>
</div>
</div>
@* Load bootstrap datatable css *@
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<link href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
@* Load bootstrap datatable js and initialize DataTable *@
@section Scripts {
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
<script>
$(document).ready(function () {
// jQuery DataTables initialization
$('#myTable').DataTable({
"language": {
"processing": "<div class='overlay custom-loader-background'><i class='fa fa-cog fa-spin custom-loader-color'></i></div>"
},
"processing": true,
"serverSide": true,
"orderMulti": false,
"dom": '<"top"i>rt<"bottom"lp><"clear">',
"ajax": {
"url": "/home/FilterData",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "FirstName", "name": "FirstName", "autoWidth": true },
{ "data": "LastName", "name": "LastName", "autoWidth": true },
{ "data": "Age", "name": "Age", "autoWidth": true },
{ "data": "Address", "name": "Address", "autoWidth": true },
{ "data": "City", "name": "City", "autoWidth": true },
{ "data": "State", "name": "State", "autoWidth": true }
]
});
// DataTables filtering on button click
var oTable = $('#myTable').DataTable();
$('#btnFilter').click(function () {
oTable.columns(4).search($('#txtCity').val().trim());
oTable.columns(5).search($('#ddState').val().trim());
oTable.draw();
});
});
</script>
}




Mohd Syafiq)Posted Jan 27, 2022, 4:26 AM
Anyone has source code for this?
Jakub KratzerPosted Jun 1, 2021, 11:59 AM
I don't know why but i never get value from Request.Form.GetValues("columns[4][search][value]").FirstOrDefault(); They are always empty.. Only one diference between our codes is that I have not filters in table element..
neha khandelwalPosted Apr 27, 2020, 10:58 AM
I want this search for each colom but i am not able to get data using colom[i][search][value] in code behind
Peter ObikePosted Oct 8, 2019, 6:12 AM
Please where did you set your data into datatable
Arvind SinghPosted Jan 27, 2019, 10:33 PM
Nice article....
Hamid KhanPosted Jan 26, 2019, 2:55 PM
Good explanation.............Thanks