AngularJS sorting by field value - angularjs

What I am trying to do is sort some data by field value.
$scope.testarr = [{"id":"1","name":"coffee"},
{"id":"2","name":"tea"},
{"id":"3","name":"coffee"},
{"id":"4","name":"ice coffee"}]
in the html file i have select box and 3 options called coffee, tea and ice coffee,
if i select coffee should be sorted like this
$scope.testarr = [{"id":"1","name":"coffee"},
{"id":"3","name":"coffee"},
{"id":"2","name":"tea"},
{"id":"4","name":"ice coffee"}]
if i select tea should be sorted like this
$scope.testarr = [
{"id":"2","name":"tea"},
{"id":"1","name":"coffee"},
{"id":"3","name":"coffee"},
{"id":"4","name":"ice coffee"}]
i'm trying to use order by but somehow it does't work
<div ng-repeat="item in testarr | orderBy: 'name'">
{{item.id}} ------ {{item.name}}
</div>

It does seem to work. Ensure that your ng-repeat has access to $scope.testarr (i.e. it is being declared on your $scope in the correct controller or directive.
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.testarr = [{
"id": "1",
"name": "coffee"
}, {
"id": "2",
"name": "tea"
}, {
"id": "3",
"name": "coffee"
}, {
"id": "4",
"name": "ice coffee"
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<div ng-repeat="item in testarr | orderBy: 'name'">
{{item.id}} ------ {{item.name}}
</div>
</div>
</div>

Related

Iterate following Json using ng-repeat

I am trying to iterate following json inside ng-repeat but not working. I would like to get both Ques and img. Below, I have tried "Ques" part but it doesn't display anything.
[{
"Ques": [{
"column1": "3",
"column2": "ok?"
}, {
"column1": "4",
"column2": "test"
}],
"img": [{
"column1": "21",
"column2": "CDH.PNG"
}, {
"column1": "22",
"column2": "Feedback.PNG"
}]
}]
My code given below
var messages = JSON.stringify(response.data);
$scope.messages = JSON.parse(messages);
<div class="list-group-item" ng-repeat="item in messages.Ques">
<h4 class="list-group-item-heading">{{item.column2}}</h4>
<p class="list-group-item-text">{{item.column1}}</p>
</div>
I have done this way. Its working. I can also use "Img" same like below.
<div class="list-group-item" ng-repeat="item in messages">
<div ng-repeat="items in item['Ques']">
<h4 class="list-group-item-heading">{{items.column2}}</h4>
<p class="list-group-item-text">{{items.column1}}</p>
</div>
</div>

Get month from date in angularjs ng-repeat

I have an AngularJS 1.X app with a date field retrieved from a .json file. The date is being converted from a string to a date object to order the list but I need to capture the "month" part of the date to group by and for separate display but can't figure it out.
Here's my controller and HTML:
Angular Controller:
(function () {
"use strict";
angular.module("xxyyzz")
.controller("displayEventCtrl", displayEventCtrl);
function displayEventCtrl($http) {
var model = this;
model.header = "Upcoming Events";
model.events = [];
$http.get("/App/Data/eventCalendar.json")
.then(function (response) {
model.events = response.data;
console.log(model.events);
});
model.sortDate = function (event) {
var date = new Date(event.date);
return date;
}
}
})();
HTML:
<div ng-controller="displayEventCtrl as model">
<h3><i class="fa fa-calendar"></i> {{model.header}}</h3>
<div class="list-group">
<a href="#" class="list-group-item" ng-repeat="event in model.events | orderBy : -model.sortDate : true track by $index">
<div class="list-group-item-heading">
<h4>{{event.title}}</h4>
</div>
<p class="list-group-item-text">
Tuctus placerat scelerisque.
</p>
<div class="row">
<div class="col-xs-6">
<span class="small">{{event.location}}</span>
</div>
<div class="col-xs-6">
<span class="small pull-right">
{{event.startTime}} - {{event.endTime}}
</span>
</div>
</div>
<div class="row">
<div class="col-xs-9">
<span class="small">{{event.city}}, {{event.state}}</span>
</div>
<div class="col-xs-3">
<span class="small pull-right">{{event.date}}</span>
<div class="clearfix"></div>
</div>
</div>
</a>
</div>
</div>
JSON example:`
[
{
"id": 1,
"title": "Christmas Parade",
"date": "12/25/2017",
"city": "Dallas",
"state": "TX",
"location": "Galleria Mall",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 2,
"title": "Thanksgiving Parade",
"date": "11/23/2017",
"city": "Denver",
"state": "CO",
"location": "Mile High Stadium",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 3,
"title": "Halloween Parade",
"date": "10/31/2017",
"city": "Sleepy Hollow",
"state": "NY",
"location": "Ichabod Crane House",
"startTime": "19:00",
"endTime": "21:00"
},
{
"id": 4,
"title": "Independence Day Fireworks",
"date": "07/04/2017",
"city": "Washington",
"state": "DC",
"location": "National Mall",
"startTime": "11:00",
"endTime": "15:00"
}
]
Any help would be appreciated.
You can use js api named moment. Inject moment as dependency. You can use moment to get date in any formate you want.
moment ().get ('month');
To get the month, use the getMonth() function
var Xmas95 = new Date('December 25, 1995 23:15:30');
var month = Xmas95.getMonth();
console.log(month); // 11
My issue is that I am trying to pull the date from the array in the ng-repeat. So I can't declare the variable like above as it is part of a returned array {{event.date}}. What I need to know is how to get that date part from the nested item.
Use a custom filter to convert the text data to a Date object:
angular.module("app",[])
.filter("toDate", function() {
return function(data) {
return new Date(data);
};
})
.run(function($rootScope) {
var vm = $rootScope;
vm.Xmas95 = 'December 25, 1995 23:15:30';
})
<script src="//unpkg.com/angular/angular.js"></script>
<div ng-app="app">
Month number = {{Xmas95 | toDate | date: 'MM'}}
</div>
For more information, see
AngularJS Developer Guide - Creating Custom Filters
AngularJS date Filter API Reference

How to filter multiple values (OR operation) in angularjs if filter is false

Have some trouble with multiple filters. One filter works good, if add second filter ng-repeat get filters with operator AND, but I need take second filter if first is FALSE.
function Ctrl($scope) {
$scope.users = [
{"id":1,"username":"", "age": "18"},
{"id":8,"username":"betty", "age": ""},
{"id":14,"username":"", "age": "18"},
{"id":3,"username":"jumbo1", "age": ""},
]
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app>
<div ng-controller="Ctrl">
<div ng-repeat="user in filtered = (users | filter:{ 'username':'betty'}:true)">
<span>{{ user.id }}</span>
</div>
Users: {{users.length}}<br>
Filtered Users: {{filtered.length}}
</div>
</body>
Multiple filters like, don't work:
filter:{ 'username':'betty'} || { 'age':'18'}
Finally need if first filter is FALSE take second filter
For this you will need to create a simple filter function in controller. The usage could be something like this for example:
users | filter:userFilter({username: 'betty', age: 18}
Here is a demo:
angular.module('demo', []).controller('Ctrl', Ctrl);
function Ctrl($scope) {
$scope.users = [
{"id":1,"username":"", "age": "18"},
{"id":8,"username":"betty", "age": ""},
{"id":14,"username":"", "age": "18"},
{"id":3,"username":"jumbo1", "age": ""},
];
$scope.userFilter = function(filter) {
return function(user) {
return user.username == filter.username || user.age == filter.age;
};
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<body ng-app="demo">
<div ng-controller="Ctrl">
<div ng-repeat="user in filtered = (users | filter:userFilter({username: 'betty', age: 18}))">
<span>{{ user | json }}</span>
</div>
<pre>Users: {{users.length}}<br>Filtered Users: {{filtered.length}}</pre>
</div>
</body>

Creating nested ng-repeats in Angular

I'm using Angular and trying to create nested ng-repeats. I've referred to the ng-repeat examples on the Angular site here here. But when I use the code below, the <ul> tag repeats but the <li> tag is blank. Any suggestions on how to do this correctly?
(EDIT: Updated array to address comment)
Array:
vm.projects = [
{"$id": "1234",
"people": {
"-K-v76MWDTIQqnR2w10r": {
"name": "John Doe",
"city": "New York",
"company": "Acme, Inc."
},
"-K-q7HmGUduAf5JkSGDY": {
"name": "Jane Smith",
"city": "Chicago",
"company": "ABC, Inc."
}
}
},
{
"$id": "2345",
"people": {
"-K-qq6Pcd0v1wggmALcZ": {
"name": "David Jones",
"city": "London",
"company": "Stardust, LLC"
}
}
}
]
HTML
<ul ng-repeat="project in vm.projects" >ID: {{project.$id}}
<li ng-repeat="name in vm.projects.people.name track by projects.$id">{{project.person.name}}
</li>
</ul>
Here is what actually displays:
ID: 1234
ID: 2345
You can use parent ul project object in li like below:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example85-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular-animate.js"></script>
</head>
<body ng-app="initExample">
<script>
angular.module('initExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.projects = [{"$id":"1234","people":{"-K-v76MWDTIQqnR2w10r":{"name":"John Doe","city":"New York","company":"Acme, Inc."},"-K-q7HmGUduAf5JkSGDY":{"name":"Jane Smith","city":"Chicago","company":"ABC, Inc."}}},{"$id":"2345","people":{"-K-qq6Pcd0v1wggmALcZ":{"name":"David Jones","city":"London","company":"Stardust, LLC"}}}];
}]);
</script>
<div ng-controller="ExampleController">
<ul ng-repeat="project in projects" >ID: {{project.$id}}
<li ng-repeat="(id, person) in project.people">{{person.name}}
</li>
</ul>
</div>
</body>
</html>
You want to do something like (in your inner ng-repeat):
name in project.people
Instead of
name in vm.projects.people.name track by projects.$id
(If you post a valid JSON sample I can give you the exact thing to put.)
you can use like this
<ul>
<li ng-repeat="c in countries">
<label>
<input type="checkbox" ng-model="CountryName" />
{{c.CountryName}}
</label>
<ul ng-repeat="s in cities" ng-if="c.CountryId==s.CountryId">
<li>
<label>
<input type="checkbox" ng-model="CityName" />
{{s.CityName}}
</label>
</li>
</ul>
</li>
</ul>
refere below url http://www.codeproject.com/Tips/987499/Nested-Ng-Repeat-on-AngularJS
First it's important to point out that there were some significant syntax issues in your JSON which was breaking your ng-repeats.
You must refer to the object inside your first ng-repeat (the one you declare in the ul tag) to make sure the scope is mapped properly for the li tag scopes. In my example "someKey" refers to the alpha numeric strings you had. You will need to make sure these are the same in each son structure in order to refer to them properly. This is why I changed the name to "someKey". My suggestion is to use a key named "data" and then use "id" to keep track of the name of the data you are using for your key. Example:
{"$id": "1234",
"people": {
id: "-K-v76MWDTIQqnR2w10r",
data: {
"name": "John Doe",
"city": "New York",
"company": "Acme, Inc."
},
Here's the code:
$scope.vm = {};
$scope.vm.projects = [
{$id:"1234",
"people":[
{"someKey":
{"name":"John Doe","city":"New York","company":"Acme, Inc."}
},
{"someKey":
{"name":"Jane Smith","city":"Chicago","company":"ABC, Inc."}
}]
},
{$id:"2345",
"people":[
{"someKey":
{"name":"David Jones","city":"London","company":"Stardust, LLC"}
}]
}
]
<div ng-controller="MyCtrl">
Hello, {{name}}!
<ul ng-repeat="project in vm.projects" >ID: {{project.$id}}
<li ng-repeat="people in project.people">{{people.someKey.name}}
</li>
</ul>
</div>

Angular list with ng-repeat with date as divider

I've got a JSON object with different events that looks like this:
{
"error":false,
"events":[
{
"id":1,
"title":"First entry",
"date":"2014-11-04"
},
{
"id":2,
"title":"Second entry",
"date":"2014-11-04"
},
{
"id":3,
"title":"Third entry",
"date":"2014-11-05"
},
{
"id":4,
"title":"Fourth entry",
"date":"2014-11-06"
},
{
"id":5,
"title":"Fifth entry",
"date":"2014-11-06"
}
]
}
This object is stored in $scope.events in my controller.
Now I'm looping this array to build the list of events:
<ion-list>
<div class="item item-divider">
{{event.date}}
</div>
<a class="item item-thumbnail-left" href="#/app/event/{{event.id}}" ng-repeat="event in events">
<img src="media/thumbnails/{{event.id}}.jpg">
<h1>{{event.title}}</h1>
</a>
</ion-list>
My goal is to display {{event.date}} as a list divider just once for every day. So in this examle it shoudl look like this:
2014-11-04 (divider)
First entry
Second entry
2014-11-05 (divider)
Third entry
2014-11-06 (divider)
Fourth entry
Fifth entry
I'm relly new to ionic & angular and I have no idea how to achieve this. May with some custom filter?
All in all I'm looking for something similar to Angular: Getting list with ng-repeat with dividers / separators but with the date as separator instead of initial letter.
Some ideas?
Any help/tip is really appreciated!
You can use angular.filters (https://github.com/a8m/angular-filter) to group your list by date please see demo below
var app = angular.module('app', ['angular.filter']);
app.controller('homeCtrl', function($scope) {
$scope.data = {
"error": false,
"events": [{
"id": 1,
"title": "First entry",
"date": "2014-11-04"
}, {
"id": 2,
"title": "Second entry",
"date": "2014-11-04"
}, {
"id": 3,
"title": "Third entry",
"date": "2014-11-05"
}, {
"id": 4,
"title": "Fourth entry",
"date": "2014-11-06"
}, {
"id": 5,
"title": "Fifth entry",
"date": "2014-11-06"
}]
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.9/angular-filter.min.js"></script>
<div ng-app="app">
<div ng-controller="homeCtrl">
<ion-list ng-repeat="(key, value) in data.events | groupBy: 'date'">
<div class="item item-divider">
<h1> {{key}}</h1>
</div>
<a class="item item-thumbnail-left" href="#/app/event/{{event.id}}" ng-repeat="event in value">
<img src="media/thumbnails/{{event.id}}.jpg">
<h3>{{event.title}}</h3>
</a>
</ion-list>
</div>
I solved this in another SO question here, Ionic Dynamic List Divider. Basically you can modify the list (in the controller, not the source of course) to include the dates. In your view, you notice this and render them as dividers.

Resources