question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular datatables: disable sorting not working for multiple tables

See original GitHub issue

What versions you are using?

  • angular version:1.2.15
  • jquery version:1.10.1
  • angular-datatables version:0.5.0

What’s the problem?

I am working with multiple data tables and generating a new table every time user selects an option from the drop down list.Based on the user’s selection i make an $http request to fetch new data from the database .For every table i have different dtColumnDefs which is set dynamically since my table column headers are dynamic except 1st two and last columns .My intention is to disable sorting for these column headers.I am able to find out total number of dynamic column then run a loop to dynamically disable those columns,Though the data table renders successfully for every user input but expected columns sorting options are not getting disabled. Check the plunker

Can you share your code?

   $scope.depshow=function(){
    $scope.dep=$scope.selected_dep;
      if($scope.dep==1)
    {
            $scope.list = [
                {"eid":"10","dyn1":"adval1","dyn2":"dval2","dyn3":"dval3","sales":"20"},
               
                {"eid":"20","dyn1":"adval2","dyn2":"dval2","dyn3":"dval3","sales":"20"},
                {"eid":"30","dyn1":"adval3","dyn2":"dval2","dyn3":"dval3","sales":"20"}
            ];
          }  
            else if($scope.dep==2)
            {
            $scope.list = [
                {"eid":"40","dyn1":"bdval1","dyn2":"dval2","dyn3":"dval3","sales":"20"},
               
                {"eid":"50","dyn1":"bdval2","dyn2":"dval2","dyn3":"dval3","sales":"20"},
                {"eid":"60","dyn1":"bdval3","dyn2":"dval2","dyn3":"dval3","sales":"20"}
            ];
            }
     if($scope.dep==3)       {
            $scope.list = [
                {"eid":"70","dyn1":"ccdval1","dyn2":"dval2","dyn3":"dval3","sales":"20"},
               
                {"eid":"80","dyn1":"cdval2","dyn2":"dval2","dyn3":"dval3","sales":"20"},
                {"eid":"0","dyn1":"cdval3","dyn2":"dval2","dyn3":"dval3","sales":"20"}
            ];
    }
   // i was getting data table can not be reinitialized so tried to make  initialization dynamic by counting      //function call
   $scope.i+=1;
    var x='v'+$scope.i;
    $scope.m='v'+$scope.i;
  
     $scope.x = {};
    $scope.x.dtOptions = DTOptionsBuilder.newOptions()
              .withOption('order', [0, 'asc']);
    //this length is dynamic ,used to determine how many dynamic columns are there
    $scope.ln=4;
    

    $scope.x.dtColumnDefs = [];
    for(var i=1;i<$scope.ln;i++){  
     $scope.x.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(i).notSortable());
    }  

  }

`

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
l-lincommented, Jul 10, 2017

Here an example using directives: http://plnkr.co/edit/tDnp4s3eaMcaO8ZwVqZc?p=preview

I created a directive that contains the options of the datatables along with the list of data to be displayed and your $scope.ln:

angular.module('datatablesSampleApp', ['datatables']).directive('sampleTable', function() {
    return {
      restrict: 'E',
      scope: {
        list: '=',
        ln: '='
      },
      templateUrl: 'sampleTable.html',
      controller: 'tableCtrl'
    };
  }).controller('tableCtrl', function($scope, DTOptionsBuilder, DTColumnDefBuilder) {
    $scope.dtOptions = DTOptionsBuilder.newOptions()
      .withOption('order', [0, 'asc']);
    $scope.dtColumnDefs = [];

    for (var i = 1; i < $scope.ln; i++) {
      $scope.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(i).notSortable());
    }
  });

In the HTML that calls the new directive:

<div ng-if="displayTable">
      <sample-table list="list" ln="ln"></sample-table>
    </div>

This flag displayTable is necessarily in order to re-render the table each time we switch the list.

Finally, in your main controller:

angular.module('datatablesSampleApp', ['datatables']).
  controller('sampleCtrl', function($scope, $timeout) {
    $scope.displayTable = false;
    $scope.department = [{
      value: "1",
      name: "sales"
    }, {
      value: "2",
      name: "admin"
    }, {
      value: "3",
      name: "other"
    }];

    $scope.dep = $scope.selected_dep;

    $scope.depshow = function() {
      $scope.displayTable = false;
      $scope.dep = $scope.selected_dep;
      if ($scope.dep == 1) {
        $scope.list = [{
           ...
        ];
        $scope.ln = 2;
      } else if ($scope.dep == 2) {
        $scope.list = [{
            ...
        ];
        $scope.ln = 3;
      }
      if ($scope.dep == 3) {
        $scope.list = [{
           ...
          }
        ];
        $scope.ln = 4;
      }
      // We need to "hack" this way in order to trigger the digest loop so that
      // ngIf will destroy its content, thus re-rendering the directive
      $timeout(function() {
        $scope.displayTable = true;
      }, 0);
    };

We need to manipulate the flag displayTable so angular will destroy and re-render the content. It’s a bit hackish, but it does the job.

0reactions
hubcoderscommented, Jul 8, 2017

@l-lin I can’t explain how eagerly i was waiting for your answer as i am stuck in this issue for last 15 days and no one could help me to solve the issue and you are the last light for me so thanks for managing some time for me.

In my plunker $scope.ln; is the length of dynamic columns for which i want to disable sorting .The $scope.ln; is not fixed and can be any number as it is dynamic .Kindly look to my updated plunker where the table is giving re initialization error on the 3rd call to the function. Up to the 2nd function call it is working fine…

you are advising to render a new table each time.can you update my plunker with your preferred way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable column sorting not working for multiple angularjs ...
I am able to find out total number of dynamic columns then run a loop to dynamically disable sorting on those columns.Though the...
Read more >
Disable sorting of one column — DataTables forums
The columns.orderable option simply allows or disallows the user from sorting that column. It does not stop you, the developer, from sorting ...
Read more >
Applying Sort Option and Disable Select Column
You probably just missed the proper syntax and coma after "columnDefs" assignment. Keep in mind that if you want to set different column...
Read more >
Remove Sorting from Specific Column - DataTables - Makitweb -
DataTables makes pagination implementation easier. For allowing searching it adds a search box and also adds an up & down arrow on the ......
Read more >
DataTables Js Enable Disable Sorting - YouTube
Hi friends, In this video, you will learn how to Enable / Disable Sorting, also how to Enable / Disable particular column for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found