Angular properties (variables) use in the view - angularjs

I have a list of schools and a list of teachers.
Schools
[
{
"id": 1,
"nameOfInstitution": "Summer Elementary",
"schoolLevel": "01304"
},
{
"id": 2,
"nameOfInstitution": "Grady Middle",
"schoolLevel": "02400"
}
]
Teachers
[
{
"id": "1",
"school": "1",
"name": {
"lastOrSurname": "HARRISON",
"firstName": "JOHN",
"middleName": ""
}
},
{
"id": "2",
"school": "1",
"name": {
"lastOrSurname": "Nelson",
"firstName": "Darren",
"middleName": ""
}
},
{
"id": "3",
"school": "2",
"name": {
"lastOrSurname": "Stewart",
"firstName": "Manuel",
"middleName": ""
}
}]
In the controller I have
...
$scope.schools = schools;
$scope.teachers = teachers;
The question is related to the view. How can I achieve this?
Summer Elementary
JOHN HARRISON
Darren Nelson
Grady Middle
Manuel Steward
I've been using the "ng-repeat" directive, but how can I display only the teachers that belong to each schools?
Thanks!

You can filter the teachers in your ng-repeat:
<div ng-repeat="school in schools">
<div ng-repeat="teacher in teachers | filter: {school: school.id}">
But be sure to add an id property for each school that are used by the teachers.
Or if the teacher.school properties are the schools index in the schools array, you can do that:
<div ng-repeat="school in schools">
<div ng-repeat="teacher in teachers | filter: {school: $index}">
A final note: this will work but if you have many schools and teacher, it would be more performant to perform the filtering from javascript before you display it, i.e. build a structure like
schools = [{
nameOfInstitution: ...,
schoolLevel: ...,
teachers: [{
lastOrSurname: ...
}]
// ... etc
}];

Use filter:
<div ng-repeat="school in schools">
{{school.nameOfInstitution}}
<p ng-repeat="teacher in teachers | filter:{school: ''+ ($index+1)}:true">
{{teacher.name.lastOrSurname}}
</p>
</div>
But you'd better have some sort of id for schools...

Related

Angularjs nested json object with ng-repeat not working at ionic

First of all, I'm pretty new to the ionic and angularjs. Now I trying to develop a app and i'm having trouble displaying and searching data right now.
I have a nested json object and I cannot display it with ng-repeat.
Here is the json object.
$scope.nestedTestdata = [{
"leadlist ": [{
"submitted_date": "01-01-2160",
"submission_value": {
"full_name": "Full Name ",
"phone": "This is test data.",
"source": "I hate you"
}
},
{
"submitted_date": "01-01-2015",
"submission_value": {
"full_name": "full name",
"phone": "phone.",
"source": "I really really hate you"
}
},
{
"submitted_date": "01-01-2016",
"submission_value": {
"full_name": "I am awesome",
"phone": "HP phone.",
"source": "I hate you"
}
}]
}];
Here is how I display.
<div class="lead_row" ng-repeat="data in nestedTestdata.leadlist | filter: model.search" >
<div class="lead_col">{{data.submitted_date | date : 'dd-MMM-yyyy'}}</div>
<div class="lead_col">{{data.submission_value.source}}</div>
<div class="lead_col">{{data.submission_value.full_name}}</div>
<div class="lead_col">{{data.submission_value.phone}}</div>
</div>
When I change it to following json object, it can display and search function also working.
$scope.testdata = [{
"submitted_date": "01-01-2160",
"submission_value": {
"full_name": "Search is working",
"phone": "This is fucking test data.",
"source": "I hate you"
}
},
{
"submitted_date": "01-01-2015",
"submission_value": {
"full_name": "Fucking full name",
"phone": "Fucking phone.",
"source": "I really really hate you"
}
},
{
"submitted_date": "01-01-2016",
"submission_value": {
"full_name": "I am awesome",
"phone": "Fucking HP phone.",
"source": "I hate you"
}
}];
So I think it is not displaying problem.
Please help to figure out how can i make this work.
Please check inside the following link for the full code.
http://play.ionic.io/app/720567016712
Thank you so much in advance.
nestedTestData contains an array. Inside the array, there is an object.
ng-repeat="data in nestedTestdata[0].leadlist | filter: model.search"
SO code should be as follows:
<div class="lead_row" ng-repeat="data in nestedTestdata[0].leadlist | filter: model.search" >
<div class="lead_col">{{data.submitted_date | date : 'dd-MMM-yyyy'}}</div>
<div class="lead_col">{{data.submission_value.source}}</div>
<div class="lead_col">{{data.submission_value.full_name}}</div>
<div class="lead_col">{{data.submission_value.phone}}</div>
</div>
So you have an array then you are defining another array with an object with a key of 'leadlist'. The answer above could solve your problem or you could just remove the array above your key you can just access it as normal data.nestedTestData
Below is how you could rewrite it:
`$scope.nestedTestdata = {
"leadlist" : [{
"submitted_date": "01-01-2160",
"submission_value": {
"full_name": "Full Name ",
"phone": "This is test data.",
"source": "I hate you"
}
},
{
"submitted_date": "01-01-2015",
"submission_value": {
"full_name": "full name",
"phone": "phone.",
"source": "I really really hate you"
}
},
{
"submitted_date": "01-01-2016",
"submission_value": {
"full_name": "I am awesome",
"phone": "HP phone.",
"source": "I hate you"
}
}]
};`
You can use nested ng-repeat, It will also work if there are multiple objects in $scope.nestedTestdata.
Here is working fiddle
<div ng-controller="MyCtrl">
<div class="lead_row" ng-repeat="data in nestedTestdata">
<div ng-repeat="list in data.leadlist">
<div class="lead_col">{{list.submitted_date | date : 'dd-MMM-yyyy'}}</div>
<div class="lead_col">{{list.submission_value.source}}</div>
<div class="lead_col">{{list.submission_value.full_name}}</div>
<div class="lead_col">{{list.submission_value.phone}}</div>
</div>
</div>
</div>
please remove the space after "leadlist "

