How can i make dynamic ng-repeat using angularjs? - angularjs

Below I have added my data
[{
"name":"testapp",
"version":"2.0",
"description":"testapp",
"applicationenvironment":"angularjs"
}]
I want to make ng-repeat but I don't want to hard code any field (name, version, description, applicationenvironment)
How can I achieve this?
MY expectation :
IN TABLE it should come like this

Your array should be an object. So your structure simplifies quite a lot. Just extract key and values from your object and loop over it for each row. Then display key and values in separate columns per row:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.table = [{
"name": "testapp",
"version": "2.0",
"description": "testapp",
"applicationenvironment": "angularjs"
}]
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr ng-repeat="(key, value) in table[0]">
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
</table>
</div>
</body>
</html>

Although I don't recommend you to have this strucuture, you can do something like this:
angular.module('app', [])
.controller('appController', function () {
this.data = {
"name":"testapp",
"version":"2.0",
"description":"testapp",
"applicationenvironment":"angularjs"
};
});
And your HTML would be something like this:
<div ng-app="app" ng-controller="appController as vm">
<ul>
<li ng-repeat="(key, value) in vm.data"> {{ key }} : {{ value }}</li>
</ul>
</div>
If you still need to have the data inside an array as you've wrote in the question, you would have to iterate over both the array and the object.

