Question

So i'm trying to parse an html table to the YUI 3 DataTable widget, modifiyng the widget HTML_PARSER object.

HTML

<div id="table">
<table>
<thead>
<tr>
    <th>Tipo</th> 
    <th>Codigo</th> 
    <th>Descripcion</th>
    <th>Impuesto para la Venta</th>
    <th>Precio</th>
    <th>Precio con IVA</th>
    <th>Cantidad</th>
</tr>
</thead>
<tbody>
<tr>
    <td>Producto</td> 
    <td>1</td> 
    <td>7</td> 
    <td>12</td> 
    <td>7.00</td> 
    <td></td> 
    <td>7</td> 
</tr>
</tbody>
</table>
</div>

Javascript

Y.DataTable.HTML_PARSER = {
    columns:function(srcNode) {
        var cols = [];

        srcNode.all("th").each(function(th){
            var col = {
                // sets column "key" to contents of TH with spaces removed
                key:    th.getHTML().replace(/\s+/g, '').toLowerCase(),   
                label:  th.getHTML()                   
            };
            cols.push(col);
        });
        return cols;
    },
    data:function(srcNode) {
        var data = [];
        srcNode.all("tbody tr").each(function(tr){
            var col = {};
            tr.all("td").each( function(td_item, td_index){
               // extracts the "key" name from the column based on it's TD index
                var dataKey = Y.DataTable.HTML_PARSER.cols[td_index].key,    
                    data = td_item.getHTML();               
                // sets "key:data" for this TD element ...    
                col[dataKey] = data;    
            });
            data.push(col);  
        });
        return data;
   }
};

new Y.DataTable({srcNode:'#table'}).render('#table');

There must be something wrong. Maybe i misread the documentation. Need some help. PLAYGROUND

Was it helpful?

Solution

When you are getting dataKey, you are calling method cols instead of columns. By the way, you shouldn't call it for each cell, it would be too slow. Get the column keys into an array before looping over the data cells and save them in a local variable. Other than that, it looks good to me though I haven't done it in ages and might be forgetting something.

Suerte

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top