I can generate datatable during form initialization.(JSF). Then I want to write javascript for read data from rich:dataTable values
<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>
Help me. Thanks for your effort.
First get the table from the document (rightclick page, do View Source to see generated id
yourself).
var table = document.getElementById('tableForm:salesTable');
Then get the rows as array from the table body.
var rows = table.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
Then loop over rows and get the cells as array from the row.
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].getElementsByTagName("td");
}
Then loop over the cells and get the content.
for (var j = 0; j < cells.length; j++) {
var cell = .cells[j];
alert(cell.innerHTML);
}
See also:
- Hunlock JavaScript tutorial
- Gecko HTML DOM introduction
- W3schools HTML DOM reference
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");
}
}
精彩评论