AngularJS [SOLVED]: Angular datatable select filter not working with 0 or false value

AngularJS [SOLVED]: Angular datatable select filter not working with 0 or false value

Home Forums Frameworks AngularJS tutorials AngularJS [SOLVED]: Angular datatable select filter not working with 0 or false value

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246238

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’m using Angular datatable in Server side processing mode(Ajax) And use Light Column Filter plugin to filter data in each columns.

    HTML

    <table datatable dt-options="dtOptions" dt-columns="dtColumns"></table>
    

    Datatable config

    DTOptionsBuilder.dtOptions = DTOptionsBuilder.fromSource()
        .withOption('processing', true)
        .withOption('serverSide', true)
        .withDataProp('data')
        .withPaginationType('full_numbers')
        .withOption('ajax', {
             url: '/products',
             type: 'GET',
             headers: {Authorization: 'Bearer ' + $auth.getToken()},
             error: function (xhr, error) {
                $log.error(error);
             },
             complete: function (data) {
                $log.log('Done');
             }
        })
        .withLightColumnFilter({
             '0': {type: 'text'},
             '1': {
                type: 'select',
                values: [
                   { value: 1, label: 'Yes'},
                   { value: 0, label: 'No'}
                ]
             }
        });
    

    create columns

     $scope.dtColumns = [
    
        DTColumnBuilder.newColumn('title').withTitle('title').renderWith(function(data) {
           return data;
        }),
        DTColumnBuilder.newColumn('for_sale').withTitle('for sale').renderWith(function(data){
           return data; // 0, 1 or true, false
       })
     ]
    

    Filter with type:'text' is correct and work fine. But filter with type:'select' is incorrect! In the other words when I select Yes filter is work and data filtered, But when select No result is empty.

    I test select value with 0,1 and true,false. In both ways, option with true or 1 value is correct but option with 0 or false value is incorrect.

    In your opinion, where is the problem?

    #246239

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You define the values as nunbers

    values: [
      { value: 1, label: 'Yes'},
      { value: 0, label: 'No'}
    ]
    

    But you return strings

    return data; // 0, 1 or true, false
    

    data will always be a string because the source is JSON. Try set both to strings and return '0' and '1' by force :

    { value: '1', label: 'Yes'},
    { value: '0', label: 'No'}
    

    and

    return data == '0' || data == 'false' ? '0' : '1'
    

    Source: https://stackoverflow.com/questions/47966064/angular-datatable-select-filter-not-working-with-0-or-false-value
    Author: davidkonrad
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.