function
setup() {
createCanvas(500, 400);
textSize(16);
rowInput = createInput();
rowInput.position(30, 40);
clearRowBtn = createButton(
"Clear Given Row"
);
clearRowBtn.position(30, 70);
clearRowBtn.mouseClicked(clearRow);
table =
new
p5.Table();
table.addColumn(
"author"
);
table.addColumn(
"book"
);
for
(let i = 0; i < 10; i++) {
let newRow = table.addRow();
newRow.setString(
"author"
,
"Author "
+ floor(random(1, 100)));
newRow.setString(
"book"
,
"Book "
+ floor(random(1, 100)));
}
getTableRows();
}
function
clearRow() {
clear();
text(
"Click on the button to clear the given row in the table"
, 20, 20);
let rowToClear = int(rowInput.value());
if
(rowToClear >= 0 && rowToClear < table.getRowCount())
table.removeRow(rowToClear);
text(
"Last row cleared!"
, 20, 140);
getTableRows();
}
function
getTableRows() {
clear();
text(
"Click on the button to clear the given row in the table"
, 20, 20);
text(
"There are "
+ table.getRowCount() +
" rows in the table"
, 20, 120);
for
(let i = 0; i < table.getRowCount(); i++) {
let rowContents = table.rows[i].arr.toString();
text(
"Row "
+ i +
": "
+ rowContents, 20, 160 + i * 20);
}
}