Im using Datatable for my current project. In one of my table I have several columns. One of the column is Status
column that will have one of three value Open
, Rejected
, and Approved
. I want to filter record that shown in table with three button, In Progress
and Closed
, like this:
<div class="btn-group pull-right">
<button id="onprogress" class="btn btn-default filter">On Process</button>
<button id="closed" class="btn btn-default filter">Closed</button>
<button id="all" class="btn btn-default filter">All</button>
</div>
Here is the javascript code that i used:
var dataTables = $('#datatable').DataTable({
"info": false,
"lengthChange": false
});
$('#all').on('click', function () {
dataTables.columns(4).search("").draw();
});
$('#onprogress').on('click', function () {
dataTables.columns(4).search("Open" ).draw();
});
$('#closed').on('click', function () {
dataTables.columns(4).search("Rejected","Approved").draw();
});
The javascript code work well for the #onprogreess button, because it only search for one value Open
. How to make it works with two value search?
(#closed button supposed to show record with Rejected
or Done
Status)
With the current version of DataTables you can do this using the 'search' function. var data_table = $('#data-table'). DataTable(); var column_index = 0; data_table. columns(column_index).search('^(?:(?!-).)
fn. dataTable. ext.search . This is an array of functions (push your own onto it) which will will be run at table draw time to see if a particular row should be included or not. This example shows a search being performed on the age column in the data, based upon two inputs.
The first <table > element is used to add a custom filter element. I have added one input box for name searching and <select > element for gender filtering. The second <table > is used to initialize dataTable. <!--
The search, filter, and pagination functionality can be easily added to the HTML table with DataTables. Using the DataTables server-side processing, you can fetch the data dynamically from the database and list them in an HTML table with search, sorting, and pagination functionality.
When DataTable is initialized on the HTML table then it generates pagination which has sorting, searching on all columns, change the number of records display features. The default search control mainly uses to finds value on all columns and display filter list. But it can be customized.
There are two main parts to the HTML, the category filter drop-down menu and the datatable. The values in the category filter will be the values that are to be filtered from the table when the user selects an item. <!-- Create the dropdown filter --> <!-- Set up the datatable --> <!--
To search multiple values from single column you can use pipeline as below:
dataTable.columns(4).search("Rejected|Done", true, false, true).draw();
You have to pass four parameters as below:
(true)
or not (default, false)
.More details
var table = $('#example').DataTable();
table .columns( 4 ) .search("") .draw();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With