SearchPanes - Custom Filtering Options (Partial String Filter)
SearchPanes - Custom Filtering Options (Partial String Filter)

I'm trying to create a create a custom filter where I can filter by a partial string. To borrow the example here:
https://datatables.net/extensions/searchpanes/examples/customFiltering/customOptionConditions
Some row data that I have says, "Edinburgh". While other row data that I have says, "Edinburgh and Glasgow".
The following code will only filter data that say "Edinburgh" rather than also including "Edinburgh and Glasgow" as a partial string filter.
Is there a way I can adapt the below code to succeed in filtering both 'Edinburgh' and 'Edinburgh and Glasgow ' (without hard-coding it)?
{
searchPanes: {
options:[
{
label: 'Not Edinburgh',
value: function(rowData, rowIdx){
return rowData[3] !== 'Edinburgh';
}
},
{
label: 'Not London',
value: function(rowData, rowIdx){
return rowData[3] !== 'London';
}
}
],
combiner: 'and'
},
targets: [3]
}
Replies
See if the Srting includes() method will work for what you want.
Kevin
I have it working here:
http://live.datatables.net/nipucawa/1/edit
However, my main issue is that on my end with my work, instead of:
return rowData[3].includes('Edinburgh');
I have:
return rowData["Membrane_Attachment_Type"].includes('Edinburgh');
SO I wasn't able to get it to work on my end using the data variable pulled from my server_processing.php file unless I used the name. I tried using the numeric method with my column number, but it doesn't work:
return rowData[7].includes('Edinburgh');
I thought maybe it was due to the top portion:
But that didn't work for me either... see anything wrong here?
Without knowing your data structure its hard to say specifically. Sounds like your data structure is object based so you would use object notation, something like
rowData["Membrane_Attachment_Type"]
. I usedcolumns.data
to define object for your example and it works with usingreturn rowData.office.includes('Edinburgh');
:http://live.datatables.net/poqafecu/1/edit
You can use the browser's debugger or console.log statements to see what
rowData
is within the function. If you still need help then please provide a link to your page or a test case showing the problem you are having.Kevin
When I use this first code snippet, it pulls the data correctly:
This works fine pulling data and anything that starts with "WB II", but won't use WB II as a keyword search, so for instance, "WB II, SBIV, etc.." won't be included in the filter.
When I try this:
and check the console, it gives me:
"Uncaught TypeError: rowData.Membrane_Attachment_Type is null"
and points to this line:
Even though it's not really null, so I'm not sure how to solve this one.
That suggests you have a value of
null
in that column. Is this the case? If so then you will need an additional check so you don't useincludes()
withnull
values or change yournull
values to empty strings""
.Kevin
You're right, that was the issue. Works great now! You've been a huge help, thank you!