function
setup() {
createCanvas(500, 300);
textSize(16);
addRowBtn = createButton(
"Add Row"
);
addRowBtn.position(30, 40);
addRowBtn.mouseClicked(addNewRow);
table =
new
p5.Table();
table.addColumn(
"author"
);
table.addColumn(
"language"
);
}
function
addNewRow() {
let newRow = table.addRow();
newRow.setString(
"author"
,
"Author "
+ floor(random(1, 100)));
newRow.setString(
"langauge"
,
"Langauge "
+ floor(random(1, 100)));
}
function
draw() {
clear();
text(
"The table has "
+ table.getRowCount() +
" rows"
, 20, 20);
text(
"Author"
, 20, 80);
text(
"Language"
, 120, 80);
for
(let r = 0; r < table.getRowCount(); r++)
for
(let c = 0; c < table.getColumnCount(); c++)
text(table.getString(r, c), 20 + c * 100, 120 + r * 20);
}