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

dynamic columns with withFnServerData

Open plane11 opened this issue 11 years ago • 5 comments

Hello, I want fill dtColumns and dtOptions with my ajax call result.

$scope.dtOptions = DTOptionsBuilder.fromSource("sample/json")
            .withFnServerData(function (sSource, aoData, fnCallback, oSettings) {
                oSettings.jqXHR = $.ajax( {
                    'dataType': 'json',
                    'type': 'POST',
                    'url': sSource,
                    'data': aoData,
                    'success': function(json){   
                        angular.forEach(json.headers, function(value, key){
                            $scope.dtColumns.push(DTColumnBuilder.newColumn(value).withTitle(value).withClass("center").withOption("width", "100px"));
                        });
                        fnCallback(json.data);
                    }
                });
        });
$scope.dtOptions = DTOptionsBuilder.fromSource("sample/json")
            .withFnServerData(function (sSource, aoData, fnCallback, oSettings) {
                oSettings.jqXHR = $.ajax( {
                    'dataType': 'json',
                    'type': 'POST',
                    'url': sSource,
                    'data': aoData,
                    'success': function(json){   
                        angular.forEach(json.headers, function(value, key){
                            $scope.dtColumns.push(DTColumnBuilder.newColumn(value).withTitle(value).withClass("center").withOption("width", "100px"));
                        });

                        $scope.dtOptions.reloadData();
                        fnCallback(json.data);
                    }
                });
        });
$scope.dtColumns =    DTColumnBuilder.newColumn('name').withTitle('Status').withClass("center").notSortable().withOption("width", "15%");

According to 2nd Code, Header Column is not reload. Just 1(Status) exist.

How can i set headers and datas with ajax data?

plane11 avatar Dec 17 '14 07:12 plane11

Your code is not completed (where ddtype comes from?). Can you correct them please?

l-lin avatar Dec 17 '14 08:12 l-lin

Sorry, I modified wrong part.

plane11 avatar Dec 17 '14 08:12 plane11

It seems you want to change the header dynamically. There is already a bug report #149 about this.

l-lin avatar Dec 17 '14 13:12 l-lin

Thank you, I'll try it.

plane11 avatar Dec 18 '14 00:12 plane11

if server return empty result,how can i setting an empty datatable?

q640202047 avatar Jan 04 '18 07:01 q640202047