1

I have an Array which has Role and Screen variables.

Role variable can be repeated in this array.

I want to create rowspan for repeated data.

How can I do this easily?

My pseudocode is in below

Angular

$scope.testData=[
    {"Role":"Admin","Screen":"A"},
    {"Role":"Admin","Screen":"B"},
    {"Role":"Admin","Screen":"C"},
    {"Role":"Standard","Screen":"H"},
    {"Role":"Standard","Screen":"FH"},
];

HTML

 <tr ng-repeat="detail in Roles">
        <td>{{detail.Role}}</td>
        <td>{{detail.Screen}}</td>
 </tr>

I need the output in below

<table>
   <tr>
        <td rowspan="3">Admin</td>
        <td>AAA</td>
    </tr>
    <tr>
        <td>BBB</td>
    </tr>
    <tr>
        <td>CCC</td>
    </tr>
    <tr>
        <td rowspan="2">Standard</td>
        <td>DDD</td>
    </tr>
    <tr>
        <td>EEE</td>
    </tr>
</table>
3
  • how do you need the output? Commented Jun 27, 2017 at 6:05
  • can you post the hardcoded html of how you want the table output to look (i.e. without angular)? Commented Jun 27, 2017 at 6:19
  • Thanks for answers guys. @SlavaUtesinov your answer is partially correct but my data structure like testData and I want to create my output using my Array with AngularJS Commented Jun 27, 2017 at 6:38

1 Answer 1

2

Change your object to look like (Using angular.forEach() for example)

$scope.testData = [
    {"Role":"Admin","Screen":["A", "B", "C"]},
    {"Role":"Standard","Screen": ["H", "FH"]}
];

Then it's too easy for your rowspan table:

<table>
    <tr ng-repeat-start="(key, val) in testData">
        <td rowspan="{{val.Screen.length}}">{{val.Role}}</td>
        <td>{{val.Screen[0]}}</td>
    </tr>
    <tr ng-repeat-end ng-repeat="value in val.Screen.slice(1)">
        <td>{{value}}</td>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Cool.. Thank you so much

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.