jquery datatables: columnFilter() is not a function error

空扰寡人 提交于 2019-12-12 11:11:24

问题


i am using Data Tables with custom server side filtering, search and sorting... why is the columnFilter() returning an error "TypeError: $(...).DataTable(...).columnFilter is not a function"

here is how i use columnFilter:

var table = $('#item-table').DataTable({
    ajax: '<?= site_url("price_update"); ?>',
    serverSide: true,
    processing: true,
    paging: true
}).columnFilter();

my code without the ".columnFilter()" works fine.


回答1:


You must use the "oldschool" dataTable() constructor when using columnFilter. Proof of concept :

works not, produces same error as in the question :
columnFilter with 1.10.x instantiated with DataTable() -> http://jsfiddle.net/87kam74q/

works :
columnFilter with 1.10.x instantiated with dataTable() -> http://jsfiddle.net/LvL4vm8e/

The reason is, that columnFilter assumes it is working the "old" jQuery object, not the new API object. Though, you can still access the new API through the .api() method, for example :

var table = $('#example').dataTable();
table.api().search('test').draw();

If you not want to go through table.api() for using the new AP, and insists on using DataTable(), you can achieve the same by giving up the chaining :

var table = $('#example').DataTable();
$('#example').dataTable().columnFilter({
    sPlaceHolder : 'head:before',
    aoColumns: [ { type: "text"},
                 { type: "text"},
                 { type: "text"},
                 { type: "text"},
                 { type: "text"}
               ] 
});

fiddle -> http://jsfiddle.net/qbr01oya/. This does not result in the dataTable being initialized twice (dataTables check for that).



来源:https://stackoverflow.com/questions/26229855/jquery-datatables-columnfilter-is-not-a-function-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!