所以我试图将一个 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');