2013-07-17 4 views
1

그래서 위젯 HTML_PARSER 개체를 수정하여 YUI 3 DataTable 위젯으로 html 테이블을 구문 분석하려고합니다.위젯 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> 

자바 스크립트

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

답변

0

dataKey를 가져올 때 columns 대신 cols 메쏘드를 호출 중입니다. 그건 그렇고, 각 셀에 대해 호출해서는 안됩니다. 너무 느립니다. 데이터 셀을 반복하기 전에 배열로 열 키를 가져 와서 로컬 변수에 저장하십시오. 그 이외에, 나이에 그것을하지 않고 무언가를 잊고 있을지 모르지만 그것은 나에게 좋게 보인다.

suerte