jQuery DataTables Filter: Server-Side Filtering with Custom Filters
Raheel Shan
May 9th, 2025 · 8 min read
To filter a jQuery DataTable on the server, put your filter inputs outside the table, read their values inside the ajax.data function so they are sent with every request, and call table.ajax.reload() when the user clicks Search. DataTables then asks the server for page one of the filtered rows. This post shows the full setup: the HTML, the JavaScript, and the server response DataTables expects.
Quick answer
- Put filter inputs (keyword, selects) above the table, outside the DataTables markup.
- Turn on
serverSideand giveajaxa URL and adatafunction that copies only the filled-in filters onto the request. - Keep the table in a variable and call
table.ajax.reload()from the Search button. - On the server, apply each filter only if it was sent, and return
draw,recordsTotal,recordsFilteredanddata.
The scenario
We list products with pagination, and above the table sit three filters: a keyword, a category and a brand. The table shows all products on load. The user picks filters, clicks Search, and the table reloads with the matching rows.
The HTML
The table has one header cell per column that DataTables will draw. Here that is four: name, category, brand and actions.
<table id="dataTable">
<thead>
<tr>
<th>Product Name</th>
<th>Category</th>
<th>Brand</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
The filters
Plain inputs, nothing DataTables-specific. The value 0 means "All".
<div class="row">
<div class="col-md-3">
<label for="keyword">Search by name, SKU or keyword</label>
<input type="text" id="keyword" class="form-control">
</div>
<div class="col-md-3">
<label for="category_id">Category</label>
<select id="category_id" class="form-control">
<option value="0">All</option>
<option value="1">Electronics</option>
<option value="2">Furniture</option>
<option value="3">Office Supplies</option>
</select>
</div>
<div class="col-md-3">
<label for="brand_id">Brand</label>
<select id="brand_id" class="form-control">
<option value="0">All</option>
<option value="1">Lenovo</option>
<option value="2">HP</option>
<option value="3">Lorell</option>
</select>
</div>
<div class="col-md-3">
<button type="button" id="search">Search</button>
<button type="button" id="clear">Clear</button>
</div>
</div>
Step 1: Send filters with ajax.data
The ajax.data option can be a function. DataTables runs it before every request and passes the object it is about to send. Add your filters to that object and the server receives them next to DataTables' own parameters (draw, start, length). If the function returns nothing, DataTables sends the object you changed.
var table = $('#dataTable').DataTable({
processing: true,
serverSide: true,
searching: false, // we have our own keyword box
ordering: false, // the server decides the order
pageLength: 10,
lengthMenu: [10, 50, 100],
ajax: {
url: 'api/products',
type: 'POST',
data: function (d) {
var category = $('#category_id').val();
var brand = $('#brand_id').val();
var keyword = $.trim($('#keyword').val());
if (category > 0) d.category_id = category;
if (brand > 0) d.brand_id = brand;
if (keyword !== '') d.keyword = keyword;
}
},
columns: [
{ data: 'name' },
{ data: 'category' },
{ data: 'brand' },
{
data: 'id',
render: function (id) {
return '<a class="btn btn-info" href="/products/' + id + '/edit">Edit</a>';
}
}
]
});
Step 2: Reload when the user searches
Because table holds the DataTable instance, the buttons can reload it. ajax.reload() repeats the request, runs the data function again so the current filter values are picked up, and goes back to the first page.
$('#search').on('click', function () {
table.ajax.reload();
});
$('#clear').on('click', function () {
$('#category_id').val(0);
$('#brand_id').val(0);
$('#keyword').val('');
table.ajax.reload();
});
You can also reload on change for the selects if you want instant filtering. Keep the keyword behind the Search button, or debounce it, so you do not fire a request on every key press.
Step 3: Return what DataTables expects
In server-side mode DataTables does not filter or page anything itself. It sends start and length, and the response must contain four fields: draw (the request's draw counter, cast to an integer), recordsTotal (rows before filtering), recordsFiltered (rows after filtering) and data.
// routes/api.php
Route::post('products', [ProductController::class, 'index']);
// ProductController
public function index(Request $request)
{
$query = Product::query();
if ($request->filled('category_id')) {
$query->where('category_id', $request->category_id);
}
if ($request->filled('brand_id')) {
$query->where('brand_id', $request->brand_id);
}
if ($request->filled('keyword')) {
$query->where('name', 'like', '%'.$request->keyword.'%');
}
$filtered = (clone $query)->count();
$rows = $query->with(['category', 'brand'])
->latest('id')
->skip((int) $request->start)
->take((int) $request->length)
->get()
->map(fn ($p) => [
'id' => $p->id,
'name' => $p->name,
'category' => $p->category?->name,
'brand' => $p->brand?->name,
]);
return response()->json([
'draw' => (int) $request->draw,
'recordsTotal' => Product::count(),
'recordsFiltered' => $filtered,
'data' => $rows,
]);
}
Why only send filters that have a value
Sending category_id=0 forces the server to know that 0 means "no filter". Leaving the parameter out says the same thing with no convention to remember, and each if on the server stays independent. With three optional filters there are eight combinations, and none of them needs its own code. This is the idea behind composing clean API queries with jQuery DataTables and Atomic Query Construction. If you want to move the query out of the controller into one reusable class, start with the Atomic Query Construction design pattern.
Bonus: use extra data from the response
The JSON can carry more than DataTables needs, such as a summary of the filtered rows. Pass a callback to ajax.reload() and it receives the server's JSON once the table is redrawn.
function showSummary(json) {
console.log(json.summary);
}
table.ajax.reload(showSummary);
Limits and when not to use this
- A few hundred rows. If the whole list fits in the page, drop
serverSideand let DataTables filter in the browser. It is simpler. - Several filters that change often. Each reload is a database query. Index the columns you filter on, and cap
lengthon the server so nobody asks for 100000 rows. - A table that must stay empty until the user searches. DataTables has a
deferLoadingoption to skip the first request, but it expects the first page to already be in the HTML. Check the docs before using it for an empty start.
FAQ
How do I filter a jQuery DataTable with custom inputs? Read the inputs inside the ajax.data function and add their values to the request, then call table.ajax.reload() when the user applies the filter.
How does DataTables server-side filtering work? With serverSide: true, DataTables sends draw, start and length to your URL on each page, sort or search. The server filters, pages and returns recordsTotal, recordsFiltered and data.
How do I filter a table in jQuery without DataTables? Read the input value and hide rows that do not match, using $('#table tbody tr').filter(...).toggle(...). That works for small tables only; for large data use server-side filtering.
How do I reload a DataTable after changing a filter? Call table.ajax.reload(). Pass false as the second argument if you want to stay on the current page instead of returning to page one.
Do I need to send the filter values as POST? No. type: 'GET' works too. POST avoids long query strings when you have many filters.
Get a second pair of eyes on your code
We open your repository together. I review structure, queries, security and naming.
You leave with fixes you can apply the same day.
Found this useful?
Support the work that goes into these posts.