Sunday, 23 September 2018

Updating datatables with rows.add + aoColumnDefs?

I'm populating 2 datatables with 1 ajax call. That's 2 datatable populations wrapped in a jQuery.ajax. For that reason, I have to use DataTable().clear().rows.add to update them. Here's what the initial population looks like:

pojQuery(function(){
    jQuery.ajax({
        url: '/api/?action=getStats',
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            if(data.data.instances[0].Strategy != "None yet"){
                anyInstance = true;
                tableInterval = 4000;
            }
            console.log(data);
            jQuery("#tablepress-1").DataTable({
                "bProcessing": true,

                //url:'/api/?action=getStats',
                data: data.data.instances,
                dataSrc: 'data',
                "deferRender": true,

                "columns": [
                    { "data": "Item 1" },
                    { "data": "Item 2" },
                    { "data": "Item 3" },
                    { "data": "Item 4" },
                    { "data": "Item 5" },
                    //{ "data": "ID" }
                ],
                "aoColumnDefs": [
                    {
                        "aTargets": [5],
                        "mData": "ID",
                        "mRender": function (data, type, full) {
                            if(anyInstance){
                                return '<a href="javascript:void(0)" onclick="return false;"><i class="fas fa-trash delIns" value="' + data + '"></i></a>';
                            }else{
                                return '...';
                            }

                        }
                    }
                ]
            })
            //var header1 = "<th>Item 1</th><th>Item 2</th><th>Item 3</th><th>Item 4</th><th>Item 5</th>";
            //jQuery("#tablepress-1").append(header1);
            var instanceTable = jQuery('#tablepress-1 th');
            instanceTable.eq(0).text( "Item 1" );
            instanceTable.eq(1).text( "Item 2" );
            instanceTable.eq(2).text( "Item 3" );
            instanceTable.eq(3).text( "Item 4" );
            instanceTable.eq(4).text( "Item 5" );


            jQuery("#tablepress-2").DataTable({
                "bProcessing": true,
                //url:'/api/?action=getUserTrades',
                data: data.data.trades,
                dataSrc: 'data',
                "deferRender": true,

                "columns": [
                    { "data": "Item 1" },
                    { "data": "Item 2" },
                    { "data": "Item 3" },
                    { "data": "Item 4" },
                    { "data": "Item 5" },
                    { "data": "Item 6" },
                    { "data": "Item 7" }
                ]
            })
            var tradesTable = jQuery('#tablepress-2 th');
            tradesTable.eq(0).text( "Item 1" );
            tradesTable.eq(1).text( "Item 2" );
            tradesTable.eq(2).text( "Item 3" );
            tradesTable.eq(3).text( "Item 4" );
            tradesTable.eq(4).text( "Item 5" );
            tradesTable.eq(5).text( "Item 6" );
            tradesTable.eq(6).text( "Item 7" );

            updateWidgets(data)
        }
    });
})

(I changed some of the values for privacy)

I'm using aoColumnDefs, as you can see. That's how I display an action icon for deleting rows. Here's how I update the data periodically:

function updateTables(){
    jQuery.ajax({
        url: '/api/?action=getStats',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            jQuery('#tablepress-1').DataTable().clear().rows.add(data.data.instances).draw();
            jQuery('#tablepress-2').DataTable().clear().rows.add(data.data.trades).draw();

            updateWidgets(data)
        }

    })
}

I call that on a timer and anywhere else needed. The problem is, my action column I populated with aoColumnDefs isn't defined anywhere in that. So when the table updates, all my rows update except for that one. How to do it?



from Updating datatables with rows.add + aoColumnDefs?

No comments:

Post a Comment