I have an issue regarding filter data on some criteria in AngularJS. I want to show/order all filled fields first then in the end all fields having data ("-"). As filter is inside ng-repeat so, I want to run the filter only once which is I know not possible as AngularJS using 'dirty checking'.
'appStatusData'=
Data-A: "0.000"
Data-Date: "2014-09-07 00:00:00.0"
Data-B: "111"
Data-C: "-"
Data-D: "0517"
Data-E: "462.000"
Data-F: "-"
Data-G: "No"
Data-H: "-"
Expected Output:
Data-A: "0.000"
Data-Date: "2014-09-07 00:00:00.0"
Data-B: "111"
Data-D: "0517"
Data-E: "462.000"
Data-G: "No"
Data-C: "-"
Data-F: "-"
Data-H: "-"
<ion-view view-title="Application Status" name="permit-view">
<ion-header-bar class="bar-stable bar-subheader">
<div class="title" style="color: #2A4660"><u>Header</u></div>
</ion-header-bar>
<ion-content class="padding" has-subheader=true>
<ion-list>
<ion-item class="item" ng-repeat="(key, val) in (appStatusData | showDecimalData | showDataByNotNullFirst ) ">
<b>{{key}} </b> {{val}}
</ion-item>
</ion-list>
<button class="button button-full button-dark" ng-click="showList();">Show Details</button>
</ion-content>
Javascript: I am doing like this which is not working at all as 'present' and 'empty' array are undefined.
angular.module('myApp')
.filter('showDataByNotNullFirst', function($filter) {
return function(array) {
var present, empty, result;
//console.log('Key is: ' + key + ' Value is: ' + value);
$filter('filter')(array, function(key, val) {
if (val != '-') {
present[key] = val;
} else {
empty[key] = val;
}
});
result = present.concat(empty);
return result;
};
});
Can any please resolve this or suggest me the solution?
I would solve this in the controller.
First you would need to convert your object properties to an array.
var statusArr = [],
sortedArr = [];
// Loop over every property inside your object
for(var propertyName in appStatusData) {
var obj = {
name: propertyName,
value: m[propertyName]
}
statusArr.push(obj);
}
Now lets loop over every item inside your statusArr array. If the item = '-', push it in the new array. If it isn't a '-', unshift it in the beginning of the array.
More information about JavaScript Array methods.
angular.forEach(statusArr, function (s) {
if (s === '-') {
// add it to the 'right' side of the array
sortedArr.push(s);
} else {
// add it on the 'left' side of the array
sortedArr.unshift(s);
}
});