禁用angularjs数据表中的排序不起作用

时间:2017-06-13 13:45:13

标签: javascript angularjs sorting

我正在使用angularjs数据表,我不需要对所有列进行排序。所以我想禁用指定列的排序。我想禁用列号的排序。以下情况为2和4.但我收到DTColumnDefBuilder is not defined错误。



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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您忘记将DTColumnDefBuilder注入控制器定义函数构造函数。

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

    ...