Now since your data source is an array, you need to do two nested ng-repeats
let app = angular.module("table",[]);
app.controller("tableCtrl", ["$scope", function($scope){
$scope.data = [{
"name":"testapp",
"version":"2.0",
"description":"testapp",
"applicationenvironment":"angularjs"
},
{
"name":"testapp 2",
"version":"2.1",
"description":"testapp 2",
"applicationenvironment":"angularjs"
}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="table" ng-controller="tableCtrl">
<table ng-repeat="row in data">
<tr><td>Key</td><td>Value</td></tr>
<tr ng-repeat="(key,value) in row">
<td>{{key}}</td><td>{{value}}</td>
</tr>
</table>
</div>
Now if you do this, your data structure should yield the wanted result

You need to loop twice: Once through each the entire array of objects to access each object, and then inside each object to access individual key-value pairs.
I have posted the code below:
angular.module('app', [])
.controller('Controller1', function () {
this.data = {
"name":"testapp",
"version":"2.0",
"description":"testapp",
"applicationenvironment":"angularjs"
};
});
<div ng-app="app" ng-controller="Controller1 as c1">
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr ng-repeat="c in c1.data">
<table>
<tr ng-repeat="(key, value) in c">
<td> {{ key }} </td>
<td> {{ value }} </td>
</tr>
</table>
</tr>
</table>
</div>

Related

Json data bind div in angularjs

Please help. I am new at angularjs, I want to bind json data into div as list using angularjs
Sample Data
"[{"T_FORM_CODE":"T12040"},{"T_FORM_CODE":"T18025"},{"T_FORM_CODE":"Q12014"},{"T_FORM_CODE":"R12039"}]"
Json Data :
const HEROES = [
{"T_FORM_CODE":"T12040"},{"T_FORM_CODE":"T18025"},{"T_FORM_CODE":"Q12014"},{"T_FORM_CODE":"R12039"}
];
Template :
<tr *ngFor="let hero of heroes">
<td>{{hero.T_FORM_CODE}}</td>
</tr>
do like this:
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr ng-repeat="x in array">
<td>{{x.T_FORM_CODE}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.array = [{"T_FORM_CODE":"T12040"},{"T_FORM_CODE":"T18025"},{"T_FORM_CODE":"Q12014"},{"T_FORM_CODE":"R12039"}];
});
</script>
you can use ng-repeat.
you can see plunker
<div ng-repeat ="dr in data">
{{dr.column}}
</div>
Plunker

handling arrays in ng-repeat

I have a table and I need to display values in table from my JSON response. But I am unable to fetch the datas inside array.somewhere I am missing something.
JSON:
var jobs = [
{"id":1,"title":"Need comedian","company":"AMS","description":"Need comedian"},
{"id":2,"title":"Need Actor","company":"ERS","description":"Actor for Romantic Movie"}
]
HTML:
<tr ng-repeat ="item in jobs">
<td>{{item.jobs.title}}</td>
<td>{{item.jobs.description}}</td>
</tr>
You need to use $scope:
$scope.jobs = [{"id":1,"title":"Need comedian","company":"AMS","description":"Need comedian"},{"id":2,"title":"Need Actor","company":"ERS","description":"Actor for Romantic Movie"}]
<tr ng-repeat ="item in jobs">
<td>{{item.title}}</td>
<td>{{item.description}}</td>
</tr>
Hope it helps =)
There are two issues,
(i)You need to use $scope variable
(ii)You need to access item.title not item.jobs.title inside ng-repeat
You should access item
<tr ng-repeat="item in jobs">
<td>{{item.title}}</td>
<td>{{item.description}}</td>
</tr>
DEMO
The jobs array needs to be in $scope.
$scope.jobs = [{"id":1,"title":"Need comedian","company":"AMS","description":"Need comedian"},{"id":2,"title":"Need Actor","company":"ERS","description":"Actor for Romantic Movie"}]
<tr ng-repeat ="item in jobs">
<td>{{item.title}}</td>
<td>{{item.description}}</td>
</tr>
Keep these things in mind while playing with ng-repeat :
You have to use $scope.jobs object instead of var jobs as an array declaration in the controller which you are going to pass in ng-repeat for binding.
When you are going to iterate the array inside ng-repeat="item in jobs" no need to use again jobs to access the property of the objects(item) of an array(jobs).
Working Demo :
var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope) {
$scope.jobs = [
{
"id":1,
"title":"Need comedian",
"company":"AMS",
"description":"Need comedian"
},
{
"id":2,
"title":"Need Actor",
"company":"ERS",
"description":"Actor for Romantic Movie"
}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr ng-repeat ="item in jobs">
<td>{{item.title}}</td>
<td>{{item.description}}</td>
</tr>
</table>
</div>

How i can bind json data in html table using angular js?

Here is my json data. How I can bind this data in HTML table using angular.js?
[{"keycolumn1":1,"originkey1":1,"datafield1":1},
{"keycolumn1":2,"originkey1":2,"datafield1":2},
{"keycolumn1":3,"originkey1":3,"datafield1":3},
{"keycolumn1":4,"originkey1":4,"datafield1":4},
{"keycolumn1":5,"originkey1":5,"datafield1":5},
{"keycolumn1":11,"originkey1":11,"datafield1":11},
{"keycolumn1":12,"originkey1":12,"datafield1":12},
{"keycolumn1":13,"originkey1":13,"datafield1":13},
{"keycolumn1":14,"originkey1":14,"datafield1":14},
{"keycolumn1":15,"originkey1":15,"datafield1":15}]
There are many ways to display the json data in angular,
you can bind your json as
ng-repeat
<tr ng-repeat="values in data">
nested ng-repeat depending on the json format
ng-repeat with 'track by' while dealing with index values
<tr ng-repeat="item in rows">
<td>{{item.project}}({{item.task}})</td>
<td ng-repeat="values in item.hour track by $index">
<input type="number" ng-model="item.hour[$index]"/>
</td>
</tr>
ng-repeat with key value pairs
<tr ng-repeat="(key, value) in data">
<td> {{key}} </td> <td> {{ value }} </td>
</tr>
In your case, best option is to use basic ng-repeat as
<tr ng-repeat="values in data">
<td>{{values.keycolumn1}}</td>
<td>{{values.originkey1}}</td>
<td>{{values.datafield1}}</td>
</tr>
Just try like this,
var appReminder = angular.module('testApp', []);
appReminder.factory('testFactory', function ($http) {
return {}
});
appReminder.controller('testController', function PostController($scope, testFactory, $timeout)
{
$scope.result_function = function ()
{
$scope.respose = [
{"keycolumn1":1,"originkey1":1,"datafield1":1},
{"keycolumn1":2,"originkey1":2,"datafield1":2},
{"keycolumn1":3,"originkey1":3,"datafield1":3},
{"keycolumn1":4,"originkey1":4,"datafield1":4},
{"keycolumn1":5,"originkey1":5,"datafield1":5},
{"keycolumn1":11,"originkey1":11,"datafield1":11},
{"keycolumn1":12,"originkey1":12,"datafield1":12},
{"keycolumn1":13,"originkey1":13,"datafield1":13},
{"keycolumn1":14,"originkey1":14,"datafield1":14},
{"keycolumn1":15,"originkey1":15,"datafield1":15}];
;}
$scope.result_function();
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" data-ng-controller="testController">
<table border="1">
<tr>
<th>Keycolumn</th>
<th>Originkey</th>
<th>Datafield</th>
<tr>
<tr ng-repeat="item in respose">
<td>{{item.keycolumn1}}</td>
<td>{{item.originkey1}}</td>
<td>{{item.datafield1}}</td>
</tr>
</table>
</div>
Do you mean to display the json content in a html table?
$scope.json = [
{"keycolumn1":1,"originkey1":1,"datafield1":1},
{"keycolumn1":2,"originkey1":2,"datafield1":2},
{"keycolumn1":3,"originkey1":3,"datafield1":3},
{"keycolumn1":4,"originkey1":4,"datafield1":4},
{"keycolumn1":5,"originkey1":5,"datafield1":5},
{"keycolumn1":11,"originkey1":11,"datafield1":11},
{"keycolumn1":12,"originkey1":12,"datafield1":12},
{"keycolumn1":13,"originkey1":13,"datafield1":13},
{"keycolumn1":14,"originkey1":14,"datafield1":14},
{"keycolumn1":15,"originkey1":15,"datafield1":15}];
in html you can use ng-repeat
<table>
<tr ng-repeat="r in json">
<td>{{r.keycolumn1}}</td>
<td>{{r.originkey1}}</td>
<td>{{r.datafield1}}</td>
</tr>
</table>
Store this in a json file (data.json). Use $http to get this data as a response and store it in a $scope variable.
For Example:
$http.get("data.json").then(function(response) {
$scope.data = response.data;
});
you need to assign your json to a scope variable like below
$scope.data="your data";
now using this data you can loop in table by using ng-repeat
here is a sample plunker with your data
Simple using ng-repeat by having your json Data in your controller
<table>
<tr ng-repeat="r in jsonData">
<td>{{r.keycolumn1}}</td>
<td>{{r.originkey1}}</td>
<td>{{r.datafield1}}</td>
</tr>
</table>
Also you can have it in your Json file like this
{
"data":[
{
"keycolumn1":1,
"originkey1":1,
"datafield1":1
},
{
"keycolumn1":2,
"originkey1":2,
"datafield1":2
},
{
"keycolumn1":3,
"originkey1":3,
"datafield1":3
},
{
"keycolumn1":4,
"originkey1":4,
"datafield1":4
},
{
"keycolumn1":5,
"originkey1":5,
"datafield1":5
},
{
"keycolumn1":11,
"originkey1":11,
"datafield1":11
},
{
"keycolumn1":12,
"originkey1":12,
"datafield1":12
},
{
"keycolumn1":13,
"originkey1":13,
"datafield1":13
},
{
"keycolumn1":14,
"originkey1":14,
"datafield1":14
},
{
"keycolumn1":15,
"originkey1":15,
"datafield1":15
}
]
}
and use it in your controller like this
$http.get('jsonData.json').success(function(data) {
$scope.jsonFileData = data.data;
});
and I have made a working LIVE PLUNK which contains both examples
First you need to associate controller with view then you can access variables of controller in view.
<div ng-controller="controllername as vm">
<table>
<tr ng-repeat="anyvariable in vm.json">
<td>{{anyvariable.keycolumn1}}</td>
<td>{{anyvariable.originkey1}}</td>
<td>{{anyvariable.datafield1}}</td>
</tr>
</table>
</div>

AngularFire searching key node and assign to variable

I 'm working on my AngularFire project. I've a database something like this https://dinosaur-facts.firebaseio.com/. What I'm doing is first searching the key like the dinosaurs child such as "linhenykus" and storing its keys and values to other $scope variable.
my code in controller
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs" + "/" + $scope.dinoID); //$scope.dinoID is variable containg dino keys
$scope.detRef = $firebaseArray(ref);
I'm getting the output like
[{"$value":-85000000,"$id":"appeared","$priority":null},{"$value":0.6,"$id":"height","$priority":null},{"$value":1,"$id":"length","$priority":null},{"$value":"theropoda","$id":"order","$priority":null},{"$value":-75000000,"$id":"vanished","$priority":null},{"$value":3,"$id":"weight","$priority":null}]
How to fetch the keys and values?
Use ngRepeat directive, as below:
<tr ng-repeat="obj in array track by $index">
To filter the array you can use the filter of Angular:
<tr ng-repeat="obj in array | filter: criteria track by $index">
Here's an example:
(function() {
angular
.module('app', [])
.controller('MainCtrl', MainCtrl);
MainCtrl.$inject = ['$scope', '$filter'];
function MainCtrl($scope, $filter) {
$scope.criteria = {};
$scope.array = [
{
"$value":-85000000,
"$id":"appeared",
"$priority":null
},
{
"$value":0.6,
"$id":"height",
"$priority":null
},
{
"$value":1,
"$id":"length",
"$priority":null
},
{
"$value":"theropoda",
"$id":"order",
"$priority":null
},
{
"$value":-75000000,
"$id":"vanished",
"$priority":null
},
{
"$value":3,
"$id":"weight",
"$priority":null
}
];
$scope.getObj = function(value, id) {
$scope.obj = $filter('filter')($scope.array, {
"$value": value,
"$id": id
})[0];
}
$scope.getObj("theropoda", "order");
}
})();
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">
<input type="text" placeholder="Search by value" ng-model="criteria.$value">
<input type="text" placeholder="Search by id" ng-model="criteria.$id">
<!-- Note that this button is just as an example to change the object displayed below -->
<button type="button" ng-click="getObj()">Change the value of object</button>
<hr>
Obj: <span ng-bind-template="Value: {{obj.$value}}, Id: {{obj.$id}}"></span>
<p></p>
<table width="100%">
<thead>
<tr>
<th>Value</th>
<th>Id</th>
<th>Priority</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="obj in array | filter: criteria track by $index">
<td ng-bind="obj.$value"></td>
<td ng-bind="obj.$id"></td>
<td ng-bind="obj.$priority"></td>
</tr>
</tbody>
</table>
</body>
</html>

How print array of object in ng-repeat with out using key.value in angular.js dynamically?

I am using angular.js for font-end and node.js for server side.
Now, I am having some list of values in array randomly.
Html code :
<html ng-app='app' ng-controller='mainController'>
<head>
</head>
<body>
<div class="container">
<div class="jumbotron">
<table><tr ng-repeat="report in reports">
<td style="padding-left:10px;">{{report.salary_head_value11}}</td>
<td style="padding-left:10px;">{{report.salary_head_value12}}</td>
<td style="padding-left:10px;">{{report.salary_head_value13}}</td>
<td style="padding-left:10px;">{{report.salary_head_value14}}</td>
<td style="padding-left:10px;">{{report.salary_head_value15}}</td>
</tr>
</table>
</div>
</div>
<pre>{{ cleanData | json}}</pre>
</body>
</html>
Controller code :
angular.module('app', [])
.controller('mainController', ['$scope', '$filter', function($scope, $filter) {
$scope.reports = [{
"salary_head_value11":100,
"salary_head_value12":300,
"salary_head_value13":200,
"salary_head_value14":800,
"salary_head_value15":500
},{
"salary_head_value11":200,
"salary_head_value12":400,
"salary_head_value13":900,
"salary_head_value14":800,
"salary_head_value15":600
}];
}]);
Output :
Expected code :
i want to print the same output without using {{report.salary_head_value11}} ......{{report.salary_head_value15}} in ng-repeat
If you dont need the string name salary_head_value, you can turn the array of objects into a 2d array:
$scope.reports = [
[ 100, 300, 200, 800, 500 ],
[ 200, 400, 900, 800, 600 ]
];
Then in HTML:
<table>
<tr ng-repeat="report in reports">
<td style="padding-left:10px;" ng-repeat="val in report">{{val}}</td>
</tr>
</table>
Note: untested, this is the concept, please modify it if there are syntax errors

Resources