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

Custon option DisplayLength does not work in serverSide when stateSave

Open thiagogarbazza opened this issue 9 years ago • 1 comments

What versions you are using?

  • angular version: 1.5.7
  • jquery version: 2.2.4
  • datatables version: 1.10.13
  • angular-datatables version: 0.5.7

What's the problem?

When we set the default value of stateSave parameter to true and set any value to the displayLength parameter

If in the controller we set the serverSide parameter to true, no matter how we customize the value of the displayLength parameter, it is never used. The default value is always applied.

Example configuration default values for datatables

angular
  .module('appModule')
  .run(function dataTablesConfigRun(DTDefaultOptions) {
    DTDefaultOptions.setDisplayLength(5); // global default display length
    DTDefaultOptions.setOption('lengthMenu', [5, 10, 15, 20, 25, 30, 50, 100, 500, 1000]);
    DTDefaultOptions.setOption('stateSave', true); // Error occurs when stateSave is TRUE
  });

Example view Controller using datatables

  angular
    .module('appModule')
    .controller('appController', function appController($http, $scope, $compile, 
        DTColumnBuilder, DTOptionsBuilder) {
      var vm = this;

      activated();

      function activated() {
        loadData()
        vm.dtColumns = createColumns();
      }

      function createColumns() {
        return [
          DTColumnBuilder.newColumn("email"),
          DTColumnBuilder.newColumn("name.first")
        ];
      }

      function createOptions() {
        return DTOptionsBuilder.newOptions()
          .withDisplayLength(20) // custon display length  
          .withOption('serverSide', true)
          .withOption('createdRow', createdRow)
          .withFnServerData(serverData);
      }

      function loadData() {
        return $http.get('https://randomuser.me/api/?results=100')
          .then(function(response) {
            vm.employers = response.data.results;
            vm.dtOptions = createOptions();
          });
      }

      function serverData(sSource, aoData, fnCallback) {
        var draw = aoData[0].value;
        var order = aoData[2].value;
        var start = draw === 1 ? 0 : aoData[3].value;
        var length = aoData[4].value; // Always with default value (5)

        var records = {
          data: vm.employers.slice(start, start + length),
          draw: draw,
          recordsFiltered: vm.employers.length,
          recordsTotal: vm.employers.length
        };

        fnCallback(records);
      }

      function createdRow(row) {
        $compile(angular.element(row).contents())($scope);
      }
    });

To see a running example, go to jsfiddle.

thiagogarbazza avatar Feb 20 '17 13:02 thiagogarbazza

It's because you are fetching the data as the datatables is rendering. So that happens:

  • table is rendering with 0 data and with the default options (so with stateSave set to true and default length set to 5)
  • fetching the data and setting the options of the table
  • re-rendering the datatable with the new data, but since the stateSave is on, then the default length is still 5

One way is to add a flag to render the table to ensure your data is fetched.

See the fixed jsfiddle.

l-lin avatar Feb 23 '17 08:02 l-lin