I need to filter out rows from a datatable that do not contain a certain value in a column. For example, with the data below, I would like to just show results where type = "Dog":
<table id="petowners">
<tr>
<th>Type</th>
<th>Breed</th>
<th>Owner</th>
</tr>
<tr>
<td>Dog</td>
<td>Doberman</td>
<td>Peter</td>
</tr>
<tr>
<td>Cat</td>
<td>Jaguar</td>
<td>Paul</td>
</tr>
<tr>
<td>Dog</td>
<td>Poodle</td>
<td>Mary</td>
</tr>
<tr>
<td>Cat</td>
<td>Lion</td>
<td>Ringo</td>
</tr>
<tr>
<td>Cat</td>
<td>Tiger</td>
<td>John</td>
</tr>
</table>
Here's the script that I am using to configuring sorting and results-per-page. Obviously the column-based filtering is the missing bit I need help with.
$(document).ready(function() {
$('#petowners').dataTable( {
"order": [[ 0, "asc" ]],
"iDisplayLength": -1,
"oLanguage":
{
"sLengthMenu": 'Display <select>'+
'<option value="10">10</option>'+
'<option value="10">25</option>'+
'<option value="10">50</option>'+
'<option value="100">100</option>'+
'<option value="500">500</option>'+
'<option value="-1">All</option>'+
'</select> records'
},
} );
} );
I need to add two links, buttons or checkboxes, one for "Dog" and one for "Cat".
When a user clicks "Dog", only the rows that contain "Dog" in the "Type" column are displayed. Similarly when "Cat" is clicked, only the rows that containt "Cat" in the "Type" column should be displayed.
It seems like a fairly straightforward feature, but I have not been able to find anything in the datatables.net site that shows how this could be done.
I hope this makes sense and someone can help.
Many thanks in advance wOnkO tHe SaNE
Filtering the values of ASP.NET DataTable can be achieved using RowFilter property where in we can make use of “where” clause to filter our DataTable based on Column values. Filtering can also be done for multiple column values and also for Date values using the ‘between’ keyword.
This article highlights various ways to filter rows in python datatable. The examples used here are based off the excellent article by Susan Baert. You can filter numeric variables based on their values. A number of commonly used operators include: >, >=, <, <=, == and !=.
Select all rows where brainwt is larger than 1, but bodywt does not exceed 100: There are two options for filtering out empty rows; comparing with None, or using the isna function: It is possible to filter for rows based on values across columns.
In this post, we will see different ways to filter Pandas Dataframe by column values. First, Let’s create a Dataframe: Method 1: Selecting rows of Pandas Dataframe based on particular column value using ‘>’, ‘=’, ‘=’, ‘<=’, ‘!=’ operator. Example 1: Selecting all the rows from the given Dataframe in which ‘Percentage’ is greater than 75 using [ ].
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('^(?:(?!-).)*$\r?\n?', true, false).draw();
If you want to undo the filter, just pass an empty string instead of regex.
data_table.columns(column_index).search('', true, false).draw();
Hope this helps.
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