改变 jquery 数据表 aoColumns
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13671552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Altering jquery datatables aoColumns
提问by h0ru5
I am using uptil now 3 datatables in jquery-ui tabs. i prefer to keep them all in background and really use 3 tables. The data comes via ajax in arrays of objects (is used in that format also by other presenters).
我现在在 jquery-ui 选项卡中使用了 3 个数据表。我更喜欢将它们全部保留在后台并真正使用 3 个表。数据通过对象数组中的 ajax 获得(其他演示者也以该格式使用)。
However, since the number and type of tables could change and should be kept easy to maintain.
但是,由于表的数量和类型可能会发生变化,因此应保持易于维护。
The problem now is to elegently use the same constructor. Can I set the aoColumns as a parameter only and not also re-set it at runtime?
现在的问题是优雅地使用相同的构造函数。我可以将 aoColumns 仅设置为参数而不是在运行时重新设置吗?
Right now, the Code looks like this:
现在,代码如下所示:
tables=$(".dat").dataTable({
"bJQueryUI": true,
"bDeferRender": true,
"bPaginate": false,
"bProcessing": true,
"sAjaxSource": "ajaxdataA.json",
"ssAjaxDataProp" : "data",
"aoColumns": [{"mData": "col1"}, {"mData": "col2"},{"mData": "col3"}]
});
I would like to set that as an array and then use specifics for the sAjaxSource and aoDataColumns for the individual tables. It could of course also be achieved by storing the parameter object and extending it or using a factory closure. I also thought on using AngularJS instead.
我想将它设置为一个数组,然后为各个表使用 sAjaxSource 和 aoDataColumns 的细节。当然也可以通过存储参数对象并扩展它或使用工厂闭包来实现。我也考虑过使用 AngularJS。
Does anybody have a more elegant solution?
有人有更优雅的解决方案吗?
采纳答案by Romias
I think you can use the aoColumnDef
instead of using aoColumns
.
我认为你可以使用aoColumnDef
而不是使用aoColumns
.
aoColumnDefcan even read some css classes in your columns to configure DataTables in a more dynamic way.
aoColumnDef甚至可以读取列中的一些 css 类,以更动态的方式配置 DataTables。
Take a look at its documentation: http://www.datatables.net/usage/columns