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
Related
I have a problem with the print out Data from the array.
I have
{{orders}} =
[{"item":"DELL","quantity":6,"price":144},
{"item":"Samsung","quantity":3,"price":3131},
{"item":"222222","quantity":1,"price":31},
{"item":"111111111","quantity":1,"price":13}]
I try in View
<div ng-repeat="order in orders track by $index">
<span>{{ order[$index].item }}
</span>
</div>
And nothing. Can someone help me with that??
Edit:
Still does not work
{{orders}}
<div ng-repeat="order in orders track by $index">
<span>
{{order.item}}
</span>
and nothing
controller:
$scope.ListOfOrders = function () {
return ApiService.cart.list().then(function (resp) {
$scope.orders = resp[0].order;
console.log(resp[0].order)
});
}
UI
<div ng-repeat="order in orders track by $index">
<span>{{order.item}}</span>
</div>
Controller
$scope.orders = [{
"item": "DELL",
"quantity": 6,
"price": 144
}, {
"item": "Samsung",
"quantity": 3,
"price": 3131
}, {
"item": "222222",
"quantity": 1,
"price": 31
}, {
"item": "111111111",
"quantity": 1,
"price": 13
}]
Working Solution
You don't need to bind order[$index], ng-repeat itself iterates over every object so just write :
<div ng-repeat="order in orders track by $index">
<span>{{ order.item }}
</span>
</div>
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});
};
I have searched the web and found no example. I wonder if someone could help.
I have two instances of a dropdown or maybe more and I want to apply the add input dynamically to only that form but it applies to everyone with that ng-model everywhere on the web page.
The code is here https://plnkr.co/edit/PPDYKjztPF528yli9FbN
HTML:
<div class="main-content__right" ng-controller="QuestionController">
<div ng-repeat="element in questionList">
<fieldset>
<div ng-repeat="choice in formOptionData track by $index">
<a class="remove-field remove u-pull-right" ng-click="remove()">Remove</a>
<input id="input{{choice.name}}" placeholder="{{choice.label}}" type="number" name="{{choice.name}}">
</div>
<div id="add-more" class="well">
<div class="field">
<div style="width:100%;" class="dropdown">
<select name="{{options.name}}" id="select" data-ng-model="selectedValue" data-ng-options="options as options.label for options in element.inputElement | orderBy:'label'" ng-change="onCategoryChange(selectedValue)">
<option value="" data-id="null" disabled="" selected="">Select an item...</option>
</select>
</div>
</div>
{{formData}}
</div>
</fieldset>
</div>
</div>
APP.js
var app = angular.module("cab", []);
app.controller('QuestionController', function($scope) {
$scope.formOptionData = [];
$scope.selectedValue = {};
$scope.questionList = [{
"sectionTitle": "Travel",
"inputType": "select",
"inputElement": [{
"label": "Public transport",
"name": "travelOutgoing",
"helpInfo": "include train journeys",
"type": "select"
}, {
"label": "Taxis",
"name": "travelOutgoing",
"type": "select"
}
]
},
{
"sectionTitle": "Leisure",
"title": "Enter the amount you spend on entertainment and leisure. Leave them blank if they don\"t apply to you.",
"inputType": "select",
"inputElement": [
{
"label": "Eating out",
"name": "leisureOutgoing",
"helpInfo": "Include coffees, teas and snacks",
"type": "select"
},
{
"label": "Going out",
"name": "leisureOutgoing",
"helpInfo": "Include drinks, taxis, admission charges",
"type": "select"
}
] }
];
$scope.onCategoryChange = function(selectedItem) {
this.formOptionData.push(selectedItem);
};
$scope.remove = function(element) {
this.formData.splice(element,1);
};
});
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>
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.