I have an json array of the form":
{
"School1": [{"name": "john", "age":"28"},
{"name": "paul", "age":"27"}],
"School2": [{"name": "david", "age":"27"},
{"name": "sil", "age":"28"}]
}
How do I display in the table in this form using ng-repeat in Angular:
----------
Name | Age
----------
School1
----------
john | 28
----------
paul | 2
----------
School2
----------
david| 27
----------
sil| 28
----------
I will Apreciate help with this.
You can perform some operation on your data and can create another object with the modified structure so that it can be used with ng-repeat to display the table in the form you want.
You can understand this better with an example.
This is your data that you want to display in table view
$scope.testData = {
"School1": [{"name": "john", "age":"28"},
{"name": "paul", "age":"27"}],
"School2": [{"name": "david", "age":"27"},
{"name": "sil", "age":"28"}]
}
We perform an operation on "testData" to identify the keys in this JSON Object which are "School1" and "School2".Following line performs that operation.
$scope.keys = Object.keys($scope.testData);
$scope.keys will be an array of keys : ["School1",School2"];
Then you can iterate on the array of keys and create another object that can be used with ng-repeat in HTML code.
Following lines perform create that object(tableData):
$scope.tableData=[];
//Iterating on array of keys
$scope.keys.forEach(element => {
$scope.tableData.push({
column : element,
data : $scope.testData[element] //Getting your data from testData
});
});
Final Code Snippet that will go in the controller will be :
$scope.testData = {
"School1": [{"name": "john", "age":"28"},
{"name": "paul", "age":"27"}],
"School2": [{"name": "david", "age":"27"},
{"name": "sil", "age":"28"}]
}
$scope.keys = Object.keys($scope.testData);
$scope.tableData=[];
$scope.keys.forEach(element => {
$scope.tableData.push({
column : element,
data : $scope.testData[element]
});
});
And your HTML Code will go something like this:
<table>
<tr>
<th>Name |</th>
<th>Age</th>
</tr>
</table>
<table ng-repeat="table in tableData">
<tr>
<th>{{table.column}}</th>
</tr>
<tr ng-repeat="row in table.data">
<td>{{row.name}} | </td>
<td>{{row.age}}</td>
</tr>
</table>
You can definitely beautify the table view by writing your own css or by including any predefined stylesheets.
Hope that helps!!
Related
I am trying to filter to just one store, given the store Id, which is picked in a dropdown.
HTML:
<tbody data-ng-repeat="store in orderVm.Stores | filter :{store.id=orderVm.Stores.selectedStore.id}">
I don't get how to set up the filter so that it only shows the one store with the specified id.
controller:
vm.Stores = json;// from file
vm.Stores.selectedStore = {
id: vm.Stores[0].Id,
name: vm.Stores[0].MarketplaceName
};
vm.Stores:
[
{
"Id": 1,
"MarketplaceId": 1,
"MarketplaceName": "Etsy"
]
},
{
"Id": 2,
"MarketplaceId": 2,
"MarketplaceName": "Shopify"
}
]
The error I get is unexpected%2C%20expecting%20%5B%3A%5D&p2=32&p3=orderVm.Stores%20%7CNaNilter%20%3A%7Bstore.id%3DorderVm.Stores.selectedStore.id%7D&p4=.id%3DorderVm.Stores.selectedStore.id%7D
Simple to filter your data by value:
<tbody data-ng-repeat="store in orderVm.Stores | filter:Vm.Stores.selectedStore.id">
It will get objects with properties have value is Vm.Stores.selectedStore.id. Because name not is a number (id), you can use this filter for it.
To specific filter by value of id:
<tbody data-ng-repeat="store in orderVm.Stores | filter:{Id:Vm.Stores.selectedStore.id}:true">
Syntax: filter:{propertyName:value}:true
true is for exact match.
I have an object as follows:
scope.items = {
"poor-John-1": {
"_id": "poor-John-1",
"name": "poor-John-1",
"sName": "room-poor-John-2"
},
"poor-John-2": {
"_id": "poor-John-2",
"name": "poor-John-2",
"sName": "room-poor-John-2"
}
}
I have rendered the object in the following way
<tr ng-repeat="item in items | orderObjectBy: '_id' track by item._id" ng-class="getStyle(item, next_item)"><td>{{item.sName}}</td></tr>
What I want to do is, pass, the current and next_item to the getStyle function, since its not an array, $index is not pointing the value. I have data in object.
Maybe you can do this with
$scope.keys = Object.keys($scope.items) // put keys on scope
// get next from keys
<tr ng-repeat="(key, item) in items track by item._id" ng-class="getStyle(item, items[keys[$index +1]])">
<td>{{key}}</td>
<td>{{item.name}}</td>
<td>next : {{items[keys[$index +1]]}}</td>
</tr>
Look this, tell me if it's what you want https://plnkr.co/edit/GCNmWb9Qv2mntqTnGsX1?p=preview
I am trying to sort a list that I have
{
Student: 100,
Student1: 60,
Student2: 90,
}
I am using a table and I need to sort the by their value. It currently sorts by the keys
<tr ng-repeat="(key, value) in ctrl.students" >
<td >{{key}}</td>
<td >{{value}}</td>
</tr>
I am not sure how to add filtering to make sure it outputs the student scores from highest to lowest.
Basically, you create an array of students with student name and student grade. And you loop through the students array by using ng-repeat and display individual student in descending order by grade. AngularJS orderBy example. The link is an example that using ng-repeat with orderBy.
<tr ng-repeat="student in ctrl.students | orderBy: '-grade' " >
<td>{{student.name}}</td>
<td>{{student.grade}}</td>
</tr>
Manipulate data so that it can be use for sorting:
$scope.students = [];
// someData is the data you received from database.
angular.forEach(someData, function(value, key){
$scope.students.push({
name: key,
grade: value
});
});
And after manipulation, your students array will look like this:
$scope.students = [
{name: student, grade: 100},
{name: student1, grade: 60},
{name: student2, grade: 90}
]
Nervous to ask this question.. HATE getting downvoted.. but it is what it is, I've searched and can't find the solution.
What I ended up doing is adding a loop that goes through my searchResults and reassigns the value for the column after the service returns inside the success block (PSEUDO CODE HERE, I can't copy and paste my actual code, there is an airgap):
var myNumberMap = {
1: "Number ONE!!",
2: "Number TWO!!",
3: "Number THREE!!!"
}
$scope.getSearchResults = function() {
$q.all({
resultSet : searchService.getSearchResults()
}).then(function(resultData) {
searchResults = resultData.resultSet;
for(var i = 0; i < searchResults.length; i++) {
searchResults[i].number = myNumberMap[searchResults[i].number];
}
}
}
I was really hoping there was some slick way I could just assign the data result value inside the grid config to be the value in the map?
Something like:
$scope.myCoolGridConfig = NgGridConfig.getConfig(
NgGridConfig.getDefaultConfig(), {
data: 'searchModel.searchResults.list',
columnDefs: [
field: 'number',
displayName: 'Number',
value: myNumberMap[searchModel.searchResults.list.number]
]
}
)
There are a few methods that you could take here:
Create a custom filter that you apply to your ng-repeat to transform the values based on your map.
Store your value map in your angular controller and bind the mapped value to the DOM.
// Controller
$scope.myMap = {
1 : "String One",
2 : "String Two",
3 : "String Three"
}
// something.html
<div ng-repeat='num in numList'>
{{myMap[num]}}
</div>
If I interpenetrated the question correctly your looking for something along these lines.
myMap = {
1 : "String One",
2 : "String Two",
3 : "String Three"
};
If the col number is 1 display String One instead of one in the table
Use myMap and look for the prop of col in it to pull the string value
<table>
<tr ng-repeat="col in tempCols">
<td>{{col}}</td>
<td>{{myMap[col]}}</td>
</tr>
</table>
If you need to do it towards an object that has no defining index such as the object below.
$scope.objectData = [{
name: "test1",
},
{
name: "test1",
},
{
name: "test1",
},
{
name: "test1",
},
{
name: "test1",
},
]
You can track it by $index + 1
<table>
<tr>
<td> Column Converted</td>
<td> Object name value</td>
<tr ng-repeat="col in objectData">
<td>{{myMap[$index + 1]}}</td>
<td>{{col.name}}</td>
</tr>
</table>
Heres a plunker for a better visual
I have a listing using ng-repeat with a search for two available parameters (month and state).
If I make a search, I get a ng-repeat error found duplicates.
Can not understand why if in both cases the JSON data have the same structure (just the values will change).
I have these ng-repeats: item in data and nested inside : uniqueitem in item
I've tried to use track by $index but it loops for every single character, and for item.index or item.label1 but triggers the found duplicates erros again.
Here is my loop using ng-repeat.
<tbody ng-repeat="item in data">
<tr ng-repeat="uniqueitem in item">
<td>
{{uniqueitem.label1 | number}}
</td>
<td>
{{uniqueitem.label2 | number}}
</td>
My JSON has this structure :
[
{
"index": 0,
"label1": "Initials",
"label2": "2",
"label3": "18",
"label4": "12",
"label5": 150,
"label6": "30",
"label7": 60,
"label5A": "v",
"label7A": "r"
},
{
"index": 1,
"label1": "Others",
"label2": 5485,
"label3": 27289,
"label4": 37776,
"label5": 72.23,
"label6": 91949,
"label7": 29.67,
"label5A": "r",
"label7A": "r"
},
....
]
Just works !
Inside
$http.post(ApiEndpoint.url,$scope.formData).success(function(data) {
Instead of this line :
$scope.data = JSON.stringify(data);
I add these lines :
var acp = {};
acp.resultdata = [ data ];
$scope.data = acp.resultdata;
I will replicate in a Plunkr, can not say why JSON.stringify causes this behavior.