Angularjs select - set initial selection based on model - arrays

I have an array of job objects that gets pulled from the server, and an array of people objects also pulled from the server. I want the persons job to be represented by a select element, populated from the jobs array. I have populated the select using ng-options, and when changing the selection, the person model's job object is updated. The only issue I have is that to start with the select shows an empty value. I can't work out how to have it show the person objects current job?
<select
ng-model='person.job'
ng-options='job.title for job in jobs'>
</select>
the person object looks like this
{
"id": "1",
"firstName": "Marianne",
"lastName": "Jenkins",
"middleNames": null,
"ext": "4680",
"phoneCell": "1-174-668-3846",
"phoneHome": "+10(2)5744088105",
"takerNumber": "180",
"hidden": "0",
"created_at": "2014-01-09 12:55:12",
"updated_at": "2014-01-09 12:55:12",
"job": {
"id": "25",
"title": "Office Manager",
"created_at": "2014-01-09 12:55:11",
"updated_at": "2014-01-09 19:25:03"
},
"office": {
"id": "4",
"name": "Salt Lake City",
"prefix": "702",
"order": "2",
"takerNumber": "103",
"address_id": "1"
}
}
and the jobs array looks like this
[
{
"id": "1",
"title": "Field Service Tech",
"created_at": "2014-01-09 12:55:11",
"updated_at": "2014-01-09 19:25:03"
},
{
"id": "2",
"title": "Inside Sales Manager",
"created_at": "2014-01-09 12:55:11",
"updated_at": "2014-01-09 19:25:03"
},
{
"id": "3",
"title": "Office Assistant",
"created_at": "2014-01-09 12:55:11",
"updated_at": "2014-01-09 19:25:03"
},
...
{
"id": "25",
"title": "Office Manager",
"created_at": "2014-01-09 12:55:11",
"updated_at": "2014-01-09 19:25:03"
}
...
]
Hope I've provided enough information and sorry if my description is a bit unclear, I struggled a bit to accurately describe my problem here.
Thanks in advance!
Note: The person shown here was generated and is not real.

please try this:
<select
ng-model='person.job'
ng-options='job.title for job in jobs track by job.id'>
</select>
e.g. extends the expression with: track by job.id

Related

How to Break json object in a for each

How can one split the below json example by different email in a for each loop? So the end result is two completely separate entities keeping there own objects and arrays.
This result comes from a sql dump but the sql connector alters the output, so I've had to parse that output in a for each loop of its own even though there is no loop it's all in one line.
[
{
"Email": "Steve#gmail.com",
"Name": "Steve Larson",
"ID": "1111",
"Date": "2022-09-12",
"Address": "10 Chicken Place",
"Town": "Gatebody",
"Postcode": "xxx 1xx",
"Data": [
{
"Ref": "34546",
"Transaction_Date": "2018-11-29",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep1"
},
{
"Ref": "34546v2",
"Transaction_Date": "2018-12-24",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep2"
}
]
},
{
"Email": "innis#gmail.com",
"Name": "Innis Blitz",
"ID": "3456",
"Date": "2022-10-12",
"Address": "33 Snake Road",
"Town": "March",
"Postcode": "cxc 3cd",
"Data": [
{
"Ref": "078776701",
"Transaction_Date": "2021-08-27",
"Amount": 984.68,
"Balance": 984.68,
"Notes": "Yes please"
},
{
"Ref": "078776701v2",
"Transaction_Date": "2021-08-27",
"Amount": 98422.6,
"Balance": 98432.6,
"Notes": "Please not now"
}
]
}
]
Would like to do a for each so it splits the above like so. The idea then for my to load the result of the for each into a template that takes arrays.
{
"Email": "Steve#gmail.com",
"Name": "Steve Larson",
"ID": "1111",
"Date": "2022-09-12",
"Address": "10 Chicken Place",
"Town": "Gatebody",
"Postcode": "xxx 1xx",
"Data": [
{
"Ref": "34546",
"Transaction_Date": "2018-11-29",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep1"
},
{
"Ref": "34546v2",
"Transaction_Date": "2018-12-24",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep2"
}
]
}
{
"Email": "innis#gmail.com",
"Name": "Innis Blitz",
"ID": "3456",
"Date": "2022-10-12",
"Address": "33 Snake Road",
"Town": "March",
"Postcode": "cxc 3cd",
"Data": [
{
"Ref": "078776701",
"Transaction_Date": "2021-08-27",
"Amount": 984.68,
"Balance": 984.68,
"Notes": "Yes please"
},
{
"Ref": "078776701v2",
"Transaction_Date": "2021-08-27",
"Amount": 98422.6,
"Balance": 98432.6,
"Notes": "Please not now"
}
]
}
I have reproduced in my environment and got expected results and I followed below process:
I have initialized your input as below:
Then i added for each control and send the output of previous step as input as inside of foreach loop, i have added send email action.
Outputs:
I have got tow jsons in two seperate emails as above.

