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

标签 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>

最佳答案

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

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

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

但这没有用;然后我发现,即使我禁用列的排序,数据表排序 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>

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()];
}]);

关于javascript - 禁用指定列的 angularjs 数据表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44501820/

相关文章:

javascript - 在 React 中使用 velocity.js 动画

javascript - 简单代码中奇怪的 jquery bug

javascript - 在 AngularJS View 中使用服务

javascript - 高度扩展时背景颜色渗入主体

javascript - 如何根据索引数组更改元素的状态?

jQuery 未准备好时执行 JavaScript

jquery - Div 相互重叠

javascript - 在 Chrome 扩展程序或应用程序中获取用户本地系统上目录或文件的完整绝对路径

javascript - SAPUI5:将 OData 与同一个表中的其他 Web 服务组合

javascript - jqGrid 返回 rowObject 未定义