如何使用Select2设置数据表长度选择元素的样式

时间:2013-05-16 06:16:57

标签: jquery datatables

我正在使用jquery datatables插件和jquery Select2插件。我尝试用我自己的类重写css类但它没有用。它仍然显示默认的操作系统/浏览器样式。

3 个答案:

答案 0 :(得分:1)

您还可以使用DataTables' fnInitComplete回调,在初始化DataTables时调用。请参阅其文档中的Callbacks页面。

以下是扩展DataTable默认值以完成此操作的示例:

$.extend($.fn.dataTable.defaults, {
  "bPaginate": true,                /* just an example. can be false or removed altogether */
  "bLengthChange": true,            /* another example. */

  /* ... other options ... */

  "fnInitComplete": function(oSettings, json) {
    $(".dataTables_length .select2").select2({ 
      /* select2 options, as an example */
      minimumResultsForSearch: -1 
    });
  },
});

答案 1 :(得分:0)

在表格加载后,在select元素上调用select2。

$(document).ready(function(){

//加载数据表    jQuery的( '#data_table')。dataTable中({     ....做设置和其他东西    });

$('div.dataTables_length select')。select2();

});

答案 2 :(得分:0)

对于之前的活动,请使用preInit

$(document).ready(function () {
  var dataTable = $('#datatables').DataTable({
  //your datatables settings
  });
});

$(document).on( 'preInit.dt', function (settings, json) {
    $('div.dataTables_length select').select2();
});

这样,你的select就会在datatables上的init事件完成之前启动...如果你因为通过Ajax加载数据而有延迟,那么这可能会更好。

https://datatables.net/reference/event/preInit