Below is my JSON object(partial) which I would like to show as a matrix table preferably using ng-repeat:
[
{
"product": "milk",
"resource": "a",
"checked": true
},
{
"product": "bread",
"resource": "a",
"checked": false
},
{
"product": "butter",
"resource": "a",
"checked": true
}
]
I have tried http://plnkr.co/edit/iW1dZV?p=info but I don't want to use coffeescript.
Although this is not the exact solution for your problem, I hope this will give you idea of what needs to be done. I am printing the array based on sequence but you can change the condition easily.
Convert an array into a matrix/ grid
I have an array which i wanted to convert into a grid/matrix of column size 4. the following implementation worked for me. You can use the two counters : row and col as you like in side the nested ng-repeat
In my case number of columns is 3. But you can replace that 3 with a variable everywhere.
h.seats
is my array of the objects and i want to print either X or - based on value of element in that array<th ng-repeat="n in [].constructor(3 + 1) track by $index">{{$index}}</th>
prints the header row with column number at the top.getNumber(h.seats.length, 3)
returns me the number of rows of that table as followsThe line
ng-if="col >= (row)*3 && col < (row+1)*3"
is important logic to calculate which elements should be put in that row. The output looks like belowRefer to following link for details of how row and col counters are used: https://stackoverflow.com/a/35566132/5076414
No ng, but plain javascript.
Basically you need a function which returns the checked property of the array with the given properties. This function iterates over the elements until the property values are matched.
The second part is to generate the table, which should be easier in ng, as I think.
@forgottofly here is an example of better json for what you need: