美文网首页
ng-table插件(二-惰性加载数组)

ng-table插件(二-惰性加载数组)

作者: MakingChoice | 来源:发表于2016-06-16 01:38 被阅读768次

    AngularJS ng-table插件第二讲,下面这个官网例子会根据ng-option来调用map来改变内部数据,然后整个ng-table也会改变。
    下面是源码,我会根据源码来讲解

    
    <div ng-app="myApp">
      <div ng-controller="demoController as demo">
        <h2 class="page-header">Lazy loading managed array</h2>
        <div class="bs-callout bs-callout-info">
          <h4>Overview</h4>
          <p>You are not limited to having the data array to hand at the time when <code>NgTableParams</code> is created. So for example you could load the data using <code>$http</code> and then hand this to <code>NgTableParams</code></p>
        </div>
        <div class="form-inline" style="margin-bottom: 20px">
          <div class="form-group">
            <label for="datasets">Select dataset</label>
            <select id="datasets" class="form-control" ng-model="demo.dataset" ng-options="dataset for ds in demo.datasets"
                    ng-change="demo.changeDs()"></select>
          </div>
        </div>
        <table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
          <tr ng-repeat="row in $data">
            <td data-title="'Name'" filter="{name: 'text'}" sortable="'name'">{{row.name}}</td>
            <td data-title="'Age'" filter="{age: 'number'}" sortable="'age'">{{row.age}}</td>
            <td data-title="'Money'" filter="{money: 'number'}" sortable="'money'">{{row.money}}</td>
          </tr>
        </table>
      </div>
    </div>
    
    • 使用<code>controller as demo</code>来把<code>controller</code>内的对象绑定在<code>controller </code>的实例对象上。可以跨越<code>scope</code>调用,进行双向绑定。
    • 当<code>ng-option</code>发生变化的时候,会触发<code>ng-change</code>,<code>ng-change</code>会把值传进到<code>changeDs()</code>中。
    • <code>$data</code>是ng-table内部数据源,<code>row in $data</code> ng-table会遍历展示数据源。
    • <code>filter="{name: 'text'}" </code> filter属性会规定过滤条件。
    (function() {
      "use strict";
      var app = angular.module("myApp", ["ngTable", "ngTableDemos"]);
      app.controller("demoController", demoController);
      demoController.$inject = ["NgTableParams", "ngTableSimpleList"];
      function demoController(NgTableParams, simpleList) {
        var self = this;
        self.changeDs = changeDs;//因为已经用controller as demo,相当于实例化controller,直接通过this可以调用
        self.datasets = ["1", "2"];//ng-options的值
        self.dataset1 = simpleList;//simpleList相当于ngTableSimpleList注入的数据源。与createDs2想对应(当ng-option为1的时候)。
        self.dataset2 = createDs2();//与createDs2想对应,与ng-option=2对应与。
        self.tableParams = new NgTableParams();//实例化table插件
        function changeDs() {//当ng-change时候调用
          self.tableParams.settings({
            dataset: self["dataset" + self.dataset];//确定调用dataset1还是dataset2
          });
        }
        function createDs2() {//这里就是通过map改变数据
          return simpleList.map(function(item) {
            return angular.extend({}, item, {
              age: item.age + 100
            });
          });
        }
      }
    })();
    (function() {
      "use strict";
      angular.module("myApp").run(configureDefaults);
      configureDefaults.$inject = ["ngTableDefaults"];
      function configureDefaults(ngTableDefaults) {//这里可以看出源码是根据provider实现的
          ngTableDefaults.params.count = 5;
          ngTableDefaults.settings.counts = [];
      }
    })();
    

    相关文章

      网友评论

          本文标题:ng-table插件(二-惰性加载数组)

          本文链接:https://www.haomeiwen.com/subject/nolmdttx.html