4

我正在尝试设置一个自定义属性并能够稍后访问/编辑它。这甚至可能吗,我在旧版本中看到可以使用 fnSettings 但如何在 1.10.5 中使用它?

$('#table').DataTable({
        customProp: 'Hello World'
});

然后单击按钮,我想我可以执行以下操作:

$('.test').on('click', function(e){
      var table = $('#table').DataTable();
      console.log(table.settings.oInit.customProp);
 }

但是我得到: Uncaught TypeError: Cannot read property 'customProp' of undefined

有谁知道我该怎么做?

4

3 回答 3

1

您可以使用 jQuerydata()方法将数据存储在关联元素中。例如:

$('#table').data('customProp', 'Hello World');

稍后您可以检索它,如下所示:

$('.test').on('click', function(e){
    console.log($('#table').data('customProp'));
}
于 2015-03-14T04:32:39.480 回答
1

出于某种原因,table.settings.oInit只能在初始化时访问。初始化后,既不table.settings也不$("#table").DataTable().settings保持oInit(或访问初始化值的函数)。一种解决方法是存储oInit在变量中:

var init;

$('#example').DataTable({
   customProp: 'Hello World',
   initComplete: function(settings) { 
       init = settings.oInit;
   }
});

现在你可以这样做:

alert(init.customProp);

演示(1.10.5)-> http://jsfiddle.net/ajLe1484/

显然,dataTables 在回调中传递了一个对象,并通过表实例传递了另一个以某种方式“清理”的对象。我有点惊讶 dataTables 这样做。也用 1.10.x 对其进行了测试 - 行为是相同的,所以这不是因为oInit已被 1.10.5 淘汰。

于 2015-03-14T08:59:26.040 回答
1

对于 DataTables 1.10.10 和更新版本,以下将起作用:

$('#table').DataTable({
        customProp: 'Hello World'
});

console.log($('#table').DataTable().init().customProp);
于 2017-03-01T10:54:49.907 回答