column.search filtering on blank value

column.search filtering on blank value

good day, i am using
column.search(val, true, false ).order( [[ 1, 'asc' ]] ).draw();
and in my column i have blank values and some strings. Could you recomend me how can i receive rows with blank values? Reason, when i send to value blank string, i am getting whole list, like unfiltered, but i wanna to have list where cells is blank... Do someone has ideas?

Answers

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17
    edited October 2017

    Maybe this might give you some suggestions? https://datatables.net/reference/api/filter()

    In the examples, you can check for blank.

  • allanallan Posts: 61,642Questions: 1Answers: 10,093 Site admin

    The filter() method doesn't actually change what is shown in the table is the thing, it filters the result set from an API call.

    column().search() will change what is shown in the table, and you could use a regex to get only blank values:

    table.column(0).search( '^$', true, false );
    

    http://live.datatables.net/lehirela/1/edit

    Allan

  • Sergey.Volchenkov@arris.comSergey.Volchenkov@arris.com Posts: 3Questions: 2Answers: 0

    Allan,
    column(0).search("", true, false ).order( 'desc' ).draw();
    column(0).search('^$', true, false ).order( 'desc' ).draw();

    providing exactly same result

    if u have some idea, lease tell me

  • allanallan Posts: 61,642Questions: 1Answers: 10,093 Site admin

    I'd need a link to a test case showing the issue please. The example I linked to above appears to show it working.

    Allan

  • AashutoshAashutosh Posts: 1Questions: 0Answers: 0
     initComplete: function () {
                this.api().columns('.colFilter').every(function () {
                    var column = this;
                    var select = $('<select class="form-control input-sm"><option value="">Select</option></select>')
                        .appendTo($(column.footer()).empty())
                        .on('change', function () {
                            var val = $(this).val();
                            if (val === "\\(Blank\\)")
                                column
                                    .search('^$', true, false)
                                    .draw();
                            else
                                column
                                .search(val ? '^' + val + '$' : '', true, false)
                                .draw();
    
                        });
    
                    column.data().unique().sort().each(function (d, j) {
                        if (d === '')
                        {
                            d='(Blank)';
                        }
                        select.append('<option value="' + d + '">' + d + '</option>')
                    });
                });
            },
    
  • kthorngrenkthorngren Posts: 20,266Questions: 26Answers: 4,764
    edited December 2019

    As Allan asked we need a running test case that shows the problem to help debug. this way we can see your data and the behavior of your code.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    The place to start is to make sure if (val === "\\(Blank\\)") is correct. You can put a console.log statement inside to make sure .search('^$', true, false) is executing when you select (blank). I think the backslashes are causing the if statement to always be false. Its not a regex so they aren't needed to escape the parenthesis.

    Kevin

  • Wizard85Wizard85 Posts: 9Questions: 2Answers: 0

    I have a similar issue to the abouve one, but I'm using bootstrap selectpicker.

    How should I change the below code to manage the "(Blank)" string?

    .on('changed.bs.select', function(e) {
                                    var val = $(this).val();
                                    var fVal = val.join("|");
                                    
                                    column.search(fVal, true, false).draw();
    ...
    
  • kthorngrenkthorngren Posts: 20,266Questions: 26Answers: 4,764

    Its been asked multiple times to provide a test case showing the issue so we can help debug. Without seeing your specific data it wold be hard to guess what to change.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.