3

I generated table data and table columns dynamically in angularjs. My table is

Name    Hobby
XXXX    Music
XXXX    Cricket
XXXX    Books
YYYY    Tennis
YYYY    Books
YYYY    Drawing

But I want my table to be displayed like this :

Name    Hobby

XXXX
        Music
        Cricket
        Books
YYYY
        Tennis
        Books
        Drawing

I used the following code to generate table :

        <tr>
            <th ng-repeat="th in keys">{{th}}</th>
        </tr>
        <tr ng-repeat="x in data ">
            <td ng-repeat="th in keys">
                {{ x[th]}}
            </td>
         </tr>

My json looks like this

[{"Name:"XXXX", "Hobby":"Music"},
{"Name:"XXXX", "Hobby":"Cricket"},
{"Name:"XXXX", "Hobby":"Books"},
{"Name:"YYYY", "Hobby":"Tennis"},
{"Name:"YYYY", "Hobby":"Books"},
{"Name:"YYYY", "Hobby":"Drawing"}]

I can't use similar to this

<ng-repeat="(key, value) in players | groupBy: 'team'">

because my table headers are created dynamically

How I can do this in angularjs?

3
  • we need to see your JSON data Commented May 28, 2015 at 12:07
  • 1
    Possible duplicate of stackoverflow.com/questions/23493063/… Commented May 28, 2015 at 12:09
  • 1
    Manipulate Your data accordingly as you want to show it .. Commented May 28, 2015 at 12:12

3 Answers 3

3

here is simple javascript solution:

var array = [{"Name":"XXXX", "Hobby":"Music"},
{"Name":"XXXX", "Hobby":"Cricket"},
{"Name":"XXXX", "Hobby":"Books"},
{"Name":"YYYY", "Hobby":"Tennis"},
{"Name":"YYYY", "Hobby":"Books"},
{"Name":"YYYY", "Hobby":"Drawing"}];

 var distinctNames = []

 for (i = 0; i < array.length; i++) { 
   if(distinctNames.indexOf(array[i].Name) === -1){
      distinctNames.push(array[i].Name);
   }
   else{
       array[i].Name = "";
   }
 }

DEMO

After this filter you can render array in your table

Sign up to request clarification or add additional context in comments.

Comments

1

You could do this thing on markup itself, no need of sorting or filtering an array.

Markup

  <table>
    <thead>
      <tr>
        <th ng-repeat="th in keys">{{th}}</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="x in data">
        <td ng-repeat="th in keys">
          <span ng-show="th != 'Name' || (th == 'Name' && data[$parent.$index - 1]['Name'] != x['Name'])">
          {{ x[th]}}
          </span>
        </td>
      </tr>
    </tbody>
  </table>

Demo

Comments

0

I would use ng-table You will be able to easily sort, filter and group your table.

Comments

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.