How we can show collection data in array

When I am using get api and get the data in json format
{
"uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"name": "AMan",
"dispute_location": "Hyder",
"max_dispute_value": "200",
"min_dispute_value": "100",
"state": "U.p",
"district": "morene",
"calendar_id": "3",
"description": null,
"subject_matters": [
{
"id": 1,
"name": "A",
"created_at": "2020-08-14T12:24:52.000000Z",
"updated_at": "2020-08-14T12:24:52.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 1
}
},
{
"id": 2,
"name": "B",
"created_at": "2020-08-14T12:24:57.000000Z",
"updated_at": "2020-08-14T12:24:57.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 2
}
}
]
}
but i want subject_matters data in this format
"subject_matters": [1,2]
Api resource (court resources)
You can use ->pluck('id'); in your resource.
return [
'subject_matters_id' => $this->subjectMatters->pluck('id')
]
It should return an array of subject matter IDs.

Why are bonuses paid out by Coinbase set as type "send"?

This seems like a mistake in the classification of the transaction type. It should be a "buy" or a new type of some sort like "bonus", no?
{
"id": "c06b1025-d339-53f6-b439-f9b087502152",
"type": "send",
"status": "completed",
"amount": {
"amount": "0.00059300",
"currency": "BTC"
},
"native_amount": {
"amount": "9.99",
"currency": "USD"
},
"description": "Congrats! You just earned a $10 bonus for inviting your friend ****** to Coinbase. They received the same bonus. Earn more by inviting your friends with this link: https://www.coinbase.com/join/52957440cf2d06a92a0000bb",
"created_at": "2017-12-13T21:31:40Z",
"updated_at": "2017-12-13T21:31:40Z",
"resource": "transaction",
"resource_path": "/v2/accounts/6401de05-e7a3-5320-a2dc-867df3ff3c0a/transactions/c06b1025-d339-53f6-b439-f9b087502152",
"instant_exchange": false,
"network": {
"status": "off_blockchain"
},
"from": {
"id": "7fd10cd7-b091-5cee-ba41-c29e49a7cccf",
"resource": "user",
"resource_path": "/v2/users/7fd10cd7-b091-5cee-ba41-c29e49a7cccf",
"currency": "BTC"
},
"details": {
"title": "Received Bitcoin",
"subtitle": "From Coinbase"
}
}
a buy is a two-way transaction... you're giving something, and getting something
a send is a one-way transaction... either you're giving to someone, or someone is giving to you
coinbase is getting nothing from you (other than the friend you referred) but they are giving you money, so it's a simple send

Compare two array and change styles of ng-repeat items in angular

I have a json like this -
[{
"id": "152",
"name": "Accounting",
"parent_id": "0",
"created_at": "2016-12-20 18:10:57",
"updated_at": "2016-12-20 18:10:57"
}, {
"id": "292",
"name": "Administration",
"parent_id": "0",
"created_at": "2016-12-20 18:37:29",
"updated_at": "2016-12-20 18:37:29"
}, {
"id": "422",
"name": "Banquet",
"parent_id": "0",
"created_at": "2016-12-20 18:43:26",
"updated_at": "2016-12-20 18:43:26"
}, {
"id": "502",
"name": "Engineering & Maintenance",
"parent_id": "0",
"created_at": "2016-12-20 18:47:12",
"updated_at": "2016-12-20 18:47:12"
}, {
"id": "622",
"name": "Food & Beverage Admin",
"parent_id": "0",
"created_at": "2016-12-20 18:51:00",
"updated_at": "2016-12-20 18:51:00"
}, {
"id": "782",
"name": "Food & Beverage Service",
"parent_id": "0",
"created_at": "2016-12-20 18:56:38",
"updated_at": "2016-12-20 18:56:38"
}, {
"id": "982",
"name": "Front Office",
"parent_id": "0",
"created_at": "2016-12-20 19:05:04",
"updated_at": "2016-12-20 19:05:04"
}, {
"id": "1212",
"name": "Housekeeping & Laundry",
"parent_id": "0",
"created_at": "2016-12-20 19:15:48",
"updated_at": "2016-12-20 19:15:48"
}, {
"id": "1352",
"name": "HR",
"parent_id": "0",
"created_at": "2016-12-21 10:12:38",
"updated_at": "2016-12-21 10:12:38"
}, {
"id": "1462",
"name": "IT",
"parent_id": "0",
"created_at": "2016-12-21 10:16:14",
"updated_at": "2016-12-21 10:16:14"
}, {
"id": "1492",
"name": "Kitchen / Food Preparation",
"parent_id": "0",
"created_at": "2016-12-21 10:17:29",
"updated_at": "2016-12-21 10:17:29"
}, {
"id": "1712",
"name": "Purchase",
"parent_id": "0",
"created_at": "2016-12-21 10:26:09",
"updated_at": "2016-12-21 10:26:09"
}]
Which I am iterating in list with ng-repeat.
Now I have another array like
[152,292,422,1712].
Now how do I match the second array with the id of first array and change the style of those matching id's object in the view.
Please suggest.
You can use the ngClass directive.
The code would look something like this, assuming the Json is saved as foodList:
<div ng-repeat="food in foodList">
<div ng-class="{'classNameToWantedStyle': isIDInList(food.id)}">
{{food.name}}
</div>
</div>
and in your controller you would make a corresponding function that returns true if the id is in the list. Here you could use javascripts indextOf method to determine that.
Assuming 'items' is the name of the array, then you have your ng-repeat:
<div ng-repeat="item in items">
You can use ng-switch (or here) to look at each item then separate that out into sections that you can style differently:
<div ng-switch="item.id">
<div ng-switch-when="152" class="152-class"></div>
<div ng-switch-when="292" class="292-class"></div>
<div ng-switch-default class="default-class"></div>
</div>
You get the idea, but take a look at the documentation I linked, they might explain it better.

