function
setup() {
createCanvas(500, 300);
textSize(18);
text(
"Enter the index of the table "
+
"to be modified"
, 20, 20);
rowInput = createInput();
rowInput.size(30);
rowInput.position(30, 40);
colInput = createInput();
colInput.size(30);
colInput.position(80, 40);
setBtn =
createButton(
"Modify Given Row and Column"
);
setBtn.position(30, 80);
setBtn.mouseClicked(modifyTableData);
table =
new
p5.Table();
for
(let i = 0; i < 5; i++) {
table.addColumn(
"Column "
+ i);
table.addRow();
}
for
(let r = 0; r < 5; r++) {
let tableRow = table.rows[r];
for
(let c = 0; c < 5; c++) {
tableRow.set(c, r * 100 + c);
}
}
showTable();
}
function
modifyTableData() {
givenRow = int(rowInput.value());
givenCol = int(colInput.value());
if
(givenRow < table.getRowCount() &&
givenCol < table.getColumnCount()) {
let tableRow = table.rows[givenRow];
tableRow.setNum(givenCol, -99999);
}
showTable();
}
function
showTable() {
clear();
text(
"The value is modified using "
+
"the setNum() method"
, 20, 20);
let colCount = table.getColumnCount();
let rowCount = table.getRowCount();
for
(let c = 0; c < colCount; c++) {
text(table.columns, 20 + 100 * c, 120);
}
for
(let r = 0; r < rowCount; r++) {
for
(let c = 0; c < colCount; c++) {
text(table.getString(r, c),
20 + 100 * c, 150 + 20 * r);
}
}
}