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);
table =
new
p5.Table();
table.addColumn(
"movie"
);
table.addColumn(
"rating"
);
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();
if
(rowToFind >= 0 &&
rowToFind < table.getRowCount()) {
requested_row = table.getRow(rowToFind);
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();
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);
}