1

So I want to create a table something similar to this

enter image description here

with this data

$scope.data = [
 {question : 1, answer : a},
 {question : 1, answer : b},
 {question : 1, answer : c},
 {question : 2, answer : b},
 {question : 2, answer : a},
 {question : 3, answer : c},
]

So far this is what i have

<tbody ng-repeat="(key,value) in data" ng-init="current=null">
 <tr>
 <td><span ng-if="current != value.question">{{value.question}}</span></td>
 <td>{{value.answer}}</td>
 <td class="hidden">{{ current = value.question }}</td>
 </tr>
</tbody>

would be glad if you could help and enlighten me thank you!

Anik Islam Abhi
25.4k8 gold badges61 silver badges82 bronze badges
asked Dec 17, 2015 at 9:32

2 Answers 2

1

modify your $scope.data first:: provide "" for key. Here is the DEMO

$scope.data = [
 {"question" : 1, "answer" : "a"},
 {"question" : 1, "answer" : "b"},
 {"question" : 1, "answer" : "c"},
 {"question" : 2, "answer" : "b"},
 {"question" : 2, "answer" : "a"},
 {"question" : 3, "answer" : "c"}
]

View: instead of use ng-if use ng-show for maintaining the structure of table.

<table>
 <tbody ng-repeat="(key,value) in data">
 <tr>
 <td ng-show="data[key-1].question != value.question">{{value.question}}</td>
 <td>{{value.answer}}</td>
 <td class="hidden">{{ current = value.question }}</td>
 </tr>
 </tbody>
</table>
answered Dec 17, 2015 at 10:51
Sign up to request clarification or add additional context in comments.

Comments

1

What you have there in data is an array of objects, and you are using the syntax for looping over objects and not arrays in your ng-repeat.

Just change like so:

<table>
 <thead>
 <th>Question#</th>
 <th>Answer#</th>
 </thead>
 <tbody ng-repeat="value in data" >
 <tr>
 <td><span>{{value.question}}</span></td>
 <td>{{value.answer}}</td>
 </tr>
 </tbody>
</table>

Here is a fiddle to get you on your way.

answered Dec 17, 2015 at 9:34

1 Comment

will this generate the table structure like i posted above? thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.