自定义属性jquery Datatables

Custom attribute jquery Datatables

本文关键字:Datatables jquery 自定义属性      更新时间:2024-06-29

我需要为我的数据表实例创建一个自定义属性,并且我需要保留这个值,例如:

当我创建一个实例时:

$('#table').dataTable({
    //common attributes
    ajax: 'url.json',
    columns: [...],
    //custom attribute
    hasSomeValueHere: 'helloword'
});

我想把它保存在数据表的设置中,所以如果我检查它会在那里:

$('#table').dataTable({
    //common attributes
    ...
    fnDrawCallback: function(oSettings){
        alert(oSettings.hasSomeValueHere); //helloword
    }
});

有没有办法用这种方式扩展数据表?感谢

您不需要为此扩展dataTables。oSettings有一个对象oInit,它保存整个初始化对象,即dataTables选项。示例:

$('#example').dataTable({
    hasSomeValueHere: 'helloworld',
    fnDrawCallback: function(oSettings){
        alert(oSettings.oInit.hasSomeValueHere); //helloworld
    }
});

演示->http://jsfiddle.net/bvr2jk8z/


这也适用于1.10.x,使用DataTable()

$('#example').DataTable({
    hasSomeValueHere: 'helloworld',
    drawCallback: function(settings){
        alert(settings.oInit.hasSomeValueHere); //helloworld
    }
});

演示->http://jsfiddle.net/fkbtv1x7/