I need to display an array of strings within ui-grid. Single row, one row for each ui-grid

I need to display each data from array in a single ui-grid row. Please help me, whether it's correct or not? so data[0] should display in one ui-grid and data[1] should display in another ui-grid, so it looks multiple ui-grid in a single page.
but pls help me this scenario with for each condition, becz i dnt knw how much datas am having.
Controller.js:
$rootScope.viewFollowup; // In this am having 'n' of datas only
$scope.gridView={};
$scope.data1= $.makeArray($rootScope.viewFollowup[0]);
$scope.data2= $.makeArray($rootScope.viewFollowup[1]);
$scope.data3= $.makeArray($rootScope.viewFollowup[2]);
//$scope.gridOptions -- in this grid options am having all the columndefs details
$scope.aa=$scope.gridOptions;
$scope.aa.data= $scope.data1;
$scope.ab= $scope.gridOptions;
$scope.ab.data= $scope.data2;
$scope.ac=$scope.gridOptions;
$scope.ac.data=$scope.data3;
$scope.gridView=[$scope.aa,$scope.ab,$scope.ac];
$rootScope.tabledata=$scope.gridView;
HTML:
<div class="row" ng-repeat="group in tabledata track by $index">
<div ui-grid="group" page-equalheight ui-grid-resize-columns ui-grid-selection ui-grid-auto-resize class="grid" style=style="height:250px"></div>
</div>
Output:
Grid View 1
-------------------------------------------
a| b| c|d|
-------------------------------------------
Grid View 2
-------------------------------------------
c| d| e|f|
-------------------------------------------
but what am getting is:
Grid View 1
-------------------------------------------
c| d| e|f|
-------------------------------------------
Grid View 2
-------------------------------------------
c| d| e|f|
-------------------------------------------
All ui-grid needs is a gridOptions object that contains data to show on the grid. You can put those gridOptions in an array and then loop over them in you markup using ng-repeat. Here is an example:
var gridOptions1 = {
data: [{
"name": "Ethel Price",
"gender": "female",
"company": "Enersol"
}, {
"name": "Claudine Neal",
"gender": "female",
"company": "Sealoud"
}, {
"name": "Beryl Rice",
"gender": "female",
"company": "Velity"
}, {
"name": "Wilder Gonzales",
"gender": "male",
"company": "Geekko"
}]
};
var gridOptions2 = {
data: [{
"name": "Georgina Schultz",
"gender": "female",
"company": "Suretech"
}, {
"name": "Carroll Buchanan",
"gender": "male",
"company": "Ecosys"
}, {
"name": "Valarie Atkinson",
"gender": "female",
"company": "Hopeli"
}, {
"name": "Schroeder Mathews",
"gender": "male",
"company": "Polarium"
}, {
"name": "Lynda Mendoza",
"gender": "female",
"company": "Dogspa"
}]
};
$scope.grids = [gridOptions1, gridOptions2];
and then in your markup you can use ng-repeat like:
<div ng-repeat='options in grids'>
<div id="grid1" ui-grid="options" class="grid"></div>
</div>
Here is a working plnkr of the example above.

