Order post By Date inside ng-repeat in View - angularjs

I am working on an angular app . here i need to show data in date order for user timeline
JS:
$scope.comments = [{"_id":"535e912cc6b93c7b30479454",
"created_at":"2014-04-28 23:04:36",
"post_id":"535e912cc6b93c7b30479453",
"type":"story",
"story":"[{"_id":"535e912cc6b93c7b30479454",
"title":"test story",
"content":"this is my story...",
"created_at":2014-04-30 22:04:36"}]",
"article":"[]"
},
{"_id":"535e912cc6b93c7b30479454",
"created_at":"2014-04-25 23:04:36",
"post_id":"535e912cc6b93c7b30479453",
"type":"article"
"article":"[{"_id":"535e912cc6b93c7b30479454",
"title":"test article",
"content":"this is my article...",
"created_at":2014-04-30 22:04:36"}]",
"story":"[]"
},----
//so on
]
View:
<div ng-repeat = "data in comments">
<div class="time">
<span>{{data.created_at | DateFiltertoIso | date:'MMM-dd-yy'}}
<div>
<div ng-switch-on="data.type">
<div ng-switch-when="story">
//switch layout adn get further story data by post_id
</div>
</div>
<div ng-switch-on="data.type">
<div ng-switch-when="article">
//switch layout adn get further article data by post_id
</div>
</div>
</div>
Expected Output is like
Apr-04-14
post1
post2
/all post for that date
Apr-01-14
post5
post6
//so on
I am getting
Apr-04-2014
post1
Apr-04-2014
post2
Apr-04-2014
post3
I am newbie to angular. Any Help would be helpfull

Try this
Working Demo
Html
<div ng-controller="MyCtrl">
<div ng-repeat="data in flattenedResults">
<div class="time"> <span>
<b>{{data.created_at | badDateToISO | date:'MMM-d-yy'}} </b></span>
<div>
<u ng-show="data.story.length>0">Stories</u>
<div ng-repeat="story in data.story">
{{story.title}}{{story.created_at}}
</div>
<u ng-show="data.article.length>0">Articles</u>
<div ng-repeat="article in data.article">
{{article.title}}
</div>
Script
var myApp = angular.module('myApp', []);
myApp.filter('badDateToISO', function () {
return function (badTime) {
var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");
return goodTime;
};
});
myApp.controller('MyCtrl', function ($scope) {
$scope.flattenedResults = [];
$scope.comments = [{
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "story",
"story": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test story 1",
"content": "this is my story... 2",
"created_at": "2014-04-30 22:04:36"
}],
"article": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 33",
"content": "this is my article... 33",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-25 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 1",
"content": "this is my article... 1",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "story",
"story": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test story 2",
"content": "this is my story... 2",
"created_at": "2014-04-30 22:04:36"
}],
"article": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-25 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 2",
"content": "this is my article... 2",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}];
$scope.jsonFlatten = function () {
$scope.dates = [];
$scope.comments.reduce(function (result, item) {
$scope.dates.push(item.created_at);
}, 0);
$scope.dates = _.uniq($scope.dates);
angular.forEach($scope.dates, function (dateValue, dateKey) {
var obj = {};
obj.created_at = dateValue;
var articleValues = [];
var storyValues = [];
angular.forEach($scope.comments, function (value, key) {
if (dateValue === value.created_at) {
obj._id = value._id;
obj.post_id = value.post_id;
if (value.type === 'story') {
angular.forEach(value.story, function (storyValue, storyKey) {
storyValues.push(storyValue)
});
} else if (value.type === 'article') {
angular.forEach(value.article, function (articleValue, articleKey) {
articleValues.push(articleValue)
});
}
}
});
obj.story = storyValues;
obj.article = articleValues;
$scope.flattenedResults.push(obj);
});
};
$scope.jsonFlatten(); //calling the method jsonFlatten() and making the flatten json
});

Related

how to get lasted record from array from remove duplicate array

I have following method for removing duplicate record from array, I want latest name record by the updatedDatetime. How to sort records with last update date-time? I want to show latest record.
var list = removeDuplicates( listjson, 'user_id');
function removeDuplicates(originalArray, objKey) {
var trimmedArray = [];
var values = [];
var value;
for(var i = 0; i < originalArray.length; i++) {
value = originalArray[i][objKey].name;
if(values.indexOf(value) === -1) {
trimmedArray.push(originalArray[i]);
values.push(value);
}
}
return trimmedArray;
}
{
"_id": "59edd7c5ff809c1c4c7a43c2",
"updatedDatetime": "2017-11-08T11:51:33.106Z",
"createdDatetime": "2017-11-08T11:51:33.106Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1090",
"name": "sami"
},
"__v": 0
} {
"_id": "59e9e039d0a8251c7cf29f98",
"updatedDatetime": "2017-11-08T11:28:33.401Z",
"createdDatetime": "2017-11-08T11:28:33.401Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
} {
"_id": "59f42b6823fcdc31b4185246",
"updatedDatetime": "2017-11-07T07:02:00.841Z",
"createdDatetime": "2017-11-07T07:02:00.841Z",
"message": "hi",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "sami"
},
"__v": 0
} {
"_id": "59f1c143a1f0ce2114700ef4",
"updatedDatetime": "2017-11-06T11:04:35.140Z",
"createdDatetime": "2017-11-06T11:04:35.140Z",
"message": "mobile user",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
}]
For Ascending Order:
<div ng-repeat="record in records | orderBy : 'updatedDatetime'">
{{ record.updatedDatetime | date : 'dd/MM/yyyy' }}
</div>
For Descending Order use -
<div ng-repeat="record in records | orderBy : '-updatedDatetime'">
{{ record.updatedDatetime | date : 'dd/MM/yyyy' }}
</div>
There are few commas missing in your array of records .
If you want the latest updatedTime then you will have to sort your array :
see below an example .
Solution 1 :
If you want to order it in html:
var app = angular.module('myAngularApp', []);
app.controller('test', function ($scope,$q) {
$scope.arr = [{
"_id": "59edd7c5ff809c1c4c7a43c2",
"updatedDatetime": "2017-11-08T11:51:33.106Z",
"createdDatetime": "2017-11-08T11:51:33.106Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1090",
"name": "sami"
},
"__v": 0
}, {
"_id": "59e9e039d0a8251c7cf29f98",
"updatedDatetime": "2017-11-08T11:28:33.401Z",
"createdDatetime": "2017-11-08T11:28:33.401Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
} ,{
"_id": "59f42b6823fcdc31b4185246",
"updatedDatetime": "2017-11-07T07:02:00.841Z",
"createdDatetime": "2017-11-07T07:02:00.841Z",
"message": "hi",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "sami"
},
"__v": 0
}, {
"_id": "59f1c143a1f0ce2114700ef4",
"updatedDatetime": "2017-11-06T11:04:35.140Z",
"createdDatetime": "2017-11-06T11:04:35.140Z",
"message": "mobile user",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
}]
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myAngularApp">
<div ng-controller="test">
<div ng-repeat = "a in arr|orderBy:'updatedDatetime' : true">
{{a.updatedDatetime}}
</div>
</div>
</body>
</html>
Solution 2 :
If you want to do it in your js:
var app = angular.module('myAngularApp', []);
app.controller('ExampleController', ['$scope', 'orderByFilter', function($scope, orderBy) {
$scope.arr = [{
"_id": "59edd7c5ff809c1c4c7a43c2",
"updatedDatetime": "2017-11-08T11:51:33.106Z",
"createdDatetime": "2017-11-08T11:51:33.106Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1090",
"name": "sami"
},
"__v": 0
}, {
"_id": "59e9e039d0a8251c7cf29f98",
"updatedDatetime": "2017-11-08T11:28:33.401Z",
"createdDatetime": "2017-11-08T11:28:33.401Z",
"message": "test sms for mobile",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
} ,{
"_id": "59f42b6823fcdc31b4185246",
"updatedDatetime": "2017-11-07T07:02:00.841Z",
"createdDatetime": "2017-11-07T07:02:00.841Z",
"message": "hi",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "sami"
},
"__v": 0
}, {
"_id": "59f1c143a1f0ce2114700ef4",
"updatedDatetime": "2017-11-01T11:04:35.140Z",
"createdDatetime": "2017-11-09T11:04:35.140Z",
"message": "mobile user",
"user_id": {
"_id": "59f07d5c935f27764c8d1091",
"name": "zami"
},
"__v": 0
}]
$scope.propertyName = 'updatedDatetime';
$scope.reverse = true;
$scope.arr = orderBy($scope.arr, $scope.propertyName, $scope.reverse);
}]);
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myAngularApp">
<div ng-controller="ExampleController">
<div ng-repeat = "a in arr ">
{{a.updatedDatetime}}
</div>
</div>
</body>
</html>

