简体   繁体   中英

YADCF select - with custom options

Have a table with some data loaded from server by provised ajax to DataTable, also have yadcf for filtering on server side. In one of columns i have select2 box, in what i want see only predefined (loaded from server) options (in example on jsfiddle it is opt variable), but options in select2 contains also data from column. How i can disable adding column data, and stay only loaded from server options?

https://jsfiddle.net/6ssh8mg4/

code:

 var opts = [{id:0, text:"Some 1"},{id:1, text:"Some 2"}];
 var table = $("#docTable").DataTable({
            processing: true,
            serverSide: false,
            paging: true,
            ordering: true,
            deferRender: true,
            pageLength: 10,
            lengthMenu: [[10, 50, 100, -1], [10, 50, 100, "All"]],
            columns: [
               { "data": "Identifier", className: "nowrap" },
               { "data": "Name" },
            ]
 });

yadcf.init(table, [
  { column_number: 0, filter_type: "text", style_class: "form_control", filter_default_label: "" },
  {
    column_number: 1, filter_type: "select", select_type: "select2", filter_default_label: "",
    select_type_options: {
      data: opts,
      width: '180px',
      dropdownAutoWidth: true,
    }
  },
]);

You should use the data attribute (in your case with data/label properties), read the docs

Here is a working sample

var opts = [{value:0, label:"Some 1"},{value:1, label:"Some 2"}];
var table = $("#docTable").DataTable({
    processing: true,
    serverSide: false,
    paging: true,
    ordering: true,
    deferRender: true,
    pageLength: 10,
    lengthMenu: [[10, 50, 100, -1], [10, 50, 100, "All"]],
    columns: [
       { "data": "Identifier", className: "nowrap" },
       { "data": "Name" },
    ]
 });

yadcf.init(table, [
    { 
    column_number: 0, filter_type: "text", style_class: "form_control", filter_default_label: "" },
    {
        data: opts,
        column_number: 1, filter_type: "select", select_type: "select2", filter_default_label: "",
        select_type_options: {
            width: '180px',
            dropdownAutoWidth: true,
        }
    },
]);

jsfiddle

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