使HTML_PARSER在小部件DataTable中工作

Making a HTML_PARSER to work in widget DataTable

本文关键字:DataTable 工作 小部 HTML PARSER      更新时间:2023-09-26

所以我试图将一个html表解析为YUI3DataTable小部件,修改小部件html_PARSER对象。

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');

一定是出了什么问题。也许我看错了文件。需要一些帮助。PLAYGROUND

获取dataKey时,调用的是方法cols而不是columns。顺便说一句,你不应该为每个细胞调用它,它太慢了。在数据单元上循环之前,将列键获取到数组中,并将其保存在本地变量中。除此之外,它对我来说很好,尽管我已经很久没有做过了,可能会忘记一些事情。

翻毛皮