首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >智能表以编程方式排序

智能表以编程方式排序
EN

Stack Overflow用户
提问于 2016-02-09 04:03:45
回答 3查看 1.7K关注 0票数 2

我为AngularJS安装了一个使用智能表插件的表。一切看起来都很顺利。我不想让用户单击表头来触发排序,而是以编程方式触发来自角度控制器的排序。在这里的文档中,我没有看到这样做的方法:

http://lorenzofox3.github.io/smart-table-website/

我是不是忽略了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-09 04:07:11

在JSFiddle上找到这个,可能对你有帮助:http://jsfiddle.net/vojtajina/js64b/14/

代码语言:javascript
复制
<script type="text/javascript" ng:autobind
src="http://code.angularjs.org/0.10.5/angular-0.10.5.js"></script>

<table ng:controller="SortableTableCtrl">
    <thead>
        <tr>
            <th ng:repeat="(i,th) in head" ng:class="selectedCls(i)" ng:click="changeSorting(i)">{{th}}</th>
        </tr>
    </thead>
    <tbody>
        <tr ng:repeat="row in body.$orderBy(sort.column, sort.descending)">
            <td>{{row.a}}</td>
            <td>{{row.b}}</td>
            <td>{{row.c}}</td> 
        </tr>
    </tbody>
</table>
票数 3
EN

Stack Overflow用户

发布于 2016-04-25 10:13:08

我在如何做到这一点上找到了一个快速的方法,就是设置表头st-排序属性,然后触发对该元素的click()。

代码语言:javascript
复制
<tr>
  <th id="myelement" st-sort="date" st-sort-default="reverse">Date</th> 
  ...
</tr>

然后稍后:

代码语言:javascript
复制
 setTimeout(function() {
      document.getElementById('myelement').click()        
    }, 
  0);
票数 1
EN

Stack Overflow用户

发布于 2019-04-30 22:25:06

这是一种“角度”的方法。写个指令。此指令将访问智能表控制器。它将能够按函数调用控制器的排序。我们将命名新的指令stSortBy。

下面的HTML包括标准智能表合成糖。这里唯一的新属性指令是st-排序-by。魔法就会在那里发生。它绑定到一个作用域变量sortByColumn。这是要排序的列的字符串值。

代码语言:javascript
复制
<table st-sort-by="{{sortByColumn"}}" st-table="displayedCollection" st-safe-src="rowCollection">
<thead>
<tr>
<th st-sort="column1">Person</th>
<th st-sort="column2">Person</th>
</tr>
</thead>
</table>

<button ng-click="toggleSort()">Toggle sort columns!</button>

下面是连接到st表控制器的stSortBy指令

代码语言:javascript
复制
app.directive('stSortBy', function() {
    return {
        require: 'stTable',
        link: function (scope, element, attr, ctrl) {
            attr.$observe('stSortBy', function (newValue) {
                if(newValue) {
                    // ctrl is the smart table controller
                    // the second parameter is for the sort order
                    ctrl.sortBy(newValue, true);
                }
            });
        }
    };
});

这是控制器。控制器在其作用域中设置排序

代码语言:javascript
复制
app.controller("MyTableWrapperCtrl", ["$scope", function($scope) {
  $scope.sortByColumn = 'column2';
  
  $scope.toggleSort = function() {
     $scope.sortByColumn = $scope.sortByColumn === 'column2' ? 'column1' : 'column2';
     // The time out is here to guarantee the attribute selector in the directive
     // fires. This is useful is you do a programmatic sort and then the user sorts
     // and you want to programmatically sort back to the same column. This forces a sort, even if you are sorting the same column twice.
     $timeout(function(){
      $scope.sortByColumn = undefined;
     }, 0);
  };
}]);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35283669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档