禁用指定列的angularjs数据表排序

时间:2017-06-12 14:23:05

标签: javascript jquery angularjs sorting

我正在使用angularjs数据表,我不需要对所有列进行排序。所以我想禁用指定列的排序。我想禁用列号的排序。以下情况为2和4。

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 个答案:

答案 0 :(得分:1)

今天我遇到了同样的问题,这就是我所发现的;就我而言,我想从第一列中删除排序功能,因为它仅包含一个复选框。

根据官方文档,这是代码块;

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

但这是行不通的;然后,我发现即使禁用了列的排序,dataTables的排序order仍然保留。默认情况下,顺序为[0, 'asc']。因此,您需要另外设置order来定位其他一些列。

因此,完整的html + angular代码如下;

HTML

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

角度

app.controller('exceptionViewCtrl', ['$scope', 'DTColumnDefBuilder', 'DTOptionsBuilder', function ($scope, DTColumnDefBuilder, DTOptionsBuilder) {
    $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('order', [1, 'asc']);
    $scope.dtColumnDefs = [DTColumnDefBuilder.newColumnDef(0).notSortable()];
}]);

答案 1 :(得分:0)

试一试:

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