Unescape $.fn.dataTable.util.escapeRegex()

Unescape $.fn.dataTable.util.escapeRegex()

JohnBeaJohnBea Posts: 11Questions: 2Answers: 1
edited July 2016 in Free community support

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

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    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:

    var _re_escape_regex = new RegExp( '(\\' + [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\', '$', '^', '-' ].join('|\\') + ')', 'g' );
    val.replace( _re_escape_regex, '\\$1' );
    

    You would basically need to invert that.

    Allan

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1

    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?

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    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

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1
    edited July 2016

    I'm stripping the ^ and $. In above code I'm doing it by

    colSearch.search.slice(1, -1));
    

    But unfortunately, state saving saves the escaped value. Im checking it by

    // alert(colSearch.search.slice(1, -1))
    

    and it shows escaped value.
    If it is like You say, that state saving should not save escaped value, maybe it is a bug?

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Possibly, although I thought this was working okay. Can you link to a test case showing the issue please.

    Allan

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1
    edited July 2016

    In this example https://jsfiddle.net/r4wornfa/ please filter in select box "Tiger.Nixon" and hit Run.
    Alert will show You escaped value.

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Oh I see - yes. Sorry - it isn't DataTables that is doing the escaping, but this line:

                                    var val = $.fn.dataTable.util.escapeRegex(
                                        $(this).val()
                                    );
    

    There are two options I can see:

    1. Use event handlers to store the raw value into the state and then read that back
    2. Write an inverse of the regex escape function.

    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! :smile: )

    Allan

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1

    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.

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1

    Hi Allan, have You finished the solution ?

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    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

  • JohnBeaJohnBea Posts: 11Questions: 2Answers: 1
    edited August 2016

    Ok, no problem. In about which version number can we expect this new feature ?

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Either 1.12 or 2 depending on a number of other factors. I don't currently have a time scale for either.

    Allan

This discussion has been closed.