I'm new to Angular and would like to learn the best way to handle a problem. I want to access keys in my first ng-repeat grouped by multiple columns.
<table width="100%" style="text-align: center;" data-role="table" data-mode="" class="ui-responsive ui-shadow">
<thead>
<tr>
<th>Id</th>
<th>Course</th>
<th>Module</th>
<th>Subject</th>
<th>Toc</th>
<th>Chapter</th>
<th>Activity Done</th>
<th>Start</th>
<th>End</th>
<th>Time Spent</th>
</tr>
</thead>
<tbody ng-repeat="(key, value) in sessions | groupBy: ['course','module','subject','toc','chapter']" id="tblSessionRpt">
<tr ng-init="$index == 0 ? isOpen=true : isOpen=false">
<td>
<span ng-click="isOpen=!isOpen" style="cursor:pointer">
<span ng-show="!isOpen">
+
<span> {{ $index+1 }} </span>
</span>
<span ng-show="isOpen">
-
<span> {{ $index+1 }} </span>
</span>
</span>
</td>
<td> {{ key.course }} </td>
<td> {{ key.module }} </td>
<td> {{ key.subject }} </td>
<td> {{ key.toc }} </td>
<td> {{ key.chapter }} </td>
<td colspan='4' align="right"> </td>
</tr>
<tr ng-repeat="session in value" ng-show="isOpen">
<td colspan='6'> </td>
<td> {{ session.done }}</td>
<td> {{ session.start }}</td>
<td> {{ session.end }}</td>
<td> {{ session.timespent }}</td>
</tr>
</tbody>
</table>
My JSON data is like :
$scope.sessions=[
{
"id": 3518,
"name": "content",
"done": "A Happy Song",
"start": "2015-11-23 11:11",
"end": "2015-11-23 11:11",
"timespent": "0",
"course": "K-12",
"module": "Std - 1",
"subject": "English",
"toc": "Unit - 01",
"chapter": "A Happy Song",
"content": null
},
{
"id": 3520,
"name": "content",
"done": "A Happy Song",
"start": "2015-11-23 11:11",
"end": "2015-11-23 11:12",
"timespent": "0",
"course": "K-12",
"module": "Std - 1",
"subject": "Hindi",
"toc": "Unit - 01",
"chapter": "A Happy Song",
"content": null
},
{
"id": 3522,
"name": "content",
"done": "A Happy Song",
"start": "2015-11-23 11:13",
"end": "2015-11-23 11:13",
"timespent": "0",
"course": "K-12",
"module": "Std - 1",
"subject": "English",
"toc": "Unit - 01",
"chapter": "A Happy Song",
"content": null
},
{
"id": 3524,
"name": "content",
"done": "A Happy Song",
"start": "2015-11-23 11:17",
"end": "2015-11-23 11:17",
"timespent": "0",
"course": "K-12",
"module": "Std - 1",
"subject": "Marathi",
"toc": "Unit - 02",
"chapter": "A Happy Song",
"content": null
},
{
"id": 3537,
"name": "content",
"done": "A Happy Song",
"start": "2015-11-23 11:47",
"end": "2015-11-23 11:47",
"timespent": "0",
"course": "K-12",
"module": "Std - 1",
"subject": "English",
"toc": "Unit - 03",
"chapter": "A Happy Song",
"content": null
}];
I want to data group by multiple columns i.e. 'course','module','subject','toc','chapter'
Thanks in advance :)
the most efficient way of sorting I usually do is to put your columns first in a array with it's designated keys in your data
then loop it in your template and add ng-click
then create the
onHeadingClicked()
function that sorts your data.I don't use filters due to performance reasons, filter's run every time and thus executes that function so many many times so it would not a good idea putting it there. I use filters only if simple display manipulation is done.
Hope this helps