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: 143Questions: 22Answers: 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: 50,882Questions: 1Answers: 7,626 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: 50,882Questions: 1Answers: 7,626 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: 7,010Questions: 22Answers: 1,529
    edited December 4

    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

Sign In or Register to comment.