How to query critical JSON into ionic

I received the JSON response from the server mentioned below.
{
"employeeId": null,
"id": "DB06442E-2993-4FE8-B496-5A0CF61C8342",
"message": null,
"objects": [
{
"Children": [],
"Fields": [
{
"Key": "CallID",
"Value": 1000
},
{
"Key": "CallDate",
"Value": "Sep 9 2016 10:14AM"
},
{
"Key": "ClientName",
"Value": ""
},
{
"Key": "AssetName",
"Value": "Automatic Cold Cranking Simulator"
},
{
"Key": "CallCategory",
"Value": "Corrective Maintenance"
}
],
"Type": 8
},
{
"Children": [],
"Fields": [
{
"Key": "CallID",
"Value": 1000
},
{
"Key": "CallDate",
"Value": "Sep 9 2016 10:20AM"
},
{
"Key": "ClientName",
"Value": ""
},
{
"Key": "AssetName",
"Value": "Auto Mini Pour Point Tester "
},
{
"Key": "CallCategory",
"Value": "Preventive Maintenance"
}
],
"Type": 8
},
{
"Children": [],
"Fields": [
{
"Key": "CallID",
"Value": 1000
},
{
"Key": "CallDate",
"Value": "Sep 9 2016 10:23AM"
},
{
"Key": "ClientName",
"Value": ""
},
{
"Key": "AssetName",
"Value": "Balance - Citizon CX 220"
},
{
"Key": "CallCategory",
"Value": "Calibration"
}
],
"Type": 8
},
{
"Children": [],
"Fields": [
{
"Key": "CallID",
"Value": 1001
},
{
"Key": "CallDate",
"Value": "Sep 9 2016 10:26AM"
},
{
"Key": "ClientName",
"Value": ""
},
{
"Key": "AssetName",
"Value": "Others"
},
{
"Key": "CallCategory",
"Value": "Installation"
}
],
"Type": 8
}
],
"success": true
}
Can you please explain me bit more as per my json structure.
myhtml.html
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="item in callItems" type="item-text-wrap" ng-click="doTask()">
<h3 style="color:black;">{{item.CallID}}</h3>
<h3 style="color:black;">{{item.CallDate}}</h3>
<p style="color:black;">{{item.ClientName}}</p>
<p style="color:black;">{{item.AssetName}}</p>
<p style="color:black;">{{item.CallCategory}}</p>
</ion-item>
myjs.js
$http.post("http://testCrm.com/GetAllObjects",
{"objectId":null,"objects":null,"searchParams":null,"sessionId":"DB06442E-2993-4FE8-B496-5A0CF61C8342","type":8})
.success(function(data) {
alert("SUCCESS!");
$rootScope.callItems = data;
console.log($rootScope.callItems);
})
.error(function(data) {
alert("ERROR");
alert(data);
});
As per my code I can able to get the response But How can i parse the value in View ?
you can follow this also
<div ng-repeat="(key, value) in item.Fields">
<h3 style="color:black;"> {{value.Value}}</h3>
</div>
<button ng-click=something($index,item.Fields[0].Value)>check it</button>
</div>
js
add the below function and see u will get a alert with index
$scope.something=function(a,b){
alert(a);//alerts index
alert(b);//alerts CaalID of that index
}
working codepen
new requirments
this might not be the best solution but it's a working demo. i think you need to change the json object structure again and re create it according to your requirement.
<div ng-repeat="item in callItems.objects">
<div ng-repeat="fi in item" >
<div ng-repeat="kk in fi">
<h3 style="color:black;" ng-if="kk.Key == 'CallID'">{{kk.Value}}</h3>
<h3 style="color:black;" ng-if="kk.Key == 'CallDate'">{{kk.Value}}</h3>
<h3 style="color:black;" ng-if="kk.Key == 'ClientName'">{{kk.Value}}</h3>
<h3 style="color:black;" ng-if="kk.Key == 'AssetName'">{{kk.Value}}</h3>
<h3 style="color:black;" ng-if="kk.Key == 'CallCategory'">{{kk.Value}}</h3>
</div>
</div>

