How to search column for string with spaces
How to search column for string with spaces
All the answers I found to this question on Stack Overflow related to legacy versions of Datatables. And those answers didn't work for me, so figure it's a good time to ask this question for the current version of Data Tables.
Suppose I want to search a column for a specific string -- "Oct 08 to Oct 14". I have verified that the correct column index is used and that dayRangeFilter
has correct values. The table does not filter, however. The browser console shows no errors.
if (dayRangeFilter === "All") {
summaryTable.column(7).search("");
} else {
// e.g., dayRangeFilter === "Oct 08 to Oct 14"
console.log(dayRangeFilter); // to prove correct value exists
summaryTable.column(7).search(dayRangeFilter);
summaryTable.draw();
}
I have tried the variant with regular expressions, to no avail.
if (dayRangeFilter === "All") {
summaryTable.column(7).search("");
} else {
// e.g., dayRangeFilter === "Oct 08 to Oct 14"
console.log(dayRangeFilter);
summaryTable.column(7).search(`^${dayRangeFilter}$`,true,false);
summaryTable.draw();
}
In desperation, I tried every variant of the true/false option toggles for search
. No luck.
I tried using a string instead of a variable. summaryTable.column(7).search("Oct 08 to Oct 14");
I tried the regular expression variant and toggled all the buttons again.
Research isn't helping me, nor is randomly pressing stuff to see if it works.
What is the correct column(x).search(y)
code to search for strings with spaces?
This question has accepted answers - jump to:
Answers
Are you saying that the one cell in the column will contain
Oct 08 to Oct 14
?Or are you trying to search for a range where the values could be
Oct 08
orOct 14
within each cell?If its the first then it looks like your code should work. Can you provide a simple test case showing the issue:
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I'm not sure I understand. Does the string "Oct 08 to Oct 14" actually exist in the table, or are you intending a date range search?
@tangerine -- The value actually exists in the table I'm trying to search -- in multiple rows. And I'm doing this filtering based on click events in another table -- not by the standard search box
@rdm - apologies - I answered in haste and have edited the previous post.
...EDIT: I seem to have replicated what Kevin said at the same time. Not going well....
Stick with Kevin - he's good on searches.
@kthorngren -- I'm trying to find records that precisely match -- for example -- "Oct 08 to Oct 14".
Here's the url of the datatables fiddle. And here's the weird thing. On the live fiddle, the code works. http://live.datatables.net/yosohafo/1/.
However, the same code isn't working in my own scenario.
Just in case the problem might have been whitespaces, I trimmed the any extra spaces on the column from the database and trimmed the search variable. No solution there.
@kthorngren and @tangerine -- Thanks for your help. You confirmed that I indeed have the right code.
It turned my problem was that I was trying to filter the wrong table. Now the code works.