簡體   English   中英

如何計算HTML表中的行數?

[英]How to count the number of rows in a HTML table?

我似乎無法在任何地方找到它。 我有一個像這樣的dataTable,我希望能夠在javascript中計算dataTable中的行數。 怎么會這樣呢? 謝謝!

我需要在javascript中使用它,因為我想遍歷數據表並檢查是否有任何行包含其中的某些數據。

示例PSEUDOCODE

var tableSize = someway to get table size;
for (i = 0; i < tableSize; i++) {
if (dataTable.row(i).value == "someValue") {
    //do something
}

jQuery計算行會更容易:

var tableSize = $('#myTable tbody tr').length;

演示: http//jsfiddle.net/YLVuK/1/

使用jQuery,你可以做到

var tableSize = $("#myTable").find("tbody").find("td").length;

並使用ID“myTable”命名您的表

如果您的表有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
    }
}

您可以計算輔助bean中服務器端的數據表中的行數,然后將java對象屬性設置為等於行數。 然后在您的視圖中,您可以使用el表達式語言設置javascript變量。 像這樣的東西:

支持豆:

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();
    }
}

在你的xhtml文件中:

var tableSize = #{backer.numCars}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM