gpt4 book ai didi

javascript - 使 HTML_PARSER 在小部件 DataTable 中工作

转载 作者:行者123 更新时间:2023-11-28 09:01:15 24 4
gpt4 key购买 nike

所以我试图将 html 表解析为 YUI 3 DataTable 小部件,修改小部件 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');

肯定有什么问题。也许我误读了documentation 。需要一些帮助。 PLAYGROUND

最佳答案

当您获取 dataKey 时,您正在调用方法 cols 而不是 columns。顺便说一句,您不应该为每个单元格调用它,否则会太慢。在循环数据单元格之前将列键放入数组中并将它们保存在局部变量中。除此之外,它对我来说看起来不错,尽管我已经很久没有这样做了,并且可能会忘记一些东西。

苏尔特

关于javascript - 使 HTML_PARSER 在小部件 DataTable 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17707594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com