Pull value from JSON key value pair using AngularJS

Is there a nice way in Angular's ngRepeat to pull a value out of a JSON Array depending on its key. I working with the following JSON structure and would like to output the associated value of "Name 2" for each object.
{
"Items": [
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
}
try like this
angular.forEach(data.Items,function(value,key){
angular.forEach(value,function(v){
if(v.name == "Name 2")
console.log(v);
}
})
Here is an example. It will print the Value for "Name 2":
<body ng-app="myApp">
<div ng-controller="myCtrl">
<div ng-repeat="item in records.Items">
<div ng-repeat="singleAttribute in item.Attributes">
<div ng-if="singleAttribute.Name === 'Name 2'">
{{singleAttribute.Value}}
</div>
</div>
</div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = {
"Items": [
{
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
};
});
</script>
</body>

group subdocuments - angularjs

i have object value in JSON, within which it contains array.i am trying to group the subdocument but does not work. my plunk
The below is my controller code.
Controller
app.controller('MainCtrl', function($scope) {
$scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}
});
The below is my HTML code
HTML
<p ng-repeat="(key,value) in list.orderfood | groupBy:'name'">
{{key}}
</p>
my plunk
Dont know what you want your ouput to be but you need to do this with a custom function:
<div ng-app ng-controller="Main">
<div ng-repeat="list in itemsToFilter() | filter:filterNames">
<b>{{list.name}}</b>
<li ng-repeat="item in itemsToFilter() | filter: {name: list.name}">NAME: {{item.name}}- PRICE: {{item.price}}, etc...</li>
</div>
</div>
Then in your controller:
function Main($scope) {
$scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}
var indexedTeams = [];
$scope.itemsToFilter = function() {
indexedTeams = [];
return $scope.list.orderfood;
}
$scope.filterNames = function(item) {
var nameIsNew = indexedTeams.indexOf(item.name) == -1;
if (nameIsNew) {
indexedTeams.push(item.name);
}
return nameIsNew;
}
}
here is a link...js-fiddle exmple: http://jsfiddle.net/L6cQN/305/

Loading json data into angularjs ng-repeat and outside of ng-repeat from same json file

i'm new to angular and a little lost. i'm able to load the data and display it, but i'm confused about loading other data that is not part of the ng-repeat i have set up from the same json file. here is what i have so far.
i would like to not use $scope.titleName = "John Doe"; $scope.infotext = "some more info" and have this info come from within the json file.
thanks for any help you can give.
var app = angular.module("myApp", []);
app.controller('something', function ($scope, $http){
$http.get('site.json').success(function(data) {
$scope.sites = data;
$scope.titleName = "John Doe";
$scope.infotext = "some more info"
});
});
/* json file content */
[
{
"name": "tim",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
},
{
"name": "don",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
},
{
"name": "Mike",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
},
{
"sub": {"titleName": "Homer", "infotext":"two"}
}
]
<body ng-app="myApp" ng-controller="something" >
<div class="wrapper">
<h1>{{titleName}}</h1>
<p class="info">{{infotext}}</p>
<div class="big" ng-repeat="site in sites ">
<a href="{{site.link}}" target="_blank">
<img src="{{site.image}}">
</a>
<h2>{{site.name}}</h2>
</div>
</div>
</body>
Change your JSON because the last object of the array is different from sites, I suggest to have a JSON like this :
{
"sites": [{
"name": "tim",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
}, {
"name": "don",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
}, {
"name": "Mike",
"image": "/features/home-1.jpg",
"link": "http://www.bbc.co.uk"
}],
"sub": {
"titleName": "Homer",
"infotext": "two"
}
}
You have also to modify your controller
app.controller('something', function ($scope, $http){
$http.get('site.json').success(function(data) {
$scope.sites = data.sites;
$scope.titleName = data.sub.titleName;
$scope.infotext = data.sub.infotext;
});
});

Resources