How Do I Create A Callback For Ng-repeat Orderby?
Looking for a way to get AngularJS'a ng-repeat orderBy filter to do a callback once it's done rendering... Markup:
Solution 1:
As usual I was over-thinking the problem..
Just needed to create a custom sort function and manage the array manually instead of relying on the orderBy filter in the ng-repeat directive:
var myApp = angular.module('myApp', []);
functionMyCtrl($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;
}
}
returnnull;
};
$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 for what I was trying to do... Thanks to @LcLk for the breadcrumb above..
Has A Class, Add Class To Table Cell |
Let's say I have the following html: …
WebRTC Video Constraints Not Working
I'm trying to get a lower resolution from the webcam na…
HasClass Doesn't Work In My Js Code?
I want to use hasClass in the following code, but that does…
I am relatively new to Javascript/Ajax. When the user click…
Highcharts Donutchart: Avoid Showing Duplicate Legend With Nested Charts
I am trying to represent nested data using Highcharts Donut…
How Do I Use Data From Vue.js Child Component Within Parent Component?
I have a form component where I use a child component. I wa…
Logic For The Next Button For The Questionnaire?
I am beginner in AngularJS and facing some issues. I am try…
Assignment To Property Of Function Parameter (no-param-reassign)
I have this function and while I have this working nicely, …
I am making a web application using nodejs and angular cli …
How To Show Website Preloader Only Once
I added a preloader to my website and the preloader animati…
|
---|
Post a Comment for "How Do I Create A Callback For Ng-repeat Orderby?"