DataTables : using button - search in multiple columns using OR operator
DataTables : using button - search in multiple columns using OR operator
Hello, I am trying to make button called 'No Dates' on my page that can filter 3 columns from the table with result 'No Dates' text. I am displaying 'No Dates' text if there is null date. so using button I would like to filter table which has null dates in 3 columns - col 8, 9, 10 using OR condition. I am aware that dt.column(8,9,10).search('No Dates').draw();
will not work as it use AND operation.
I also tried using search plug-ins but its not working and also not giving any error. Here is what I tried.
// NO DATES BUTTON
table.button().add(
null, {
text: 'No Dates',
action: function ( e, dt, node, config ) {
$.fn.dataTable.ext.search.push(
function( settings, searchData, index, rowData, counter)
{
var install = searchData[8];
var pico = searchData[9];
var sat = searchData[10];
if (install === 'No Dates' || pico === 'No Dates' || sat === 'No Dates' )
{
return true;
}
return false;
});
}
}
);
I also tried following this link from web but no luck so far
https://stackoverflow.com/questions/62680861/datatables-search-in-multiple-columns-using-or-operator
I also have RESET Button which can remove all filters and display original table using below code. Not sure how to use
$.fn.dataTable.ext.search for resetting back to original form. Please advice what I am doin wrong here. Thank you.
// RESET BUTTON - To Display All Data
table.button().add(
null, {
text: 'Reset',
action: function ( e, dt, node, config ) {
table.columns().search( '' ).draw();
// Tried below line from the web solution to reset when I tried using myfilter function
//$.fn.dataTable.ext.search.splice($.fn.dataTable.ext.search.indexOf(myfilter, 1));
}
}
);
This question has accepted answers - jump to:
Answers
Its hard to say with just the code snippets. Please build a simple example that represents the data you have so we can experiment with the code.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
@kthorngren Hi Kevin, it is little complex to build example. But here is the url of my page. Let me know if you able to open it and see the code.
https://assettrack.cx/construction/systemsProgress1.php
I get the following error:
We don't need a full example. Build a test case that has a sample of your data. Use Javascript loaded data or if you table is dom sourced grab a few rows. You can start with this template:
http://live.datatables.net/
Here is an example I created a long time ago that might give you some ideas.
http://live.datatables.net/mucevape/1/edit
Kevin
@kthorngren I tried adding manually in this example. Not sure why buttons are not displayed.. but when I click on No Dates button.. it should filter column 2 and 3 with result 'No Dates' using OR condition. And when I click Reset button it should remove the filter.
live.datatables.net/nujomogi/1/edit
After you push or pop the search plugin you will need to use
draw()
to have Datatables perform the search and execute the plugin if its installed. I used thedt
parameter of thebuttons.buttons.action
function to get the Datatables API instance.http://live.datatables.net/rukuwaxa/1/edit
Kevin
@kthorngren Thanks Kevin its working correctly in the example but its not working in my code as my data is coming from ajax and its based on some calculation. Here the code of those 3 columns and how it looks like.
I am sorry for the long code but this 3 column should filter where I displayed 'No Dates' if col1 - var install_BL or install_F or col2- pico_BL or pico_F or col3- sat_BL or sat_F has null dates.
I'm not quite sure how that is working because
columns.render
expects a return statement to be used for the data to display. However you can use Orthogonal data to set the values used for the search function. For example, return the "No Date" when appropriate or return the appropriate data you want to search for.If you still need help then please update the test case to show the problem with your
columns.render
code or something close to it.Kevin
@kthorngren Sorry for confusion. Just because code is too long the comment is gone for approval. so please check this link , I have added my code in comments in javascript tab.
live.datatables.net/qikowope/1/edit
Please provide the steps to replicate the issue in the test case.
Kevin
@kthorngren Thank you Kevin for your help. I used different approach for my button. Instead of searching using
$.fn.dataTable.ext.search.push()
, I created new hidden column in which using render function and based on OR condition I assigned number for those 3 columns where I want to apply search. And than based on this new hidden column I filter my table. so if my condition for searching in 3 columns fulfill, this new hidden col will return '1' else '0'. Its working perfectly fine. Here is the code if its helpful for others.Sounds like a good approach
Kevin