ng-repeat with recursive attributes object

I'm currently learning angularjs 1.5.0
I'm making a small sample with a complex json object, like this :
{
"Families":[
{
"Name": "A",
"Age": 1,
"Children": [
{
"Name": "B",
"Age": 2,
"Children":[
{
"Name": "C",
"Age": 3
},
{
"Name": "D",
"Age": 4
}
]
}
]
},
{
"Name": "X",
"Age": 5,
"Children":[
{
"Name": "Y",
"Age": 2
},
{
"Name": "Z",
"Age": 4
}
]
}
]
}
As you see, Families contains many members, each member has name, age and children. I want to list out this family tree , but one thing is difficult to me is : I don't know how many children can a member have therefore I don't know how to list them out by using ng-repeat.
Can anyone help me please ?
Thank you so much,
For displaying recursive json data arrays, use recursive templates in views.
create a template like below
<script type="text/ng-template" id="familyTree">
{{ family .name}}
<ul ng-if="family.children">
<li ng-repeat="family in family.children" ng-include="'familyTree'">
</li>
</ul>
</script>
and use ng-repeat in view like this to render the view
<ul>
<li ng-repeat="family in families" ng-include="'familyTree'"></li>
</ul>
sample fiddle
more details

object nested into a nested array, how to implement a custom filter for it?

I have an application that I am constructing with a friend and we have a very big problem: I have a very big json with some nested arrays an objects, I am using a filter with an ng-model="search" the filter (search) works great with the top level array which is named sports, but once I try to search through the leagues array, the filter returns nothing. I saw in another question that I can search(filter) based on nested properties which is exactly what I want. I tried to follow the example answer on that question but I am having a problem trying to solve this. Someone else says: that is not possible with this kind of matching that you are trying because you want to filter through a matching sport.name and all of his matching and not non matching leagues or a non matching sport.name and only his matching leagues.
json
[
{
"name": "Cricket",
"leagues": []
},
{
"name": "NBA Games",
"leagues": [
{
"name": "NBA",
"sport": {
"id": 8,
"name": "NBA Earliest"
},
"lineType": "G",
"priority": [
1,
3
],
"part": "0"
}
]
},
{
"name": "COLLEGE Basketball",
"leagues": [
{
"name": "College - NCAA BASKETBALL",
"sport": {
"id": 24,
"name": "College Basketball"
},
"lineType": "G",
"priority": [
0,
4
],
"part": "0"
},
{
"name": "NCAA BASKETBALL ADDED GAMES",
"sport": {
"id": 24,
"name": "College Basketball"
},
"lineType": "G",
"priority": [
1,
4
],
"part": "0"
},
...
my html
<input type="search" ng-model="search">
<div ng-repeat="sport in sports | filter: query">
<!-- searching (filtering) great -->
<div>{{sport.name}}</div>
</div>
<div ng-repeat="league in sport.leagues | filter: query">
<!-- here is not filtering at all -->
{{league.name}}
</div>
</div>
can I do it this way I am trying or how do I implement a custom filter for this ?

Angular JS orderBy not working using dropdown

i am trying to do orderBy using dropdown value, but its not working :(
CODE:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.datas = [{
"id": "1",
"name": "name area 1"
}, {
"id": "2",
"name": "name area 2"
}, {
"id": "3",
"name": "name area 3"
}, {
"id": "4",
"name": "name area 4"
}];
$scope.dropdown = [{
"title": "Newest first",
"value": "true"
}, {
"title": "Oldest first",
"value": "reverse"
}];
});
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<ul ng-repeat="rows in datas | orderBy: 'id':orderByData">
<li>{{rows.id}} : {{rows.name}}</li>
</ul>
<select ng-model="orderByData" ng-options="x.value as x.title for x in dropdown"></select>
</div>
</div>
DEOM JS BIN
Why you use reverse?
Please use false instead reverse and it should work fine.
$scope.dropdown = [{
"title": "Newest first",
"value": "true"
}, {
"title": "Oldest first",
"value": "false"
}];
You don't need reverse parameter here. Instead you can do something like this:
<ul ng-repeat="rows in datas | orderBy: getSort()">
<li>{{rows.id}} : {{rows.name}}</li>
</ul>
Where getSort is defined as:
$scope.getSort = function() {
return $scope.orderByData ? 'id' : '-id';
};
Basically you want sort part to look like orderBy: 'id' or orderBy: '-id'.
Demo: http://jsbin.com/pepureta/1/edit?html,js,output

Resources