Altering jquery datatables aoColumns

2019-06-25 14:04发布

问题:

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).

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?

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.

Does anybody have a more elegant solution?

回答1:

I think you can use the aoColumnDef instead of using aoColumns.

aoColumnDef can even read some css classes in your columns to configure DataTables in a more dynamic way.

Take a look at its documentation: http://www.datatables.net/usage/columns