我在我的一个cakePHP 2.6项目(和DT 1.10.6)中使用tigrang/cakephp-datatable。到目前为止,我已经能够在插件的作者的帮助下成功实现该插件。插件很棒,我想要保留它。
现在,我正在尝试使用vedmack/YADCF插件和我现有的数据表来实现(特别是)列过滤功能,因为我发现官方DT上的示例非常混乱且有限。我在初始化YADCF时遇到错误:
TypeError: oTable.settings is not a function
var instance = oTable.settings()[0].oInstance,
有没有办法让两个插件一起工作?有人试过吗?
以下是使用cakephp-datatable的我的JS for DT:
$('.dataTable').each(function() {
var table = $(this);
var model = table.attr('data-config');
var settings = dataTableSettings[model];
settings['dom'] = 'lrtip';
settings['stateSave'] = true;
settings['stateSaveCallback'] = function (settings, data) {
$.ajax( {...});
};
settings['stateLoadCallback'] = function (settings) {
...
};
table.dataTable(settings);
});
我在上面的代码之后尝试了类似这样的事情以及其他一些尝试来让YADCF工作但我总是遇到同样的错误:
var table = $('.dataTable');
yadcf.init(table, [
{column_number: 0, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
...,
{column_number: 4, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
{column_number: 5, filter_type: "range_number", filter_reset_button_text: false, filter_delay:500},
...,
]);
我在没有其他插件的情况下尝试过这个YADCF代码,它运行完美。所以,我需要帮助修改代码以实现其他插件。
我也尝试了这个(因为DT是使用dataTable()
小写“d”初始化的,但是没有用:
$('.dataTable').each(function() {
...
...
table.dataTable(settings)
.yadcf([
{column_number: 0, ...},
...
]);
});
PS:我在第一个插件的github上也问了这个问题,但我希望yadcf的作者或有经验的人可以帮助我。
答案 0 :(得分:1)
尝试以下操作(删除循环/ this
用法)
var table;
var model = table.attr('data-config');
var settings = dataTableSettings[model];
settings['dom'] = 'lrtip';
settings['stateSave'] = true;
settings['stateSaveCallback'] = function (settings, data) {
// Send an Ajax request to the server with the state object
$.ajax( {
"url": "/cake/Users/save_state",
"data": data,
"dataType": "json",
"type": "POST",
"success": function () {
console.log("ca");
},
});
};
settings['stateLoadCallback'] = function (settings) {
var o;
// Send an Ajax request to the server to get the data. Note that
// this is a synchronous request since the data is expected back from the
// function
$.ajax( {
"url": "/cake/Users/get_state",
"async": false,
"dataType": "json",
"success": function (json) {
o = json;
},
});
return o;
};
table = $('.dataTable').DataTable(settings);
yadcf.init(table, [
{column_number: 0, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
{column_number: 1, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
{column_number: 2, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
{column_number: 3, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
{column_number: 4, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
{column_number: 5, filter_type: "range_number", filter_reset_button_text: false, filter_delay:500},
{column_number: 6, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
]);
或者您可以将当前代码修改为
$('.dataTable').DataTable(settings);
- > return $('.dataTable').DataTable(settings);
dtable('.dataTable');
- > var oTable = dtable('.dataTable');