Storing table-scope property in dataset

Storing table-scope property in dataset

radioradio Posts: 3Questions: 1Answers: 0

Howdy, I have a following dataset structure as example, which is being returned from my custom ajax:

{
globalFieldA: gfA,
globalFieldB: gfB,
    rowData: [{
        row1Name: name1,
        row1Prop: prop1
    }, 
    {
       row2Name: name2,
       row2Prop: prop2
    }]
}

Now, the question is:
how can I feed my datatable with given global fields globalFieldA, globalFieldB to make it accesible to footer callback, with table rows feeding off the rowData? I would like to avoid to store the dataset in intermediate structure to only have a single point of truth in datatables themselves trough all my processes. Thanks in advance!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin

    Assuming you have it being loaded by custom Ajax, do you have a reference to the object? Perhaps you can show your initialisation code?

    Allan

  • radioradio Posts: 3Questions: 1Answers: 0

    Sure thing, here is my initialisation, i have several instances of it on the page:

     function initDataTable($table, dataset) {
                $table.data("externally-supplied-data", dataset.externallySuppliedData);
                var $apiDeferred = $.Deferred();
                var tableLangPrefs = s4globalVariables.s4translations.dataTables;
                var oTable = $table.dataTable({
                    "dom": "<'row' <'col-xs-12'T>>" +
                    "<'row'<'col-xs-6'l><'col-xs-6 nopadding dataTable-filters pull-right'f>r>" +
                    "<'table-scrollable't><'row'<'col-xs-2 nopadding datatable-footer-left-col'>" +
                    "<'col-xs-5 col-xs-offset-1 nopadding text-center'p><'col-xs-3 nopadding pull-right datatable-footer-right-col'>>" +
                    "<'row'<'col-xs-12 nopadding margin-top-10'i>>",
                    "language": tableLangPrefs,
                    "order": [[1, 'asc']],
                    "data": dataset.items,
                    "search": {
                        "smart": true
                    },
                    "lengthMenu": [[15, 25, 50, 100,200, -1], [15, 25, 50, 100,200, tableLangPrefs.pagingSizeAll]],
                    "pageLength": 200,
                    "autoWidth": false,
                    "deferRender": true,
                    "processing": false,
                    "columnDefs": [...omitted...]
    ...
                    "footerCallback": function (row, data, start, end, display) {
                        var api = this.api();
    //this one is being returned from several ajax events and is not calculated from table data
                        var $total = $(templates.totalRowCompiledTemplate(dataset.totalFormatted));
                        $(api.column(7).footer()).html($total);
                    }
    
    

    here I am using $table.data, for example, but I find it kinda unsettling to have the row data in datatables data layer and several related to the whole table ones on jquery.data().

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin

    You have the dataset property, so you should be able to access that in the footerCallback - e.g. dataset.globalFieldA. Or have I missed the point?

    Allan

  • radioradio Posts: 3Questions: 1Answers: 0

    True, I do have access, of course, but the given dataset is only used during initialisation, afterwards ajax events do directly change the data in tables themselves. Since footerCallback fires on draw event, it would mean that footer would be just redrawn with initial data.

    So, my option would be to either write on ajax event into datatables AND mutate inital dataset, so footer reads from there (what could create a confusion), to put the needed global field onto every single row or to use additional jquery.data field, as I do right now.

    Unfortunately, there is no way to direclty set the data for headers/footers explicitly, as far I understand, right?

    Cheers

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin
    Answer ✓

    I don't quite get what you mean I'm afraid - specifically about the ajax events changing the data. Do you mean they use things like row().data() to modify the data in the table, and thus the information in the dataset would no longer be in-sync?

    If that is the case, do those ajax events get the updated data for globalFieldA etc as well? If so, can you write them into a global variable that can be accessed by the footer callback?

    Unfortunately, there is no way to direclty set the data for headers/footers explicitly, as far I understand, right?

    Currently no - sorry. That is something that will be added in the next major version.

    Allan

This discussion has been closed.