Hi again,
Sorry, I read over your email again and saw one more thing - what
would probably be the least modification to the code you have right
now:
var gotData = function(rData) {
row_display = function (row) {
return TR(null, map(partial(TD, null), row));
}
var newTable = TABLE({'id': 'myDataTable'},
THEAD(null,
row_display(keys(rData[0]))),
TBODY(null,
imap(row_display, imap(values, rData))));
appendChildNodes('myDisplay', newTable);
}
Here I used imap to avoid building both the data array and the array
of TRs in memory beforehand. I haven't tested it, but it might be a
bit more efficient especially if the table is very big.
Arnar
On 1/27/07, Arnar Birgisson <[EMAIL PROTECTED]> wrote:
> On 1/26/07, Johnny Blonde <[EMAIL PROTECTED]> wrote:
> >
> > Hello Arnar,
> >
> > maybe i need to explain it more detailed.
> >
> >
> > var rData= [
> > {
> > "VORGANG":"XX1234",
> > "NR":"572",
> > "NAME1":"Frank",
> > "NAME2":"S",
> > "REISENR":"12345"
> > },
> > {
> > "VORGANG":"XX12344",
> > "NR":"1111",
> > "NAME1":"Frank",
> > "NAME2":"L",
> > "REISENR":"12345"
> > },
> > {
> > "VORGANG":"XX12345",
> > "NR":"1264",
> > "NAME1":"Frank",
> > "NAME2":"L",
> > "REISENR":"12345"
> > }
> > ] ;
> >
> >
> > and i need it to be transformed to a html-table like
> >
> > vorgang nr name1 name2 reisenr
> > XX1234 572 Frank S 12345
> > XX12344 1111 Frank L 12345
> > XX12345 1264 Frank L 12345
> >
> >
> > what i do right now is
> >
> > var gotData = function(rData) {
> > row_display = function (row) {
> > return TR(null, map(partial(TD, null), row));
> > }
> > var dataArr = new Array();
> > forEach(rData, function(rRow) {
> > dataArr.push(values(rRow));
> > });
> > var newTable = TABLE({'id': 'myDataTable'},
> > THEAD(null,
> > row_display(keys(rData[0]))),
> > TBODY(null,
> > map(row_display, dataArr)));
> > appendChildNodes('myDisplay', newTable);
> > }
> >
> > d = MochiKit.Async.loadJSONDoc("mydemodata.json");
> > d.addCallback(gotData);
> >
> >
> >
> > My main question is: isn“t there a more elegant, smooth and flexible
> > way to transform an object-array to an array than
> >
> > var dataArr = new Array();
> > forEach(rData, function(rRow) {
> > dataArr.push(values(rRow));
> > });
>
> Yes there is: var dataArr = map(values, rData);
>
> > or even better: can someone give me a hint on how to do the things in
> > the example on an object instead of first converting it to this array?
>
> Oh, ok - I understand.
>
> Well, I would probably do something like this:
>
> var gotData = function(rData) {
> var columns = ['VORGANG', 'NR', 'NAME1', 'NAME2', 'REISENR'];
> // or, if you'd like to do it dynamically:
> // var columns = keys(rData[0]);
> var mkTD = partial(TD, null);
> var newTable = TABLE({'id': 'myDataTable'},
> THEAD(null, TR(null, map(mkTD, columns))),
> TBODY(null, map(function (row) {
> return TR(null, map(function (key) {
> return mkTD(row[key]);
> }, row));
> }, rData))
> );
> appendChildNodes('myDisplay', newTable);
> }
>
> d = MochiKit.Async.loadJSONDoc("mydemodata.json");
> d.addCallback(gotData);
>
>
> Don't know if it's more elegant or not though :o)
>
> Even simpler if you feel comfortable relying on the correct order of
> things in your input and the browser's semantics of "for a in obj":
>
> var gotData = function(rData) {
> var mkTD = partial(TD, null);
> var newTable = TABLE({'id': 'myDataTable'},
> THEAD(null, TR(null, map(mkTD, keys(rData[0])))),
> TBODY(null, map(function (row) {
> return TR(null, map(mkTD, values(row));
> }, rData))
> );
> appendChildNodes('myDisplay', newTable);
> }
>
> Those weirdos (me included) that find some beauty in functional
> programming would enjoy this:
>
> var gotData = function(rData) {
> /* look ma, no variables or function definitions! */
> appendChildNodes('myDisplay', TABLE({'id': 'myDataTable'},
> THEAD(null, TR(null, map(partial(TD, null), keys(rData[0])))),
> TBODY(null,
> map(partial(TR, null),
> map(partial(map, partial(TD, null)),
> imap(values, rData)
> )
> )
> )
> );
> }
>
> :o)
>
> Arnar
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---