5

I have this code in angular template which just makes the header on left and data on right from top to bottom.

<div class="panel-body">

        <table class="table table-striped">
            <tr ng-repeat="f in fields">
                <th>{{ f }}</th>
                <td>{{ data[f] }}</td>
            </tr>

        </table>

    </div>

But instead of one field in one row i want to have 2 fields in one row and 3rd field and 4th field in second row and so on.

so that i have 2 columns layout

 <tr><th>{{ f }}</th>
 <td>{{ data[f] }}</td> 
 <th>{{ f }}</th>
 <td>{{ data[f] }}</td>
</tr>


field = ['id', 'name', 'username', 'email', 'age']

data = [{id:1, name: 'john', username: 'john', age: 20, email: 'test'}]

The result i want is

<tr><td>id:</td><td>1</td><td>name:</td><td>john</td></tr>
<tr><td>username:</td><td>john</td><td>age:</td><td>20</td></tr>

This should be done with ng-repeat rather hard coding stuff

6
  • Can you share your fields object ? Commented Jun 20, 2015 at 10:35
  • @Vineet field is a list ['id', 'name', 'age', 'email'] Commented Jun 20, 2015 at 10:43
  • stackoverflow.com/questions/18564888/… Commented Jun 20, 2015 at 10:50
  • @user3214546 cna you please explain it with an example ? or show us something that you are expectng :) Commented Jun 20, 2015 at 10:53
  • why use <table> for this? Commented Jun 20, 2015 at 11:35

2 Answers 2

2

Here's an answer. I'm not sure it's exactly what you want but I think it comes close: http://plnkr.co/edit/ADKu2WEb9TyvEXASOJyz?p=preview

Note that I'm using ng-repeat-start/ng-repeat-end to handle the multi-line thing you want to do:

<body ng-app="example" ng-controller="ExampleController">
  <table>
    <tr ng-repeat-start="row in data">
      <td>{{ label(0) }}:</td>
      <td>{{ value(row, 0) }}</td>
      <td>{{ label(1) }}:</td>
      <td>{{ value(row, 1) }}</td>
    </tr>
    <tr ng-repeat-end>
      <td>{{ label(2) }}:</td>
      <td>{{ value(row, 2) }}</td>
      <td>{{ label(3) }}:</td>
      <td>{{ value(row, 3) }}</td>
    </tr>
  </table>
</body>

The rest is just a super simple module and controller:

angular.module('example', [])
  .controller('ExampleController', function ($scope) {
    var fields = [ 'id', 'name', 'username', 'email', 'age' ];
    $scope.data = [{id:1, name: 'john', username: 'john', age: 20, email: 'test'}];

    $scope.label = function (fieldNumber) {
      return fields[fieldNumber];
    };

    $scope.value = function (row, fieldNumber) {
      return row[fields[fieldNumber]];
    }
  });
Sign up to request clarification or add additional context in comments.

2 Comments

Its not working for me . will this work for any number of fields or you have hard coded 1,2,3,4 fields only
@user3214546 Yes, it's hard coded to four fields, you didn't say anything about varying numbers of fields. You could have a two ng-repeats inside of the other one. One going over the first half of the fields [ 0 to (n / 2) - 1] and the second over fields [ (n / 2) to (n - 1) ]. However, you'd better always have an even number of fields or your table formatting will be wonky (three fields on one row and only two on the next). Also, the view is starting to get a little complicated with logic and you might want to consider pulling some of that out into directives to simplify it.
1

Hi I tried but you have to change your response into below format

$scope.data = {
    data1: {
        id: 1,
        name: 'john',
    },
    data2: {
        username: 'john',
        age: 20
    }
};

and you don't need field variable

Below is how to have to repeat in you html although I haven't given you on two columns

 <table class="table table-striped">
            <tr ng-repeat="x in data">
                <td ng-repeat="(key,val) in x">{{key}} : {{val}}</td>
            </tr>

        </table>

7 Comments

@Anyone who gave him negative don't discourage a person who just stated stackoverflow. Yeah I know his answer is not right but atleast he tried. :)
@squiroid I may understood the question wrong but my answer is working Thanx for support
@siddharthPandey I think he wanted one more field in a row. So somebody down voted this.
if you want to use key value both to be printed use below ng-repeat="(key,val) in data"
@ManojKumar Yeah I have checked now and edited the post, I think my new answer will help him please vote up i m not able to post answers anymore on stackover flow
|

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.