Search code examples
yadcf

yadcf plugin - using select filter_type with custom_func


Using the yadcf plugin, is it possible to have the "select" filter_type with a custom function defined for custom_func to perform the filtering/matching?


Solution

  • Yes

    For example

    var table = $('#sample-table');
    var dtable = table.DataTable();
    
    yadcf.init(dtable, [{
        column_number: 0,
        select_type: "chosen",
        text_data_delimiter: ";",
        filter_type: 'custom_func',
        custom_func: myCustomFilterFunction
    }]);
    
    
    function myCustomFilterFunction(filterVal, columnVal) {
        const items = columnVal.split(';');
        return items.some(function(arrVal) {
            return filterVal === arrVal;
        });
    }
    

    Read from the docs

    * filter_type
                Required:           false
                Type:               String
                Default value:      'select'
                Possible values:    select / multi_select / auto_complete / text / date / range_number / range_number_slider / range_date / custom_func / multi_select_custom_func / date_custom_func
                Description:        The type of the filter to be used in the column
    
    
    * custom_func
                Required:           true (when filter_type is custom_func / multi_select_custom_func / date_custom_func)
                Type:               function
                Default value:      undefined
                Description:        should be pointing to a function with the following signature myCustomFilterFunction(filterVal, columnVal, rowValues, stateVal) , where `filterVal` is the value from the select box,
                                    `columnVal` is the value from the relevant row column, `rowValues` is an array that holds the values of the entire row and `stateVal` which holds the current state of the table row DOM
                                    , stateVal is perfect to handle situations in which you placing radiobuttons / checkbox inside table column. This function should return true if the row matches your condition and the row should be displayed) and false otherwise
                Note:               When using multi_select_custom_func as filter_type filterVal will hold an array of selected values from the multi select element