Display angular table

22 views
Skip to first unread message

Marco Canada

unread,
Nov 11, 2015, 4:07:27 PM11/11/15
to AngularJS
Hello, 

I am fairly new to angular, and I ran into a problem.

I have the following result from a query in an object:

"data" : [{"item":"item1", "date":"2015*01*01", "qty":"8"} .... and so on! In table it would look like this: 

item         date         qty
==============================
item1      
2015-01-01      8
item1      
2015-01-02      5
item1      
2015-01-03      6
item1      
2015-01-04      1
item2      
2015-01-01      1
item2      
2015-01-02      2
item2      
2015-01-03      3
item2      
2015-01-04      4
item3      
2015-01-01      10
item3      
2015-01-02      11
item3      
2015-01-03      12
item3      
2015-01-04      13

and in my view (html), I want to show this data like this

item            2015-01-01    2015-01-02   2015-01-03  2015-01-04
==================================================================
item1                
8             5              6         1
item2                
1             2              3         4
item3                
10            11             12        13
 

How can I use ngRepeat to get the result I want? Or any other directive as a matter of fact. I have tried hundrends of things but I can't seem to find a good solution.

Thanks.

Sander Elias

unread,
Nov 11, 2015, 9:44:02 PM11/11/15
to AngularJS
Hi Marco,

Not much of an angular problem. More of a data-manipulation one. lets see
es6 like:

let tmp  = data.reduce((table,elem) => toTable(table,elem),{});
let table
= Object.keys(tmp).map(i => tmp[i]);

function toTable(table, elem) {
   table
[elem.item] = (table[elem.item]||0) + item.qty;
   
return table;
}



Typed up here in the browser, so there might be a syntax error in somewhere.
Now use a ngRepeat over the resulting table var, and you should be done.

If you have any more questions, do not hesitate to ask.

Regards
Sander

Marco Canada

unread,
Nov 12, 2015, 1:51:55 PM11/12/15
to ang...@googlegroups.com
Thank you Sander. I ended up doing something similar to this before passsing it to my page.

Marco Cañada

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/R7o9Glv0XgM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages