Im trying to write a fairly complex ng-repeat
that potentially uses filtering and parseing.
What i am first doing is ng-repeat
ing a table heading of 12 months from the current month.
I am then displaying two rows - Searches with impact and Searches with NO impact
What i am trying to do is loop through my JSON file that contains my search data, and counts how many Searches with Impact and Search with No Impact, have been carried out for that particular month based on the SearchedOn field.
The logic behind understanding Searches with Impact and Search with No Impact is based on the IsIncludedInSearchImpactCalculation field being true or false.
What also needs to happen is that if my JSON file contains element of data that is greater than 12months old (based on the SearchedOn field), then this should be not included in the calculation.
HTML:
<div ng-app="test">
<div ng-controller="Ctrl">
<table>
<thead>
<tr>
<th></th>
<th ng-repeat="currMonth in months">{{currMonth}}</th>
</tr>
</thead>
<tbody>
<tr>
<th>Searches with impact</th>
<td></td>
<td>3</td>
<td></td>
<td>1</td>
<td>1</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>3</td>
<td>1</td>
<td></td>
<td>1</td>
</tr>
<tr>
<th>Searches with NO impact</th>
<td>2</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>1</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<br>
<p>Searches with impact:</p>
<li ng-repeat="searchedOn in Searches | filter:{IsIncludedInSearchImpactCalculation:true}">{{searchedOn.SearchedOn}}</li>
<br>
<p>Searches with NO impact:</p>
<li ng-repeat="searchedOn in Searches | filter:{IsIncludedInSearchImpactCalculation:false}">{{searchedOn.SearchedOn}}</li>
</div>
</div>
JS:
angular.module('test', []).controller('Ctrl', function ($scope) {
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
for (var i = 0; i < 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
// Subtract a month each time
date.setMonth(date.getMonth() - 1);
}
$scope.months = months;
$scope.Searches = [{
"SearchedOn": "19/04/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "18/04/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "01/05/2014",
"IsIncludedInSearchImpactCalculation": false
}, {
"SearchedOn": "21/05/2014",
"IsIncludedInSearchImpactCalculation": false
}, {
"SearchedOn": "20/07/2013",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "07/01/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "08/12/2013",
"IsIncludedInSearchImpactCalculation": false
}, {
"SearchedOn": "24/02/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "30/06/2013",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "28/06/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "14/08/2013",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "11/04/2014",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "13/08/2013",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "03/08/2013",
"IsIncludedInSearchImpactCalculation": true
}, {
"SearchedOn": "20/01/2011",
"IsIncludedInSearchImpactCalculation": true
}];
});
Here's my fiddle: http://jsfiddle.net/oampz/j9LBe/
In my fiddle, i have manually hard coded what the table should look like based on the lists.
UPDATE:
Adding:
<td ng-repeat="item in filtered = (Searches | filter:{IsIncludedInSearchImpactCalculation:true})"></td>
<td>Filtered list has {{filtered.length}} items</td>
Gets me the total number of items in a row for Searches with impact or Searches with NO impact see fiddle: http://jsfiddle.net/oampz/j9LBe/1/
Now all that is needed is to group by month based on the previous ng-repeat:
<th ng-repeat="currMonth in months">{{currMonth}}</th>