function
setup() {
createCanvas(500, 400);
textSize(16);
columnInput = createInput();
columnInput.position(30, 40);
clearColBtn =
createButton(
"Clear Given Column"
);
clearColBtn.position(30, 70);
clearColBtn.mouseClicked(clearColumn);
table =
new
p5.Table();
table.addColumn(
"author"
);
table.addColumn(
"book"
);
table.addColumn(
"price"
);
table.addColumn(
"rating"
);
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)));
newRow.setString(
"price"
,
"$"
+ floor(random(10, 100)));
newRow.setString(
"rating"
,
random(1, 5).toFixed(2));
}
getTableRows();
}
function
clearColumn() {
clear();
text(
"Click on the button to clear "
+
"the given column in the table"
,
20, 20);
let colToClear =
int(columnInput.value());
if
(colToClear >= 0 &&
colToClear < table.getColumnCount())
table.removeColumn(colToClear);
text(
"Last column cleared!"
, 20, 140);
getTableRows();
}
function
getTableRows() {
clear();
text(
"Click on the button to clear "
+
"the given column in the table"
,
20, 20);
text(
"There are "
+
table.getColumnCount() +
" columns 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);
}