YADCF选择 - 使用自定义选项

时间:2017-09-04 04:36:05

标签: datatables jquery-select2 yadcf

通过向DataTable提供ajax,从服务器加载一些数据表,在服务器端也有yadcf进行过滤。在其中一列我有select2框,在我想要的只看到预定义(从服务器加载)选项(例如在jsfiddle上它是opt变量),但select2中的选项也包含列中的数据。如何禁用添加列数据,并保持仅从服务器选项加载?

https://jsfiddle.net/6ssh8mg4/

代码:

 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,
    }
  },
]);

1 个答案:

答案 0 :(得分:1)

您应该使用data属性(在您的情况下使用数据/标签属性),阅读文档

这是一个工作样本

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