[英]How to count the number of rows in a HTML table?
I can't seem to find this anywhere. 我似乎无法在任何地方找到它。 I have a dataTable like this one , and I want to be able to count the number of rows in the dataTable in javascript.
我有一个像这样的dataTable,我希望能够在javascript中计算dataTable中的行数。 how would that be accomplished?
怎么会这样呢? thanks!
谢谢!
I need it in javascript because I want to iterate through the datatable and check to see if any rows cantain certain data in them. 我需要在javascript中使用它,因为我想遍历数据表并检查是否有任何行包含其中的某些数据。
EXAMPLE PSEUDOCODE 示例PSEUDOCODE
var tableSize = someway to get table size;
for (i = 0; i < tableSize; i++) {
if (dataTable.row(i).value == "someValue") {
//do something
}
It would be easier to count the rows with jQuery : 用jQuery计算行会更容易:
var tableSize = $('#myTable tbody tr').length;
Demo: http://jsfiddle.net/YLVuK/1/ 演示: http : //jsfiddle.net/YLVuK/1/
Using jQuery, you could simply do 使用jQuery,你可以做到
var tableSize = $("#myTable").find("tbody").find("td").length;
And name your table with an ID of "myTable" 并使用ID“myTable”命名您的表
if your table has an id, you can do it as follows: 如果您的表有id,您可以按如下方式执行:
var table = document.getElementById("tableId");
for(var i = 0, ceiling = table.rows.length; i < ceiling; i++) {
var row = table.rows[i]; // this gets you every row
for(var j = 0, ceiling2 = row.cells[j]; j < ceiling2; j++) {
var cell = row.cells[j]; // this gets you every cell of the current row in the loop
var cellContent = cell.innerHTML; // this gets you the content of the current cell in the loop
}
}
You can count the number of rows in a datatable on the server side in your backing bean and then set a java object attribute equal to the number of rows. 您可以计算辅助bean中服务器端的数据表中的行数,然后将java对象属性设置为等于行数。 Then in your view, you can set the javascript variable using el expression language.
然后在您的视图中,您可以使用el表达式语言设置javascript变量。 Something like this:
像这样的东西:
Backing bean: 支持豆:
public class backer() {
List<Cars> cars = new ArrayList<Cars>();
public List<Cars> getCars() {
return cars;
}
public void setCars(List<Cars> cars) {
this.cars = cars;
}
public int numCars() {
return cars.size();
}
}
In your xhtml file: 在你的xhtml文件中:
var tableSize = #{backer.numCars}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.