[英]Making a HTML_PARSER to work in widget DataTable
因此,我正在嘗試將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>
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');
當您獲取dataKey時,您在調用方法cols
而不是columns
。 順便說一句,您不應該為每個單元格調用它,因為它太慢了。 在遍歷數據單元之前,將列鍵放入數組中,並將其保存在局部變量中。 除此之外,它對我來說看起來不錯,盡管我已經很久沒有這樣做了,並且可能會忘記一些東西。
蘇爾特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.