简体   繁体   中英

How to Source YADCF Select Filter Options with Response Array

Here is a JSFiddle: https://jsfiddle.net/5sgc03my/ . I have a DataTable with a couple YADCF filters:

var oTable = $('#example').DataTable({
    "serverSide": true,
    "ajax": {"url": "/platform/activities/?format=datatables",
         "dataSrc": function(json){
           yadcf_data_1 = json.options.activity_type
           console.log(yadcf_data_1)
           return json.data;
          }
        },
});
console.log(yadcf_data_1)

yadcf.init(oTable,
      [
        {
          column_number : 0,
          filter_type: "range_date",
        },
        {
          column_number : 1,
          filter_type: "select",
          select_type: "select2",
        },

/platform/activities/?format=datatables returns this response:

{"recordsTotal":23286,"recordsFiltered":23286,
"data":[{"id":104,"activity_type":"Option 1",}],
"draw":1,
"options":{"activity_type":[{"label":"Option 1","value":1},
                            {"label":"Option 2","value":2},]}}

Currently, the filter for activity_type column (column 1) returns only Option 1 , because that's all that's available in the table. Therefore, I want to point the filter to the activity_type array in the options portion of the Ajax response. I know there is the data option that can be set for each filter, but I'm unsure how to point it to the key:value array. When I put console.log(yadcf_data_2); outside of the DataTable construction, it just prints undefined . This is being printed prior to the console call in the ajax function.

How would I tie YADCF to the activity_type array?

Take a look at the yadcf showcase server page , you should send the yadcf_data_0 / yadcf_data_1 along with the recordsTotal / data

You also can debug the xhr event inside the yadcf code to see whats going on

//In case that you want to populate your select / auto_complete filters with values

//you have to add to your current JSON the following attributes yadcf_data_0 / yadcf_data_1 / etc'

//where each attribute contains a list of strings

//For example:

//"yadcf_data_0":["KHTML","Webkit","Trident","Misc","Other browsers","Tasman","Presto","Gecko"],

//"yadcf_data_1":["Nintendo DS browser",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM