function
setup() {
createCanvas(500, 300);
textSize(16);
findQueryInput = createInput();
findQueryInput.position(30, 50);
getColBtn =
createButton(
"Get the matching rows"
);
getColBtn.position(30, 80);
getColBtn.mouseClicked(getFindResults);
table =
new
p5.Table();
table.addColumn(
"fruit"
);
table.addColumn(
"price"
);
let newRow = table.addRow();
newRow.setString(
"fruit"
,
"Apple"
);
newRow.setString(
"price"
, 100);
newRow = table.addRow();
newRow.setString(
"fruit"
,
"Banana"
);
newRow.setString(
"price"
, 230);
newRow = table.addRow();
newRow.setString(
"fruit"
,
"Grapes"
);
newRow.setString(
"price"
, 50);
newRow = table.addRow();
newRow.setString(
"fruit"
,
"Apple"
);
newRow.setString(
"price"
, 45);
newRow = table.addRow();
newRow.setString(
"fruit"
,
"Apple"
);
newRow.setString(
"price"
, 65);
showTable();
}
function
getFindResults() {
clear();
let findQuery = findQueryInput.value();
if
(findQuery !=
""
) {
findResults =
table.findRows(findQuery,
'fruit'
);
if
(findResults.length > 0) {
text(
"The rows that match the query are"
,
20, 120);
for
(let i = 0; i < findResults.length; i++) {
text(findResults[i].arr[0],
20, 140 + i * 20);
text(findResults[i].arr[1],
120, 140 + i * 20);
}
}
else
text(
"No Results Found"
, 20, 120);
}
else
text(
"The query string is empty"
, 20, 120);
text(
"Enter a string to find it"
+
" in the table"
, 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(
"Enter a string to find it"
+
" in the table"
, 20, 20);
}