Search code examples
javascriptyadcf

yadcf custom_filter - how to find not null and not empty values?


I want to filter on the column - a value that is not null and not empty.

 yadcf.init(table, [        
    {
        column_number: 4,
        filter_type: 'custom_func',
        custom_func: myCustomFilterFunction,
        data: [ {
            value: 'empty',
            label: 'Empty'
        },
            {
            value: 'notempty',
            label: 'NotEmpty'
            }
        ],
        filter_default_label: "All"
    }
]);  

function myCustomFilterFunction(filterVal, columnVal) {
    var found;
    if (columnVal === '') {
        return true;
    }
    switch (filterVal) {
        case 'empty':
            found = columnVal.search(null);
            break;
        case 'notempty':
            found = columnVal.lenght > 0;
            break;
        default:
            found = 1;
            break;
    }
    if (found !== -1) {
        return true;
    }
    return false;
}

Empty values filtering works . But how to filter not empty and not null values ? columnVal.lenght > 0 does not work


Solution

  • There are many ways to accomplish this, here is a one way...

    function myCustomFilterFunction(filterVal, columnVal) {
        var found = false;
        debugger;
        switch (filterVal) {
            case 'empty':
                found = columnVal ? false : true;
                break;
            case 'notempty':
                found = columnVal ? true : false;
                break;
            default:
                break;
        }
        return found;
    }
    

    See working jsfiddle