排序在智能表角度js中不起作用

时间:2016-04-28 16:11:05

标签: javascript angularjs smart-table

使用自定义指令后,智能表排序因某些原因无效。你能帮我解决我做错的事吗?

我的代码 http://plnkr.co/edit/mzPifIAT0RTd63rIYJsi?p=preview

以下是我的观点,HTML和js代码:

查看

<div ng-controller="ctrl1 as one">
    <ltcg-table options="one.rowCollection"></ltcg-table>    
</div>
<div ng-controller="ctrl2 as two">
    <ltcg-table options="two.rowCollection"></ltcg-table>   
</div>

网格HTML

<table st-table="rowCollection" class="table table-striped">
    <thead>
        <tr>
            <th st-sort="getters.firstName">first name</th>
            <th st-sort="lastName">last name</th>
            <th st-sort="birthDate">birth date</th>
            <th st-sort="balance">balance</th>
            <th>email</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="row in options">
            <td>{{row.firstName}}</td>
            <td>{{row.lastName}}</td>
            <td>{{row.birthDate}}</td>
            <td>{{row.balance}}</td>
            <td>{{row.email}}</td>
        </tr>
    </tbody>
</table>

JS

(function () {
      var myApp = angular.module('myApp', ['smart-table']);

      function one() {               
          this.rowCollection = [
              {firstName: 'Laurent', lastName: 'Renard', birthDate: 4, balance: 102, email: 'whatever@gmail.com'},
              {firstName: 'Blandine', lastName: 'Faivre', birthDate: 5, balance: -2323.22, email: 'oufblandou@gmail.com'},
              {firstName: 'Francoise', lastName: 'Frere', birthDate: 4, balance: 42343, email: 'raymondef@gmail.com'}
          ];
          //alert($scope.gridOptions.columnDefs[1].name);
          //alert($scope.gridOptions);
      };


      function two() {     
          this.rowCollection = [
              {firstName: 'test2', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com'},
              {firstName: 'test2', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'oufblandou@gmail.com'},
              {firstName: 'test2', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'raymondef@gmail.com'}
          ];
          //alert($scope.gridOptions.columnDefs[1].name);
          //alert($scope.gridOptions);
      };

      myApp.directive('ltcgTable', function() {
          return {
              restrict: 'E',
              transclude: true,
              scope: {
                'options': '='
              },
              templateUrl: "ltcg-table.html",
              link: function(scope, element, attr) {                        
                  scope.rowCollection = scope.options;
              }
          }             
     });
     myApp.controller('ctrl1', one)
     myApp.controller('ctrl2', two)
})();

1 个答案:

答案 0 :(得分:2)

好的问题是两件事

您需要使用st-safe-src

<table st-safe-src="rowCollection" st-table="displayCollection" class="table table-striped">

您的行ng-repeat需要位于displayCollection

 <tr ng-repeat="row in displayCollection">

您无需初始化displayCollection