0

I am trying to make auto generating table from json with angularJS , but how do i access name of value its self , for example this is json i get

    {"data":{"selectView":[{"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"},
{"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"}]}}

I have sent that trough main.js to my controller , in which i have done this $scope.employee = employee.data.selectView;

And i am using ng-repeat="employee in employee"

It is object json , what am i doing wrong ?

How can i return value "employeeCode" or "employeeFirstName" , i have tryed (key,value) in employee on ng-repeat , but it returns whole row ({"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"})

Any ideas ?

EDIT UPDATE

Solution was <th ng-repeat="(key, val) in employee[0]">{{key}}</th>

2
  • If one of the answers below matches what you did accept the answer, otherwise post your own answer and accept it. Commented Feb 7, 2016 at 11:48
  • @Malkus Forgot about it , sorry :) thanks for reminding Commented Feb 7, 2016 at 12:00

4 Answers 4

2

Solution was <th ng-repeat="(key, val) in employee[0]">{{key}}</th>

for td's i used

     <tr ng-repeat="dataItem in data" ng-click="newLocation(data[$index])">

       <td ng-repeat="(key, val) in dataItem">{{val}}</td>
   </tr>
Sign up to request clarification or add additional context in comments.

Comments

1

Create a simple Html table and apply ng-repeat on the

<table>
    <tr ng-repeat="employee in employees">
      <td>{{employee.employeeCode}}</td>
      <td>{{employee.employeeFirstName}}</td>
      <td>{{employee.employeeLastName}}</td>
    </tr>
  </table>

in the app.js assign the json array to $scope.employees

Comments

0

In Your Controller/Module File (.Js File)

$scope.data = [
{
employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"
},
{
"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"
}];

In Your HTML File

<ul ng-repeat = "values in data">
<li>{{values.employeeCode}}</li>
<li>{{values.employeeFirstName}}</li>
<li>{{values.employeeLastName}}</li>
</ul>

Cheers :)

1 Comment

I know the basics :P thing is i want it to create its self depending on json , so it will create <th> automaticly and <td>'s , so i need to access "employeeCode" to put it to head of table
0

Since the JSON you are getting is an array and not an object, so you should not use (key, value). You should use ng-repeat directly on your array. In your controller, assign it to a variable like this:

$scope.myjson = {"data":{"selectView": [{"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"},
{"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"}]   }}

then in your html,

<div ng-controller="MyCtrl">
 <table>
  <tr ng-repeat="emp in myjson.data.selectView">
    <td>{{emp.employeeCode}}</td>
    <td>{{emp.employeeDirstName}}</td>
    <td>{{emp.employeeLastName}}</td>
  </tr>
</table>

1 Comment

How can i return value "employeeCode" or "employeeFirstName" , not value of employeeCode , but value that would say : "employeeCode" , do you understand me now ? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.