JavaScript Iteration Through RecordSet Object?

1,579 views
Skip to first unread message

Chris Rider

unread,
Nov 25, 2013, 3:37:53 PM11/25/13
to yui-s...@googlegroups.com
For the life of me, I cannot figure out how to manually loop through a recordset.
I don't want to display a datatable, but instead am creating a window with a list of custom icons for a user interface.

All I want to do is manually go through a recordset object, row-by-row, and generate HTML based on each column in it, as needed... using a simple JavaScript loop.
(the client recordset object is generated from a server-side database using C - doesn't matter how for this particular help post)

Example of how I create my recordset:
var objYuiRs;    /* my global that I'll use 'outside' of YUI -- I think this is the best way??? maybe not? */
YUI
().use('recordset', function (Y) {
 
var recordsFromDB = [     /* these get populated to the client here, by a server-side loop -- how exactly is not important for this example :) */
   
{ recno:0, position:1, label:"Emergency!", desc:"Click to notify of an urgent emergency.", icon:"msgEmergency.png" }
   
{ recno:1, position:2, label:"Request staff meeting", desc:"Click to request a normal staff meeting.", icon:"msgDefault.png" }
   
{ recno:2, position:3, label:"Request technical help", desc:"Click to ask for technical help.", icon:"msgHelpTech.png" }
   
],
  temprs
= new Y.Recordset({records: favsFromDB});
  objYuiRs
= temprs;    /* make the recordset created above available in the global javascript */
});

I know I have my recordset object working (globally, even), because I can then hop into the Chrome console and type "objYuiRs._______" and it pops up the standard YUI recordset method choices and such. Furthermore, when I run (from the console) "objYuiRs.getValuesByKey()" it returns all the correct values.

But how do I take this beyond the debugger console and programatically iterate through each record in JavaScript?
I feel like the answer is right under my nose and I'm missing something obvious? Help! LOL

Juan Ignacio Dopazo

unread,
Nov 25, 2013, 3:50:53 PM11/25/13
to Chris Rider, yui-s...@googlegroups.com
How about recordset.each(fn)?

Juan


--
You received this message because you are subscribed to the Google Groups "yui-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yui-support...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Chris Rider

unread,
Nov 25, 2013, 4:17:05 PM11/25/13
to yui-s...@googlegroups.com, Chris Rider
Well, that's interesting now that I look at it; but I'm not sure it'll do what I want, in the way that I want to do it.
But let me make sure I understand...

Each time I call the .each(fn) method, YUI will go through every record and fire the callback on each record?

What if I want more discrete control of the record-iteration?

For example (using my original example above), say I want to do some tests... like if the value of my 'position' field is less than 3, then output some HTML/CSS that says it has red text -- otherwise do black.

I could EASILY be wrong, but I don't think .each will work for me.

Is there not some way to do something like this?:
for(i=0; i<objYuiRs.getLength(); i++)
 
{
 
/* do stuff */
  documen
t.write("This record's recno: "+ objYuiRs.[somemethod](i).[nameofmyfield]);  /*something like that*/
 
}


John Lindal

unread,
Nov 25, 2013, 4:27:19 PM11/25/13
to Chris Rider, yui-s...@googlegroups.com
each() works as as replacement for the basic for loop.  You can write code like this:

objYuiRs.each(function(record, index)
{
  /* do stuff */
  document.write("This record's recno: + record[nameofmyfield]);  /*something like that*/
});

Juan Ignacio Dopazo

unread,
Nov 25, 2013, 4:41:49 PM11/25/13
to John Lindal, Chris Rider, yui-s...@googlegroups.com
And, since a RecordSet is an ArrayList, you can also use a regular for loop like this:

var foo;
for (var i = 0; i < recordset.size(); i++) {
  foo = recordset.item(i).getValue('foo');
  // ...
}

Juan
Reply all
Reply to author
Forward
0 new messages