It looks like you can do what you want by converting each row of your
JSON data to a Hash. That way you can iterate over all your values
without having to know the keys. This is nice because if you add more
data elements the script will simply add another table column for
you. Here's what I've got:
onSuccess: function(json) {
var list = json.d;
var data;
var table = new Element('table', {'width':'100%'});
list.each(function(item) {
var tr = new Element('tr');
// convert item to a Hash and iterate over the values
$H(item).getValues().each(function(value){
// no need to store the new element in a variable here
(slight performance gain)
new Element('td', { 'text': value }).inject(tr);
});
tr.inject(table);
}.bind(this));
table.inject($(this.options.container));
}.bind(this);
On Dec 30, 3:25 am, websam <[email protected]> wrote:
> The first thing i did was :
>
> onSuccess: function(json) {
> var list = json.d;
> var table = new Element('table', {'width':'100%'});
> list.each(function(item) {
> var tr = new Element('tr');
> var td = new Element('td', { 'text': list.Id }).inject(tr);
> var td = new Element('td', { 'text': list.RoleName }).inject
> (tr);
> var td = new Element('td', { 'text': list.AddDate }).inject
> (tr);
> var td = new Element('td', { 'text': list.Active }).inject
> (tr);
> tr.inject(table);
> }.bind(this))
> table.inject($(this.options.container));
>
> }.bind(this)
>
> And this works fine, but I need to be able to do it without having to
> write the Id, RoleName, AddDate and Active ?
>
> On 30 Dec., 09:23, Sanford Whiteman <[email protected]>
> wrote:
>
> > > I'm making a class to create a table and fill that table with data
> > > from JSON. But I can't find a way to loop throug my JSON object
> > > without hardcoding the key/value pair.
>
> > each().
>
> > Or getKeys() if you want to get a list of the keys and get() by key
> > after that.
>
> > --Sandy