Set a default value in yadcf

Set a default value in yadcf

mbencimbenci Posts: 11Questions: 6Answers: 0

Hi all,

I'd like to know if there is a method to define a default filter value (NO default label) using yadcf.

Thanks and regards,
Marco B

Answers

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    edited August 2014

    You should use the exFilterColumn function of yadcf, see it in action: http://yadcf-showcase.appspot.com/multiple_tables.html (notice the "Numbers" and "Values" column filters)

    Use the latest 0.8.2 version (in case you use it already then download it again from github - because I just published a fix for 0.8.2)

    Here its documentation:

    exFilterColumn
    Description: Allows to trigger filter/s externally/programmatically (support ALL filter types!!!) , perfect for showing table with pre filtered columns
    Arguments: table_arg: (variable of the datatable),
    array of pairs: column number String/Object with from and to, filter_value (the actual string value that we want to filter by)
    Usage Example: yadcf.exFilterColumn(oTable, [[0, 'Some Data 2']]); //pre filter one column
    yadcf.exFilterColumn(oTable, [[0, 'Some Data 1'], [1, {from: 111, to: 1110}], [2, {from: "", to: "11/25/2014"}]]); //pre filter several columns

  • mbencimbenci Posts: 11Questions: 6Answers: 0

    Hi Daniel,
    Hi Daniel,
    thanks for your answer.
    I tried your solution but it does not work for me.
    Some details: I use a "server side" configuration and I use "yadcf_data" to populate the select. I've also updated my yadcf version as suggested by you.
    The filter works correctly but there is not pre selection.

    My code is:

    $(document).ready( function () {
        'use strict';
            var oTable=$('#example').dataTable({
                "bProcessing": true,
                "bSearchable": false,
                "bServerSide": true,
                "bJQueryUI": true,
                "bInfo": true,
                "iDisplayLength": 20,
                "order": [[ 3, "desc" ]],
                "sAjaxSource": "scripts/server_processing2.php",
                "dom": '<"top"i>rt<"bottom"p><"clear">',
                "aoColumns": [
                                ...
                            ],
            });
    
       oTable.yadcf([
        {column_number : 1, filter_container_id: "external_filter_container"},
        {column_number : 2, filter_container_id: "external_filter_container2"},
        {column_number : 4, filter_type: "range_number_slider"},
        {column_number : 5, filter_type: "range_number_slider"},
        {column_number : 6, filter_type: "range_number_slider"},
        {column_number : 7},
        {column_number : 8},
        {column_number : 9}
       ]);
    
       yadcf.exFilterColumn(oTable, [
         [1, "2014"],
         [2, "1"]
       ]);          
    });
    

    Thanks and regards,
    Marco B

  • mbencimbenci Posts: 11Questions: 6Answers: 0
    edited August 2014

    Hi Daniel,
    I've changed a little my code: now I use DataTable (D uppercase) and now in firebug I've this error:

       TypeError: tmpStr.replace is not a function
    

    All works except the exFilterColumn.
    The code now is:

    $(document).ready( function () {
        'use strict';
            var oTable=$('#example').DataTable({
                "bProcessing": true,
                "bSearchable": false,
                "bServerSide": true,
                "bJQueryUI": true,
                "bInfo": true,
                "iDisplayLength": 20,
                "order": [[ 3, "desc" ]],
                "sAjaxSource": "scripts/server_processing2.php",
                "dom": '<"top"i>rt<"bottom"p><"clear">',
                "aoColumns": [
                                ...
                            ],
            });
     
       yadcf.init(oTable,[
        {column_number : 1, filter_container_id: "external_filter_container"},
        {column_number : 2, filter_container_id: "external_filter_container2"},
        {column_number : 4, filter_type: "range_number_slider"},
        {column_number : 5, filter_type: "range_number_slider"},
        {column_number : 6, filter_type: "range_number_slider"},
        {column_number : 7},
        {column_number : 8},
        {column_number : 9}
       ]);
     
       yadcf.exFilterColumn(oTable, [
         [1, "2014"],
         [2, "1"]
       ]);         
    });
    
  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    edited August 2014

    I haven't tried exFilterColumn with serverside so there might be a bug , but regarding the

    TypeError: tmpStr.replace is not a function

    My guess it has to do with you mixing new datatabled API (Capital "D") with old API style attributes (Hungarian notation) , change bProcessing -> processing , aoColumns - > columns ... etc... (see migration guide or some docs of datatable)

    Also , I strongly recommend you to use (for range_number_slider)

    filter_delay: 500
    

    or any other number to reduce server usage (read docs for more info)

  • mbencimbenci Posts: 11Questions: 6Answers: 0

    Hi Daniel,

    neither if I use DataTable nor dataTable the preSelection works correctly using server side.
    Do you can make a check please?

    Thanks and regards,
    Marco B

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67

    Yes,

    please open an issue for that https://github.com/vedmack/yadcf/issues

  • toomastoomas Posts: 1Questions: 0Answers: 0

    When I tried to use this it didn,t work for me either

    yadcf.exFilterColumn(oTable, [
    [1, "2014"],
    [2, "1"]
    ]);

    Byt when I added [] around the sorting argument it worked fine.

    yadcf.exFilterColumn(oTable, [
    [1, ["2014"]],
    [2, ["1"]]
    ]);

This discussion has been closed.