How Can I Filter this JSON in Backbone?

I populated data from two separate mongodb schemas into an object below:
They are user and article. How can I filter/sort so I can find articles by user._id in backbone?
This is for a single page blog type site in which each user has their own articles.
I was looking at _.filter and _.where functions in underscore, but am still new to this. Any help is appreciated. Thanks.
Here is an example on my server of what im trying to build (was done with embedded schema):
http://kevg.co:3700/demo1
and backbone model/view/collection code is here:
http://kevg.co:3700/javascripts/demoj17.js
//Individual Model
{
"user": {
"username": "ho",
"email": "hom#gmail.com",
"_id": "51be709a148846ec25000007"
},
"name": "money",
"articlebody": "",
"_id": "51c1033283376a5808000002",
"__v": 0,
"createdAt": "2013-06-19T01:02:42.424Z"
},
//Rest of data
{
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "bob",
"articlebody": "",
"_id": "51c89ab47596ef1018000001",
"__v": 0,
"createdAt": "2013-06-24T19:15:00.835Z"
},
{
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "sasa",
"articlebody": "sajdja",
"_id": "51c8a3bf341eb4141f000001",
"__v": 0,
"createdAt": "2013-06-24T19:53:35.233Z"
}
I'd have a collection of users as well as a collection of articles.
To get the collection of articles a specific user has posted, you could perform something akin to:
// theUser would be the user selected
articlesCollection.where({'User': theUser});
Thanks, Loamhoof for pointing out the reduction.
Note, your User model could also hold a collection of Articles the user has posted.
Assuming your data as given below
var data = [{
"user": {
"username": "ho",
"email": "hom#gmail.com",
"_id": "51be709a148846ec25000007"
},
"name": "money",
"articlebody": "",
"_id": "51c1033283376a5808000002",
"__v": 0,
"createdAt": "2013-06-19T01:02:42.424Z"
}, {
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "bob",
"articlebody": "",
"_id": "51c89ab47596ef1018000001",
"__v": 0,
"createdAt": "2013-06-24T19:15:00.835Z"
}, {
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "sasa",
"articlebody": "sajdja",
"_id": "51c8a3bf341eb4141f000001",
"__v": 0,
"createdAt": "2013-06-24T19:53:35.233Z"
}];
you can use the filter function in Array or the one in underscore to filter the data
function getArticles(userName) {
return _.filter(data, function (item) {
return item.user.username == userName; // give the property to be used for filtering
});
}
var kevArticles = getArticles("kev");
var hoArticles = getArticles("ho");
Or you can use a groupBy in underscore to group the data with respect to the user
function groupByUserName(){
return _.groupBy(data, function (item) {
return item.user.username; // give the property to be used for filtering
});
}
var groupedData = groupByUserName();
This will make it in the format given below
{
"ho": [{
"user": {
"username": "ho",
"email": "hom#gmail.com",
"_id": "51be709a148846ec25000007"
},
"name": "money",
"articlebody": "",
"_id": "51c1033283376a5808000002",
"__v": 0,
"createdAt": "2013-06-19T01:02:42.424Z"
}],
"kev": [{
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "bob",
"articlebody": "",
"_id": "51c89ab47596ef1018000001",
"__v": 0,
"createdAt": "2013-06-24T19:15:00.835Z"
}, {
"user": {
"username": "kev",
"email": "kevo#o.com",
"_id": "51be6fe9148846ec25000001"
},
"name": "sasa",
"articlebody": "sajdja",
"_id": "51c8a3bf341eb4141f000001",
"__v": 0,
"createdAt": "2013-06-24T19:53:35.233Z"
}]

Resources