0

How could I get columns structure automatically with this kind of datatable? The idea is that there will be different number and order of columns depending of the params that I send to the server using the ajax data source with objects.

(I only need the base names of the columns, I don't need the translated ones)

here is my code simplified:

var tableId = 'tableId';
var tableName = jQuery('#' + tableId).DataTable({
    language: dataTablesTexts,
    pagingType: 'full',
    lengthMenu: [
        [5, 10, 25, 50, -1], 
        [5, 10, 25, 50, 'All'],
    ],
    searching: true,
    ordering: true,
    aaSorting: [],
    paging: true,
    select: false,
    info: true,
    responsive: true,
    ajax: {
        type: 'POST',
        url: '/historic/getTasks',
        data: function (d) {
            return $.extend( {}, d, {
                token: localStorage.token,
                params: params
            });
        },
    dataSrc: ""
    },
    columns: [{
        data: 'name',
        title: lang.name
    }, {
        data: 'description',
        title: lang.description
    }, {
        data: 'edit',
        title: ''
    }]
});
2
  • Possible duplicate of DataTables - Dynamic Columns From Ajax Data Source? Commented Aug 1, 2019 at 11:39
  • not really, in this case datasource is relevant (with array method is easy to loop around data and get the names for the columns, but in object source from ajax is different, Commented Aug 1, 2019 at 12:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.