Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom filtering With Button for Datatable

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)

like image 747
Suhandy Chow Avatar asked Aug 08 '16 08:08

Suhandy Chow


People also ask

How to filter DataTable based on column value in jQuery?

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('^(?:(?!-).)

What is FN DataTable ext search push?

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.

How to add custom filter elements in DataTable?

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. <!--

How to add search and filter to HTML table with DataTables?

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.

What is the default search control in DataTable?

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.

What are the different parts of the HTML category filter?

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 --> <!--


2 Answers

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:

  1. Input: Search string to apply to the table
  2. Regx: Treat as a regular expression (true) or not (default, false).
  3. Smart: Perform smart search (default, true) or not (false). See below for a description of smart searching.
  4. CaseInsen: Do case-insensitive matching (default, true) or not (false).

More details

like image 133
Sandip - Frontend Developer Avatar answered Oct 16 '22 18:10

Sandip - Frontend Developer


var table = $('#example').DataTable();

table .columns( 4 ) .search("") .draw();

like image 38
destinydz Avatar answered Oct 16 '22 17:10

destinydz