Exact dropdown match search

Exact dropdown match search

MundaringMundaring Posts: 34Questions: 12Answers: 1

Hi Allan,

I am trying to make an exact match, but it is not working for me. Could you please give me a hand on this?

this is my initComplete code:

initComplete: function (oSettings, json) {
                var col = 0;
                this.api().columns().every(function () {
                    if (col === 2 || col === 7 || col === 8 || col === 10) {
                        var column = this;

                        var select = $('<select class="styled-select"><option value="">All</option></select>')
                            .prependTo($('#tblList').find('thead tr:eq(0) th:eq(' + col + ')'))
                            .on('change', function () {
                                var val = $.fn.dataTable.util.escapeRegex(
                                    $(this).val()

                                );
                                column
                                    //.search(val ? '^' + val + '$' : '', true, false)
                                    .search(this.value)
                                    .draw();
                            });
                        if (col === 2) {
                            json.meta.assetModules.map(function (item) {
                                select.append('<option value="' + item.Name + '">' + item.Name + '</option>')
                            });
                        }
                        if (col === 7) {
                            json.meta.assetTypes.map(function (item) {
                                select.append('<option value="' + item.Name + '">' + item.Name + '</option>')
                            });
                        }
                        if (col === 8) {
                            json.meta.status.map(function (item) {
                                select.append('<option value="' + item.Name + '">' + item.Name + '</option>')
                            });
                        }
                        if (col === 10) {
                            json.meta.wards.map(function (item) {
                                select.append('<option value="' + item.Name + '">' + item.Name + '</option>')
                            });
                        }
                    }
                    col++;
                });
            },

For (col === 8) the possible values on the drop down list are (Active , Not Active) the option "Not Active" is filtered ok but when I choose Active is loading as well the "Not Active", I know the reason is because it contains also the word "Active". I would like to make an exact match on this one. I have tried code like this to replace code between lines 14-17:

if (col === 8) {
    column
        .search(val ? '^' + val + '$' : '', true, false)                                                                                                                                
        .draw();
}
else {
    column
        //.search(val ? '^' + val + '$' : '', true, false)                                                                                
        .search(this.value)
        .draw();
}

Also I tried these other ones:

regExSearch = '^\\s' + myValue +'\\s*$';
table.column(columnNo).search(regExSearch, true, false).draw();
table.search( '"My exact match"' );

Thanks,
Wilson

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Hi Wilson,

    Are you using server-side processing? If so, you'd need to have your server-side script handle the regex input (or otherwise modify it to do an exact match).

    If its not that, can you give me a link to a test case showing the issue please?

    Thanks,
    Allan

  • MundaringMundaring Posts: 34Questions: 12Answers: 1

    Hi Allan,

    I have not a link, my developments are for Intranet apps. Yes I am using server side processing, How can I do an exact match? or how can I do my server-side script handle? I tried the lines before without success.

    As you can see line 15 it is commented and did not work that is why I replace it with line 16, which is working Ok.

    This is related to this case you help me up.

    https://datatables.net/forums/discussion/42249/how-to-populate-dropdownlist-on-individual-column-searching-on-server-side-processing#latest

    Thanks,
    Wilson

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    If you are using server-side processing, don't use a regular expression (unless you update your server-side script to accept it) - as indeed you note you have done with line 16.

    With server-side processing the key is the server-side script. I'm not sure what ever-side script you are using, but likely it is doing something like columnName LIKE '%searchTerm%'.

    Instead of that you want it to do columnName = 'searchTerm'.

    Allan

  • MundaringMundaring Posts: 34Questions: 12Answers: 1

    Hi Allan,

    I have found a similar case in your website that I am having problem with. If you go to the link below and select in position "Director", you will get the Director's ones but also the "Regional Director" I would like to get exact match, How can I accomplish this in your example, could you please tell me what needs to be added?

    http://www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/examples/api/multi_filter_select.html

    Please see attached image

    Thanks,
    Wilson

    9.JPG 73.2K
  • MundaringMundaring Posts: 34Questions: 12Answers: 1

    Hi Allan,

    I have also tried typing Director en position in your example in a new link below, using smart filter or Regex, still not working. It is doing what you said columnName LIKE '%searchTerm%'. when I really need is columnName = 'searchTerm'.

    http://www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/examples/api/regex.html

    Thanks,
    Wilson

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Allan already explained that you need to amend your server-side script appropriately.

    With server-side processing the key is the server-side script. I'm not sure what server-side script you are using, but likely it is doing something like columnName LIKE '%searchTerm%'.
    Instead of that you want it to do columnName = 'searchTerm'.

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Answer ✓

    Yes - the demo script on this site will use a LIKE statement with wildcards. As tangerine highlights, you'd need to modify whatever server-side script you are using since you are using server-side processing.

    Allan

This discussion has been closed.