Angular list with ng-repeat with date as divider - angularjs

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.

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 display images dynamically in slider on clicking a particular image using angular js

we have a problem in displaying images in slider dynamically.i.e, when we click on a particular image in list it must be displayed in the slider.We are getting the images from a json file.The problem is the images are not getting displayed in slider,but the click functionality is working.Clicked image ids are shown in console too.
Here is the code:
enter code here
JSON:
[{
"menuId": "01",
"menuItem": "manuchuriya",
"menuImage": "img/Categoryimages/manchuria.jpe",
"items": [{
"id": "001",
"itemName": "Noodles",
"itemImage": "item_images/aloo_chaat.jpg",
"price": "$30",
"description": "Add water or veg stock.mix well and add let the sauce simmer for 1-2 minutes.add the cornflour paste to thicken the sauce.Thicken the sauceto your desired consistency.add vinegar, salt, sugar and the fried veg balls.serve veg manchurian garnished with spring onion greens."
},
{
"id": "002",
"itemName": "Noodles",
"itemImage": "item_images/samosa.jpg",
"price": "$20",
"description": "Add water or veg stock.mix well and add let the sauce simmer for 1-2 minutes.add the cornflour paste to thicken the sauce.Thicken the sauceto your desired consistency.add vinegar, salt, sugar and the fried veg balls.serve veg manchurian garnished with spring onion greens."
}]
},
{
"menuId": "02",
"menuItem": "icecream",
"menuImage": "img/Categoryimages/ice-cream.jpg",
"items": [{
"id": "001",
"itemName": "salad",
"itemImage": "item_images/fruit_juice.jpg",
"price": "$40",
"description":"Add ghee, salt and carom seeds in maida. Mix all ingredients. Cut paneer into small chunks.Peel potatoes and mash them Knead the dough until it becomes soft. Take little amount of dough and roll it."
},
{
"id": "002",
"itemName": "salad",
"itemImage": "item_images/bevarages.jpg",
"price": "$20",
"description":"Add ghee, salt and carom seeds in maida. Mix all ingredients. Cut paneer into small chunks.Peel potatoes and mash them Knead the dough until it becomes soft. Take little amount of dough and roll it."
}]
},
{
"menuId": "03",
"menuItem": "franke",
"menuImage": "img/Categoryimages/samosas.jpg",
"items": [{
"id": "001",
"itemName": "salad",
"itemImage": "item_images/frankie.jpg",
"price": "$30",
"description":"Add ghee, salt and carom seeds in maida. Mix all ingredients. Cut paneer into small chunks.Peel potatoes and mash them Knead the dough until it becomes soft. Take little amount of dough and roll it."
},
{
"id": "002",
"itemName": "salad",
"itemImage": "item_images/aloo_chaat.jpg",
"price": "$40",
"description":"Add ghee, salt and carom seeds in maida. Mix all ingredients. Cut paneer into small chunks.Peel potatoes and mash them Knead the dough until it becomes soft. Take little amount of dough and roll it."
}]
}]
Controller:
angular.module('starter')
.controller('menuitemCtrl', function ($scope,$state,$q,$stateParams,menuService) {
console.log($stateParams.id);
$scope.id = $stateParams.id;
var getMenuList = function(){
menuService.getMenuListData()
.then(function sucessCallback(response){
$scope.menulists = response;
console.log("menu");
for(var i in $scope.menulists.items)
{
if($scope.menulists[i].items[i].id === $scope.id)
{
$scope.SliderItem=$scope.menulists[i].items[i];
console.log(SliderItem);
}
}
function errorCallback(error){
return $q.reject(error);
});
};
getMenuList();
});
HTML Code:
<ion-view class="view-styles-item" style="margin-left: 24%">
<ion-content scroll="false">
<ion-slides options="options" slider="data.slider">
<ion-slide-page ng-repeat="item in SliderItem.items">
<div class="row">
<img src="img/menuitem/{{item.itemImage}}" style="width: 52%;">
</div>
<div class="row">
<p>{{item.description}}</p>
<p>{{item.price}}</p>
</div>
</ion-slide-page>
</ion-slides>
</ion-content>
Click event HTML Code:
<ion-view class="view-styles-menu" >
<ion-content >
<div ng-repeat="item in SelectedMenu.items" >
<div class="row menu-row">
<div class="col">
<!-- <a href="#/tab/home/item?id={{item.id}}"> -->
<img src="/img/menuitem/{{item.itemImage}}" class="menu-img_styles" ng-click="goToMenuItem(item.id)">
</div>
<div class="col">
{{item.itemName}}
</div>
<div class="col">
{{item.price}}
</div>
</div>
</div>
<ion-nav-view name="tab-item"></ion-nav-view>
</ion-content>
<div class="divider"></div>
</ion-view>
Controller:
$scope.goToMenuItem = function(paramId){
$state.go('tab.home.menu.item',{id:paramId});
};

ng-if to hide all elements in page except 1

I have several divs on my page which use an ng-if to show. My issue is that certain elements are duplicates.
What I would like to do is use another expression in the ng-if for this example i added below (hideMultiplePost) to hide these duplicate elements and my idea is to identify them by adding a class which uses the post id since the duplicate elements share the same id.
<div ng-repeat="post in postList">
<div class="{{post.id}}" ng-if="post.cat.length > 0 && hideMultiplePost(post.id)">
</div>
<div ng-repeat="post in postListV2">
<div class="{{post.id}}" ng-if="post.cat.length > 0 && hideMultiplePost(post.id)">
</div>
Can someone put me in the right direction in using an expression (hideMultiplePost) where I check for duplicate classes and exclude them from the frontend but leave one them as I don't want to exclude them all.
You could use the existing 'unique' filter supplied by angular UI.
The unique filter allows you to supply a field in your model that should be unique. I assume that all of your posts have a id and that this id is unique. You can filter out multiple posts based on this field.
After the applied filter, you can still use your ng-if statement to check if the post contains any categories.
Check the snippet for more info on how to use it.
angular
.module('app', ['ui.filters']);
angular
.module('app')
.controller('PostListController', PostListController);
function PostListController() {
var vm = this;
vm.posts = getPosts();
}
function getPosts() {
return [{
"id": 1,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 2,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 3,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 4,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 5,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 3,
"title": "Post Title",
"content": "Post content here"
}, {
"id": 4,
"title": "Post Title",
"content": "Post content here"
}];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js"></script>
<div ng-app="app">
<div ng-controller="PostListController as vm">
<ul>
<li ng-repeat="post in vm.posts | unique:'id'">
<b>#{{ post.id }} {{ post.title }} </b><br />
<p>{{ post.content }}</p>
</li>
</ul>
</div>
</div>
If you cannot control the data in, then you can remove dupes using a filter. I have assumed it's an ng-repeat, but you can edit for your own use. More detail in fiddle.
<div ng-repeat="item in data | dedupe:'id'">
{{item.id}}: {{item.name}}
</div>
Edit:
Concat the two sources (postList and postListV2) in your controller, and then use the filter to dedupe:
function MyCtrl($scope) {
$scope.postList = postList;
$scope.data = $scope.postList.concat(postListV2)
}
More info in the fiddle:
http://jsfiddle.net/Lvc0u55v/7736/

AngularJS sorting by field value

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>

Resources