简体   繁体   English

使HTML_PARSER在小部件DataTable中工作

[英]Making a HTML_PARSER to work in widget DataTable

So i'm trying to parse an html table to the YUI 3 DataTable widget, modifiyng the widget HTML_PARSER object. 因此,我正在尝试将HTML表解析为YUI 3 DataTable小部件,以修改小部件HTML_PARSER对象。

HTML 的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 Java脚本

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

When you are getting dataKey, you are calling method cols instead of columns . 当您获取dataKey时,您在调用方法cols而不是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 苏尔特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM