Angular Display Data - angularjs

I have a form with a dropdown element. The value of the options in the select are IDs from another table. When I click save/add, I save the data to the server, and I add the data to a dynamic table under the form.
In the table I want to display the corresponding name, as in the dropdown element, but I only have the ID which was saved. What is the best method or common practices to display the name?
Example:
Dropdown:
1: Jackson Middle School
2: St Thomas High School
3: Jackson High School
Display table using ng-repeat:
{{school}} *Displays 1,2,3 not 'Jackson Middle School'

Assuming you send a GET request with the following result:
[
{
"Id":1,
"Name":"Jackson Middle School"
},
{
"Id":2,
"Name":"St Thomas High School"
},
{
"Id":3,
"Name":"Jackson High School"
}
]
and you assign this to $scope.Schools, you should be able to use it like this:
<li ng-repeat="school in Schools">{{ school.Name }}</li>

I just created an array "school_names = [1: Jackson Middle School, 2: St Thomas High School, 3: Jackson High School]" and in the table i just did an array look up {{school_names[school]]}}

Related

Is there a way to filter the columns I'm getting from CDS in a Logic App?

Is there a way to filter the columns I'm getting from CDS in a Logic App, something similar to SELECT Column1, Column2 FROM table instead of SELECT * FROM table.
I have tried $select=Column1,Column2 in Filter Query without any success.
===== UPDATE =====
I'm dealing with large amounts of data and I'm trying to avoid getting throttled(Logic App Content throughput limit per 5 minutes: 600MB). Filtering out all the unnecessary fields means the Logic App will get only KB of data from CDS instead of hundreds of MB
For your requirement, I think it is difficult to implement if we just use a common action in logic app. But if we use liquid template, it will be easy, please refer to the steps I provdied below:
1. You need to create an integration account in your azure portal (I think "Free" pricing tier is enough)
2. In my logic app, I use "List records" action to get data from CDS. My data show like below:
{
"#odata.context": "https://logic-apis-eastasia.azure-apim.net/apim/commondataservice/xxxxxxxxxx/$metadata#datasets('orgd46a4820.crm')/tables('activitypointers')/items",
"value": [
{
"#odata.id": "xxxxxxxxxx",
"#odata.etag": "",
"ItemInternalId": "xxxxxxxxxx",
"statecode": 1,
"_statecode_label": "Completed",
.......
},
{
"#odata.id": "xxxxxxxxxx",
"#odata.etag": "",
"ItemInternalId": "xxxxxxxxxx",
"statecode": 1,
"_statecode_label": "Completed",
.......
},
.....
]
}
For example, I just want two columns(ItemInternalId and statecode) of the data as your requirement.
To implement the requirment get two columns(ItemInternalId and statecode) of the data, we need to write a liquid template. The code show as below:
{
"value": [
{% for item in content %}
{
"ItemInternalId": "{{item.ItemInternalId}}",
"statecode":"{{item.statecode}}"
},
{% endfor %}
]
}
Please save the liquid template in local with .liquid as its suffix (such as map.liquid).
3. Go to your integration account and click "Maps".
Then click "Add" and upload the map.liquid to integration account.
4. Go to your logic app and click "Workflow settings" tab and choose the integration account which we created just now.
5. In your logic app designer, add a "Transform json to json" action. Use the value from "List records" as the "Content" box and choose the map which we upload to integration account.
6. Run the logic app, we can see the result show as below (contains only two columns).
====================================Update================================
After "List records" action, we can use a "Parse JSON" action to parse the value from "List records".
Then use "Select" action to select the columns which you want.

Best design for Category Collection

I am developing a blog website (to practice for ReactJS).
In my website, I have a slide bar will show categories and number of posts in each category.
So, how should i design category collection?
Having a field named numPosts to store total posts in that category.
And whenever i create new post document, i must increase numPost
field in it.
Don't store total posts in category collection. Everytime i want to
fetch number of posts, i have to browser post collection and count.
Is there any best design for this case?
Thank you in advance for your help!
Sorry about my English.
This is better to count the posts (via count or $count) instead of create "numPosts" field,
don't act like relational databases , following schema is a good one for what you want to do:
{
"_id" : ObjectId("5e60df95d7ff5f38b4d44d8c"),
"nameOfCategory": "Coronavirus",
"postsInsideThisCategory: [{
"titleOfPost": "Is there any cure?",
"description": "Bla Bla Bla Bla..... and Bla"
},{
"titleOfPost": "What is the type of coronavirus?",
"description": "Bla Bla Bla Bla..... and Bla"
}]
}
Then just use count or $count to count the number of documents inside the array of "postsInsideThisCategory" field.

Where to store an array of object, which is part of an another object in angular2?

I have a companies' object. In this object, I have some other object..
object{
country {
country_code: string;
country_name: string
},
yearlyIncome {
id: number;
amount: string;
},
...
}
In my project you can create new companies, or edit an existing one. I use this objects there in dropdown lists. So you not writing this datas in to a textfield, you select countries in the dropdown list, select yearly income category, like 0-1000 USD, 1000-5000 USD, ... .
So this objects have default datas, but I don't know where to store this datas. I don't thinks this is good idea to store this datas in a database, bacause, this datas will not change. So I want to write the dafault datas to the code. But where to? Create a model and a service for each object? But if I do, there will be a lot of models and services. Store it in the company service? But then the company service will be very long.
How should I solve this problem?
Store the default values in a database and use the service to get the values :
ngOnInit(){
this.countries$ = this.companyService.getDefaultCountries();
this.incomeRanges$ = this.companyService.getDefaultIncomeRanges();
}
Make sure that your backend caches these values.
If you are using Express.js, you could use apicache
Then in your template, you can use this to initialize your dropdowns
<select>
<option *ngFor="let country of countries$ | async" value="country">{{country.name}}</option>
</select>

