p5.js中p5.Table的getRow()方法用於將對指定行的引用作為p5.TableRow對象返回。返回的行對象可用於根據需要獲取和設置值。
用法:
getRow( rowID )
參數:該函數接受如上所述和以下描述的單個參數:
- rowID:該數字表示要返回的行的ID。
下麵的示例說明了p5.js中的getRow()函數:
例:
function setup() {
createCanvas(500, 400);
textSize(16);
rowIDinput = createInput();
rowIDinput.position(30, 50);
getColBtn = createButton("Get Specified Row");
getColBtn.position(30, 80);
getColBtn.mouseClicked(getRow);
// Create the table
table = new p5.Table();
// Add two columns
table.addColumn("movie");
table.addColumn("rating");
// Add 10 randomly generated rows
for (let i = 0; i < 10; i++) {
let newRow = table.addRow();
newRow.setString("movie",
"Movie " + floor(random(1, 100)));
newRow.setString("rating",
floor(random(1, 5)));
}
showTable();
}
function getRow() {
clear();
let rowToFind = rowIDinput.value();
// Get the row values using getRow() method
if (rowToFind >= 0 &&
rowToFind < table.getRowCount()) {
requested_row = table.getRow(rowToFind);
// Loop through the array
// to display the values
text("Row with the same ID:", 20, 120);
for (let i = 0; i < requested_row.arr.length; i++) {
text(requested_row.arr[i],
20 + i * 120, 140);
}
} else
text("Please enter correct row ID", 20, 120);
text("Click on the button to get the specified row",
20, 20);
}
function showTable() {
clear();
// Display the total rows present in the table
text("There are " +
table.getRowCount() +
" rows in the table", 20, 120);
for (let r = 0; r < table.getRowCount(); r++)
for (let c = 0; c < table.getColumnCount(); c++)
text(table.getString(r, c),
20 + c * 100,
140 + r * 20);
text("Click on the button to get the specified row",
20, 20);
}
輸出:
在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.Table/getRow
相關用法
- HTML DOM console.table()用法及代碼示例
- HTML DOM Table deleteRow()用法及代碼示例
- HTML DOM Table createTFoot()用法及代碼示例
- HTML DOM Table deleteTFoot()用法及代碼示例
- HTML DOM Table insertRow()用法及代碼示例
- HTML DOM Table createTHead()用法及代碼示例
- HTML DOM Table deleteCaption()用法及代碼示例
- HTML DOM Table deleteTHead()用法及代碼示例
- HTML DOM Table createCaption()用法及代碼示例
- p5.js Table addColumn()用法及代碼示例
- p5.js Table getColumn()用法及代碼示例
- p5.js Table clearRows()用法及代碼示例
- p5.js Table removeRow()用法及代碼示例
- p5.js Table addRow()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js Table getRow() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。