Javascript从rich:dataTable读取数据?

时间:2010-12-09 02:52:15

标签: javascript jsf richdatatable

我可以在表单初始化期间生成数据表。(JSF)。然后我想写一个javascript来读取来自rich:dataTable值的数据

<f:view>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

      <script type="text/javascript">
            function getTableValue()
            {
                //Here i want to write code for get one by one value  
                //from rich:datatable content
            }
      </script>

    </head>
    <body>
      <h:form id="tableForm" binding="#{DataTable.initForm}">

        <rich:dataTable id="salesTable" value="#{DataTable.saleList}" var="data">
          <f:facet name="header">
            <rich:columnGroup>
              <rich:column>
                <h:outputText value="Sales" />
              </rich:column>
            </rich:columnGroup>
          </f:facet>
          <rich:column>
            <h:outputText value="#{data.salesPercentage}" />
          </rich:column>
        </rich:dataTable>

        <input id="tableButton" type="button" value="getDataTableValue" onclick="getTableValue()" />

      </h:form>
    </body>
  </html>
</f:view>

帮帮我。 谢谢你的努力。

2 个答案:

答案 0 :(得分:2)

首先从文档中获取表格(右键单击页面,执行查看源以查看自己生成的id)。

var table = document.getElementById('tableForm:salesTable');

然后从表体中获取行作为数组。

var rows = table.getElementsByTagName("tbody")[0].getElementsByTagName("tr");

然后遍历行并从行中获取单元格作为数组。

for (var i = 0; i < rows.length; i++) {
    var cells = rows[i].getElementsByTagName("td");
}

然后遍历单元格并获取内容。

for (var j = 0; j < cells.length; j++) {
    var cell = .cells[j];
    alert(cell.innerHTML);
}

另见:

答案 1 :(得分:1)

function processDelete()
{
   var table = document.getElementById('AluminumPricingTab:forecastYearList');     var rows = table.getElementsByTagName("tbody")[0].getElementsByTagName("tr"); var yearAdd = "";
    for (var i = 0; i < rows.length; i++) {
        //alert("rows:" + i);       var cells = rows[i].getElementsByTagName("td");     var checked = document.getElementById("AluminumPricingTab:forecastYearList:"+i+":deleteSelectedRowCHK").checked;
        //alert("checked:" +checked); 
        if (checked)
        {              
             if(yearAdd != "")
                {
                yearAdd = yearAdd + ',' ;
                }
            else
                {
                  yearAdd;
                }
            var year = document.getElementById("AluminumPricingTab:forecastYearList:"+i+":year").value; 
            yearAdd = yearAdd + year;

            //alert("yearAdd:" +yearAdd);  
        }       
    }
     if (yearAdd != "")
         {
            var result = confirm(MSG_FORECASTS_DELETE_SELECTED +'\n\n'+ yearAdd);
            if (result == true)
            {   
                //alert("Row deleted"); 
                var bt1 = document.getElementById('AluminumPricingTab:deleteSelectedForecast');
                bt1.onclick='document.body.style.cursor = "wait";return true;';
                bt1.click();
            }   
            else 
            {
                return false;   
            }
         }
     else
         {
           alert("Select Row to deleted"); 
         }
  }