Column filter
Column filter
Description of problem:
Hi everyone!
I am trying to find a way to customise the search into one of the column in my grid.
Values for this column can be dynamic: for example 100,101,102,1000.
I would like to limit the result to a very specific customisation. In the above values for example, I would like to search for 100,102 and only having as result rows where either 100 or 102 are present (not 1000 or 10201 for example).
Is that possible?
I am trying using the following syntax:
this.filter = (value) => {
this.tableRef.column(`${this.columnName}:name`).search(value).draw();
let searchPattern = value
.split(',')
.map(item => $.fn.dataTable.util.escapeRegex(item.trim()))
.join('|');
this.tableRef.column(`${this.columnName}:name`).search(searchPattern, true, false).draw();
}
but the result is coming with values containing for example 10050, 100321.
Thanks for help!
Ross
Replies
Yes, you could build up a more complex regex expression that matches a number at the start, in the middle (with
,on either side), or at the end, but I think I'd be tempted to just use a function to do it:I'm sure it is possible to code-golf that to be smaller, but hopefully in this form it is clear what is going on.
I'll confess I've not tested the code, so it might have a daft error in it somewhere, but again, hopefully it is clear what is happening - but let me know how you get on with it.
Allan
Hi Allan!
I had to change a bit the code but it perfectly worked, thanks for help!
Leaving here for anyone who might have the same request:
Ross
Awesome - thanks for confirming. Good to hear that did the job for you.
Allan