Re: How to convert a table to JSON (or to Javascript array)

Thomas Fuchs
2007-06-16T16:40:15Z

As a side note, you probably want to use immediateDescendants()
instead of descendants(). The latter will add all elements contained
in the TDs, too, so you''re probably better of with the former.

Best,
Thomas

Am 16.06.2007 um 18:34 schrieb Thomas Fuchs:

>
> For cases like this one, it''s better to use the more specialized
> methods Enumerable provides.
>
> You can write this more concise by using "map" instead of the
vanilla
> "each":
>
> function tableToJSON(tableID) {
>    return $$("#"+tableID+" tr").map(function(row){
>      return row.descendants().pluck("innerHTML");
>    }).toJSON();
> }
>
> Best,
> Thomas
>
> Am 14.06.2007 um 03:58 schrieb David Dashifen Kees:
>
>>
>> Not that I''m aware of without parsing the table element using
the DOM
>> walking functions.  Something like this might do it:
>>
>> function tableToJSON(tableID) {
>>     var tableData = $A();
>>     var rows = $$("#"+tableID+" tr).each(function(row) {
>>        var cellData =
$H(row.descendants().pluck("innerHTML"));
>>        tableData.push(cellData);
>>     });
>>
>>     return tableData.toJSON();
>> }
>>
>> But I''ve not tried it, nor am I sure that will work.
>>
>>  - Dash -
>> phil swenson wrote:
>>> Is there a way to retrieve an HTML table in javascript and
>>> convert it
>>> to JSON using prototype?
>>>
>>> I''m stumped :(
>>>
>>> phil
>>>
>>>
>>>>
>>>
>>>
>>
>>>