0

I am working with angularjs data table where I don't need sorting for all the columns. So I want to disable sorting for specified columns. I want to disable sorting for column no. 2 and 4 for the below case.

var app = angular.module('myApp',['datatables']);
app.controller('MyCtrl', function($scope,DTOptionsBuilder,DTColumnBuilder) {

    $scope.list = [
        {"eid":"10","ename":"nam1","sales":"20"},
        {"eid":"20","ename":"nam2","sales":"20"},
        {"eid":"30","ename":"nam3","sales":"20"},
        {"eid":"40","ename":"nam4","sales":"20"}
    ];
    $scope.vm = {};

$scope.vm.dtOptions = DTOptionsBuilder.newOptions()
      .withOption('order', [0, 'asc']);
$scope.vm.dtColumnDefs = [
   DTColumnDefBuilder.newColumnDef(1).notSortable(),
   DTColumnDefBuilder.newColumnDef(3).notSortable()
];

});
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script src="http://phpflow.com/demo/angular_datatable_demo/angular-datatables.min.js"></script>
  <script src="http://phpflow.com/demo/angular_datatable_demo/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <link rel="stylesheet" href="http://phpflow.com/demo/angular_datatable_demo/datatables.bootstrap.css"> 
</head>
<div class="container">
<div ng-app="myApp" ng-controller="MyCtrl">
<table  class="table table-striped table-bordered" dt-options="vm.dtOptions" dt-column-defs="vm.dtColumnDefs" datatable="ng">
    <thead>
      <tr>
	 <th>Employee ID</th>
	<th>name</th>
	<th>sales</th>
	<th>details</th>

</thead>
    <tbody>
  
   <tr ng-repeat="data in list">
      <td> {{ data.eid }} </td>
      <td> {{ data.ename }} </td>
      <td> {{ data.sales }} </td>
      <td>view</td>
</tr>
</tbody>
</table>
</div>

2 Answers 2

1

Today I got the same issue and here is the what I have found; In my case I wanted to remove sorting feature from first column since it just contain a check box.

According to the official documentation this is the code block;

app.controller('exceptionViewCtrl', ['$scope', 'DTColumnDefBuilder',  function ($scope, $routeParams, DTColumnDefBuilder) {
    $scope.dtColumnDefs = [DTColumnDefBuilder.newColumnDef(0).notSortable()];
}]);

But this is not worked; Then I figure out even if I disable sorting for a column, the dataTables sort order still remain. By default order is [0, 'asc']. So you need to additionally set order to target some other column instead.

So the complete html+angular code as follows;

HTML

<table datatable="" id="example2" class="table table-bordered table-hover" dt-options="dtOptions" dt-column-defs="dtColumnDefs">
     // table data
</table>

Angular

app.controller('exceptionViewCtrl', ['$scope', 'DTColumnDefBuilder', 'DTOptionsBuilder', function ($scope, DTColumnDefBuilder, DTOptionsBuilder) {
    $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('order', [1, 'asc']);
    $scope.dtColumnDefs = [DTColumnDefBuilder.newColumnDef(0).notSortable()];
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

try it:

    $scope.vm.dtOptions = DTOptionsBuilder.newOptions()
          .withOption('order', [0, 'asc']);
    $scope.vm.dtColumnDefs = [
       DTColumnDefBuilder.newColumnDef(1).notSortable(),
       DTColumnDefBuilder.newColumnDef(3).notSortable()
    ];
});

3 Comments

i have edited my question with your answer but now i am getting DTColumnDefBuilder is not defined error!so it will be very much helpful if your can provide a code snippet with your answer.
still getting Error: DTColumnDefBuilder is not defined
update your alias name defined in the controller section from DTColumnBuilder to DTColumnDefBuilder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.