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

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

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
>>
>>
>>>
>>
>>