p5.Table getRowCount() 方法
p5.js 中 p5.Table 的getRowCount() 方法用于返回一个表对象的总行数。
句法:
getRowCount()
参数:此函数不接受任何参数。
返回值:它返回一个整数值,它指定表中的行数。
下面的示例说明了 p5.js 中的getRowCount() 方法:
例子:
let rowCount = 1;
function setup() {
createCanvas(500, 400);
textSize(16);
addRowBtn = createButton("Add Row");
addRowBtn.position(30, 50);
addRowBtn.mouseClicked(addOneRow);
removeRowBtn =
createButton("Clear Last Row");
removeRowBtn.position(160, 50);
removeRowBtn.mouseClicked(clearLastRow);
// Create the table
table = new p5.Table();
// Add columns
table.addColumn("book");
table.addColumn("price");
// Display the table
showTable();
}
function addOneRow() {
let newRow = table.addRow();
newRow.set('book', "Book " + rowCount);
newRow.set('price', "Price " +
(rowCount * random(1, 10)).toFixed(1));
rowCount++;
showTable();
}
function clearLastRow() {
let lastRow = table.getRowCount() - 1;
if (lastRow >= 0)
table.removeRow(lastRow);
showTable();
}
function showTable() {
clear();
text("Click on the buttons to change" +
" the number of rows in the table",
20, 20);
// Get the number of rows in the table
let rowCount = table.getRowCount();
// Display the total rows
// present in the table
text("There are " + rowCount +
" rows in the table", 20, 100);
// Show all the rows currently
// present in the table
for (let r = 0; r < rowCount; r++) {
let currRow = table.rows[r].arr.toString();
currRow = currRow.split(", ").join(" ");
text(currRow, 30, 140 + r * 20);
}
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.Table/getRowCount