寻找一种方法来获取AngularJS'a ng-repeat orderBy过滤器在完成渲染后进行回调...
标记:
<div ng-controller="MyCtrl">
<table>
<thead>
<tr>
<th ng-click="sort('name')">Name:</th>
<th ng-click="sort('age')">Age:</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items | orderBy:sortColumn:reverseSort">
<td>{{item.name}}</td>
<td>{{item.age}}</td>
</tr>
</tbody>
</table>
</div>
JS:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.items = [{
"name": "John",
"age": 25
}, {
"name": "Amy",
"age": 75
}, {
"name": "Sarah",
"age": 12
}, {
"name": "Matt",
"age": 55
}, {
"name": "Xaviour",
"age": 2
}];
$scope.sortColumn = 'name';
$scope.reverseSort = false;
$scope.sort = function (column) {
$scope.sortColumn = column;
$scope.reverseSort = !$scope.reverseSort;
};
$scope.sortRenderFinished = function(){
console.log('Done Rendering!');
//Do something
};
}
正如你所看到的那样..一旦orderBy过滤器执行完毕并改变表行的顺序,我希望看到像$ scope.sortRenderFinished()这样的东西。
任何帮助都会非常感谢! : - )
答案 0 :(得分:1)
像往常一样,我过度思考这个问题......
只需创建一个自定义排序函数并手动管理数组,而不是依赖于ng-repeat指令中的orderBy过滤器:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, $filter) {
$scope.items = [{
"name": "John",
"age": 25
}, {
"name": "Amy",
"age": 75
}, {
"name": "Sarah",
"age": 12,
}, {
"name": "Matt",
"age": 55
}, {
"name": "Xaviour",
"age": 2
}];
$scope.addChild = function () {
var
matt = $scope.items[findIndexByKeyValue($scope.items, 'name', 'Matt')],
savannah = {
"name": "Savannah",
"age": 2,
"parent": matt
};
console.log('matt');
console.log(matt);
$scope.items.splice($scope.items.indexOf(matt) + 1, 0, savannah);
matt.child = savannah;
$scope.matt = matt;
$scope.savannah = savannah;
};
var findIndexByKeyValue = function (obj, key, value) {
for (var i = 0; i < obj.length; i++) {
if (obj[i][key] == value) {
return i;
}
}
return null;
};
$scope.sortColumnExp = '';
$scope.sort = function (column) {
$scope.sortColumnExp = ($scope.sortColumnExp.indexOf('-') == 0) ? column : '-' + column;
console.log($scope.sortColumnExp);
if ($scope.savannah) {
$scope.items.splice($scope.items.indexOf($scope.savannah), 1);
}
$scope.items = $filter('orderBy')($scope.items, $scope.sortColumnExp);
if ($scope.savannah) {
$scope.items.splice($scope.items.indexOf($scope.savannah.parent) + 1, 0, $scope.savannah);
}
};
}
Working JSFiddle我正在尝试做什么...感谢@LcLk上面的痕迹..