1
所以我想解析一个HTML表格到YUI 3 DataTable小部件,修改小部件HTML_PARSER对象。使HTML_PARSER工作在小部件DataTable
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