【问题标题】:jQuery dataTables 1.10.5 custom propertiesjQuery dataTables 1.10.5 自定义属性
【发布时间】:2015-05-16 11:48:50
【问题描述】:

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

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

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

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

但是我得到:未捕获的类型错误:无法读取未定义的属性“customProp”

有人知道我该怎么做吗?

【问题讨论】:

    标签: jquery jquery-datatables datatables-1.10


    【解决方案1】:

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

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

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

    $('.test').on('click', function(e){
        console.log($('#table').data('customProp'));
    }
    

    【讨论】:

      【解决方案2】:

      出于某种原因,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 淘汰。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2014-11-07
          • 1970-01-01
          • 1970-01-01
          • 2017-03-26
          • 2015-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-13
          相关资源
          最近更新 更多