24sharon
24sharon

Reputation: 1975

jQuery Datatables Export Buttons Dynamic file name and title

I use jQuery datatable and Export buttons.

I change the filename and the title.

My problem is that I want the title to be dynamic, based on the filters and custom filters.

My problem is that The file name configured on the first load only

           .withButtons([
                    {
                            extend: "excelHtml5",
                            filename :$scope.getFileName(false),
                            title:$scope.getFileName(true),
                            exportOptions: {
                                    columns: ':visible'
                            },
                            //CharSet: "utf8",
                            exportData: { decodeEntities: true}

                    },

Upvotes: 1

Views: 3415

Answers (1)

davidkonrad
davidkonrad

Reputation: 85528

Of course $scope.getFileName() is only called once, or the number of times you intentionally call it. Go the other way around : Use the init() function along with a $watch :

.withButtons([{
  extend: "excelHtml5",
  //filename: $scope.getFileName(false),
  //title: $scope.getFileName(true),
  exportOptions: {
    columns: ':visible'
  },
  //CharSet: "utf8",
  exportData: {
    decodeEntities: true
  },

  init: function(dt, node, config) {
     $scope.watch('fileName', function()  
        //config.filename is in fact config.title + config.extension
        config.title = $scope.fileName.title;
        config.extension = $scope.fileName.extension;
     })
  }

}])

You are not explaining why you need to use a $scope.getFileName() despite you are using angular where such things is easily automated. With the above you can now update a fileName struct on $scope whenever it is needed, and the export filename will change accordingly

$scope.fileName = {
   extension: '.xls',
   title: 'my-table-export'
}

Upvotes: 1

Related Questions