function setup() {
createCanvas(500, 300);
textSize(16);
findQueryInput = createInput();
findQueryInput.position(30, 40);
getColBtn =
createButton("Get the matching row");
getColBtn.position(30, 70);
getColBtn.mouseClicked(getFindResults);
table = new p5.Table();
table.addColumn("name");
table.addColumn("age");
let newRow = table.addRow();
newRow.setString("name", "Sam");
newRow.setString("age", 23);
newRow = table.addRow();
newRow.setString("name", "Max");
newRow.setString("age", 20);
newRow = table.addRow();
newRow.setString("name", "Ben");
newRow.setString("age", 17);
newRow = table.addRow();
newRow.setString("name", "Sam");
newRow.setString("age", 23);
showTable();
}
function getFindResults() {
clear();
let findQuery = findQueryInput.value();
if (findQuery != "") {
findResults
= table.findRow(findQuery, 1);
if (findResults) {
text("The row that matches the query is",
20, 120);
text(findResults.arr[0], 20, 140);
text(findResults.arr[1], 120, 140);
}
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 'age' column 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 'age' column table",
20, 20);
}