4

これはどこにも見つからないようです。このようなdataTableがあり、javascriptでdataTableの行数をカウントできるようにしたいと考えています。それはどのように達成されますか?ありがとう!

データテーブルを反復処理し、行に特定のデータを含めることができるかどうかを確認するため、JavaScriptで必要です。

擬似コードの例

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

4 に答える 4

14

jQueryを使用して行を数える方が簡単です。

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

デモ: http: //jsfiddle.net/YLVuK/1/

于 2012-07-24T19:49:29.667 に答える
1

jQueryを使用すると、次のことが簡単にできます。

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

そして、「myTable」のIDでテーブルに名前を付けます

于 2012-07-24T19:46:48.757 に答える
0

if your table has an id, you can do it as follows:

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
    }
}
于 2012-07-24T20:15:35.593 に答える
0

バッキング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}
于 2012-07-24T20:44:58.290 に答える