Unescape $.fn.dataTable.util.escapeRegex()
Unescape $.fn.dataTable.util.escapeRegex()
Hello,
i need to unescape api's $.fn.dataTable.util.escapeRegex(). I need it to restore state of column filters made with select fields.
I'm doing the select fields with this:
initComplete: function () {
this.api().columns('.select-filter').every(function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
and restoring state with this:
var state = table.state.loaded();
if (state) {
table.columns().eq(0).each(function (colIdx) {
var colSearch = state.columns[colIdx].search;
// alert(colSearch.search.slice(1, -1))
if (colSearch.search) {
$('select', table.column(colIdx).footer()).val(colSearch.search.slice(1, -1));
}
});
table.draw();
}
Above code works for me, except situation, when colSearch.search.slice(1, -1) retruns values with special characters. They are with escapes (made by $.fn.dataTable.util.escapeRegex). So i need to do something like unescape this value. Does anyone tried to do that ?
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
There isn't a built in way to do this I'm afraid as it isn't something that DataTables itself currently uses.
This is basically what the escape function does:
You would basically need to invert that.
Allan
Hi allan,
thanks for Your answer. Before I go any further with invertion of escape function, please tell me, is there any other easier/cleaner way to restore state of column filters made with select fields, than provided in example code above?
Not really I'm afraid. You could perhaps use the events to store the raw value rather than the regex value, but that's the best I can think of at the moment.
Don't you just need to strip the
^
and$
that are being prefixed and postfixed to the string? State saving doesn't save the escaped value. It will save what is passed in (which has those characters), so I think if you just remove them you'll get your original value.Allan
I'm stripping the ^ and $. In above code I'm doing it by
But unfortunately, state saving saves the escaped value. Im checking it by
and it shows escaped value.
If it is like You say, that state saving should not save escaped value, maybe it is a bug?
Possibly, although I thought this was working okay. Can you link to a test case showing the issue please.
Allan
In this example https://jsfiddle.net/r4wornfa/ please filter in select box "Tiger.Nixon" and hit Run.
Alert will show You escaped value.
Oh I see - yes. Sorry - it isn't DataTables that is doing the escaping, but this line:
There are two options I can see:
Possibly it is something DataTables should provide (2) and I'll look into that as I improve DataTables filtering abilities for the next version.
I'll try to make some time this week to implement one of the two above, although can't say when exactly that would be (busy times! )
Allan
Hi Allan, thanks For Your answer. I'm happy that my question can end with providing new solution to Datatables Please, provide a short info in this thread, when You finish the solution. This will be a guide for other people facing same problem.
Hi Allan, have You finished the solution ?
No sorry. I've been overwhelmed by support requests recently and haven't been able to spend any time on this yet I'm afraid.
Allan
Ok, no problem. In about which version number can we expect this new feature ?
Either 1.12 or 2 depending on a number of other factors. I don't currently have a time scale for either.
Allan