Filter ng-repeat more times

I'm creating an application to manage restaurant orders.
I create the menu from $http so I've this list:
<div class="row vertical" style="background-image: url(/gest/images/etichette/ANTIPASTI.png);border-color: #0CF">
<div class="card piatti col s2" ng-repeat="anti in antis | filter:{tipo:'ANTIPASTI'}">
<div class="card-content"> <span class="card-title truncate red darken-3">{{anti.piatto}}</span> </div>
<div class="card-action"> {{n}}</div>
</div>
</div>
The div with class "row vertical" contain one time starters, then pasta, then beef ecc.
So I use ng-repeat each time, and filter by tipo.
My question is: is there any way to make ng-repeat only one time to show all menu (orderer before by starters, then pasta, beef ecc)?
I have this data (is a restaurant menu):
piatto: name of the the dish
tipo: category of the dish (like pasta, beef, fish, starters ecc)
I would show with only one repeat all the dishes ordered so:
starters, pasta, beef, fish, dessert etc.
And I would create each time a new row
From what I understand you already have all your date on the antis and you just want to filter it by type or do you want to OrderIt by a certain type?
This fiddle for example would order by name, but you can also provide an array with functions to retrieve each type in the way that you like, you can read about it here.
But basically you'd do
anti in antis | orderBy:'+tipo'
or
anti in antis | orderBy: [ function(){}, function(){} ]
EDIT:
As #yarons mentioned you can also chain strings to filter even further. I've updated the Fiddle so now the filter would be anti in antis | orderBy:['+tipo', '+piato']" which indicates that first the tipo would be alphabetically ordered ascending (+ indication) and after that the piato would also be alphabetically ascending.
If you'd want to define a different order than the alphabetical one I think you can use a sort of ENUM for the tipo as in:
var tipoENUM = {};
tipoENUM['ANIPASTI'] = 0;
tipoENUM['PASTA'] = 1;
tipoENUM['PIZZA'] = 2;
tipoENUM['BEEF'] = 3;
tipoENUM['DESERT'] = 4;
So that way you'd avoid using the string for the order, see following fiddle for the example.
EDIT 2:
Ok, so if you receive the data via the HTTP request it's better if you create a order function to help you, check this updated fiddle, like so:
// The enum would be defined as before but:
$scope.orderTipo = function (dish) {
return tipoENUM[dish.tipo];
}
On the HTMl you'll do:
ng-repeat="anti in antis | orderBy:[orderTipo, '+piato']"
Ok your example is perfect but I would repeat each time the "tipo" and then the relative "piato" in a list....something like this:
ANTIPASTI
- bruschetta
- suppli
- fritto
PRIMI
- caqrbonara
- amatriciana
etc.
Is it possible?

MongoDB: Query and retrieve objects inside embedded array?

Let's say I have the following document schema in a collection called 'users':
{
name: 'John',
items: [ {}, {}, {}, ... ]
}
The 'items' array contains objects in the following format:
{
item_id: "1234",
name: "some item"
}
Each user can have multiple items embedded in the 'items' array.
Now, I want to be able to fetch an item by an item_id for a given user.
For example, I want to get the item with id "1234" that belong to the user with name "John".
Can I do this with mongoDB? I'd like to utilize its powerful array indexing, but I'm not sure if you can run queries on embedded arrays and return objects from the array instead of the document that contains it.
I know I can fetch users that have a certain item using {users.items.item_id: "1234"}. But I want to fetch the actual item from the array, not the user.
Alternatively, is there maybe a better way to organize this data so that I can easily get what I want? I'm still fairly new to mongodb.
Thanks for any help or advice you can provide.
The question is old, but the response has changed since the time. With MongoDB >= 2.2, you can do :
db.users.find( { name: "John"}, { items: { $elemMatch: { item_id: "1234" } } })
You will have :
{
name: "John",
items:
[
{
item_id: "1234",
name: "some item"
}
]
}
See Documentation of $elemMatch
There are a couple of things to note about this:
1) I find that the hardest thing for folks learning MongoDB is UN-learning the relational thinking that they're used to. Your data model looks to be the right one.
2) Normally, what you do with MongoDB is return the entire document into the client program, and then search for the portion of the document that you want on the client side using your client programming language.
In your example, you'd fetch the entire 'user' document and then iterate through the 'items[]' array on the client side.
3) If you want to return just the 'items[]' array, you can do so by using the 'Field Selection' syntax. See http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelection for details. Unfortunately, it will return the entire 'items[]' array, and not just one element of the array.
4) There is an existing Jira ticket to add this functionality: it is https://jira.mongodb.org/browse/SERVER-828 SERVER-828. It looks like it's been added to the latest 2.1 (development) branch: that means it will be available for production use when release 2.2 ships.
If this is an embedded array, then you can't retrieve its elements directly. The retrieved document will have form of a user (root document), although not all fields may be filled (depending on your query).
If you want to retrieve just that element, then you have to store it as a separate document in a separate collection. It will have one additional field, user_id (can be part of _id). Then it's trivial to do what you want.
A sample document might look like this:
{
_id: {user_id: ObjectId, item_id: "1234"},
name: "some item"
}
Note that this structure ensures uniqueness of item_id per user (I'm not sure you want this or not).

Resources