angular-datatables icon indicating copy to clipboard operation
angular-datatables copied to clipboard

Multiple Tabs - changeData and reloadData issue

Open rnunez100 opened this issue 9 years ago • 2 comments

What versions you are using?

  • angular version: v1.5.0
  • angular-datatables version: v0.5.3

What's the problem?

I have a page with 10 different tabs, each tab contains one responsive DataTable [each has child rows] . On one of the tabs, I have a button that will sort that DataTable with the new json data requested. When the user clicks on the button to retrieve the new data for that DataTable, [ng-click] makes a call to a function in the controller.

Issue: In the function, I tried to run the changeData() function to reload the new json data, but instead the view_orders.json file is loaded again instead of 'orders/getProducts.json'. I want the dataTable to load the new data. Also I've looked at http://l-lin.github.io/angular-datatables/archives/#!/dataReloadWithAjax tried implementing the same way as the example and doesn't work either.

Can you share your code?

HTML

<button ng-click="filterAdminForm();" type="button" class="btn btn-sm btn-primary"
ng-disabled="archiveForm.$invalid"> Show Orders </button>

<table id="example" datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns"
dt-column-defs="showCase.dtColumnDefs" dt-instance="showCase.dtInstanceCallback" 
class="table dataTables-example order-listing-data-table"></table>

Controller

else{
            vm.dtInstance = {};
            vm.dtInstances = [];
            vm.dtOptions = DTOptionsBuilder.fromSource('orders/view_orders.json')
                .withOption('language', {zeroRecords : '<span style="color:red;">No Orders Found!</span>'})
                .withOption('lengthChange', false)
                .withOption('paging',false)
                .withFnServerData(serverData)
                .withOption('order', [0, 'desc'])
                .withOption('deferRender', true)
                .withOption('createdRow', createdRow)
                .withOption('responsive', {
                    details: {
                        display: $.fn.dataTable.Responsive.display.childRowImmediate,
                        type: false,
                        renderer: function (api, rowIdx, columns) {
                            var data = $.map(columns, function(col, i) {
                                var title = col.title != '' ? '<label class="labelsz">' + col.title + '</label>' : '';
                                if(col.hidden){
                                    switch(col.title){
                                        case 'Asgn to:':
                                            return '<div class="col-md-12"><div class="col-md-3"><div class="col-md-6"><td>' + title + col.data + '</td></div>';
                                            break;
                                        case 'Order Name:':
                                            return '<div class="col-md-6"><td>' + title + col.data + '</td></div></div>';
                                            break;
                                        default:
                                             return '<td>' + title + col.data + '</td>';
                                    }
                                }
                                else{
                                    return '';
                                }
                            }).join('');

                            return data ?
                            $compile(angular.element($('<table class="col-md-12" data-dtr-index="'+rowIdx+'"/>').append(data)))($scope) :
                            false;
                        }
                    }
                })
                .withDOM('lTfgitp')
            vm.dtColumns = [/*This is where I build my columns*/];

            vm.dtInstanceCallback = function (instance){
                vm.dtInstance = instance;
            }
        
            function createdRow(row, data, dataIndex) {
                // Assigns the color to the row/order 
                $(row).addClass(data.color);

                // Recompiling so we can bind Angular directive to the DT
                $compile(angular.element(row).contents())($scope);
            }
            function serverData(sSource, aoData, fnCallback, oSettings) {
                oSettings.jqXHR = $.ajax({
                    'dataType': 'json',
                    'type': 'POST',
                    'async': false,
                    'url': 'orders/view_orders.json',
                    'data': {position:$scope.position},
                    'success': fnCallback
                });
            }
}


    $scope.filterAdminForm = function() {
        vm.dtInstance.changeData('orders/getProducts.json');
    };

rnunez100 avatar Aug 25 '17 17:08 rnunez100

If you use only on tab, does your table update with the correct data?

l-lin avatar Sep 03 '17 10:09 l-lin

Issue Resolved. The reason that view_orders.json was loading instead of get_products.json was, because the 'serverData' function always gets loaded upon reloading the table. What I did was just make a conditional statement in the 'serverData' function for each json call and used rerender() instead of changeData.

rnunez100 avatar Sep 06 '17 21:09 rnunez100