ng-table插件(四-排序基础)

基本排序

ng-table提供一个表头来增加基本的排序功能。

(1)指定具体哪一列需要排序

(2)可以通过NgTableParams可选择的提供初始列排序

(3)在这个例子中需要为NgTableParams.sorting()设置值,然后展示出来。

下面是代码:

Overview

ngTable provides a header template that adds sorting to a html table

  • Specify which column should be sortable
  • Optionally supply the initial columns to sort to the NgTableParams constructor call

Got a getData function?

In this case you'll need to apply the values from NgTableParams.sorting() to the data you want to display in your table. This is typically the case when the data is being fetched from the server.

ngTable directive

{{row.name}} {{row.age}} {{row.money}}

ngTableDynamic directive

{{row[col.field]}}

(function() {
  "use strict";

  var app = angular.module("myApp", ["ngTable", "ngTableDemos"]);
  app.controller("demoController", demoController);
  demoController.$inject = ["NgTableParams", "ngTableSimpleList"];
  //定义controller依赖注入
  function demoController(NgTableParams, simpleList) {//默认控制器,传入数据产生表格
    this.tableParams = new NgTableParams({
      // initial sort order
      sorting: { name: "asc" } //asc表示升序
    }, {
      dataset: simpleList
    });
  }
 
  app.controller("dynamicDemoController", dynamicDemoController);
  dynamicDemoController.$inject = ["NgTableParams", "ngTableSimpleList"];
   //定义controller依赖注入
  function dynamicDemoController(NgTableParams, simpleList) {
    this.cols = [//设置哪些可以排序,哪些不可以
      { field: "name", title: "Name", sortable: "name", show: true },
      { field: "age", title: "Age", sortable: "age", show: true },
      { field: "money", title: "Money", show: true }
    ];
    
    this.tableParams = new NgTableParams({
      // initial sort order
      sorting: { age: "desc" } //降序
    }, {
      dataset: simpleList
    });
  }
})();

(function() {
  "use strict";
  angular.module("myApp").run(configureDefaults);
  configureDefaults.$inject = ["ngTableDefaults"];
  function configureDefaults(ngTableDefaults) {
    ngTableDefaults.params.count = 5;//通过provider服务,调用config设置基本参数
    ngTableDefaults.settings.counts = [];
  }
})();
ng-table插件(四-排序基础)_第1张图片
sorting.png

你可能感兴趣的:(ng-table插件(四-排序基础))