The getRow() method of p5.Table in p5.js is used to return a reference to the specified row as a p5.TableRow object. The returned row object can be used to get and set values as needed.
Syntax:
getRow( rowID )
Parameters: This function accepts a single parameter as mentioned above and described below:
- rowID: It is a number that denotes the ID of the row to be returned.
Below example illustrates the getRow() function in p5.js:
Example:
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); // Create the table table = new p5.Table(); // Add two columns table.addColumn( "movie" ); table.addColumn( "rating" ); // Add 10 randomly generated rows 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(); // Get the row values using getRow() method if (rowToFind >= 0 && rowToFind < table.getRowCount()) { requested_row = table.getRow(rowToFind); // Loop through the array // to display the values 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(); // Display the total rows present in the table 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); } |
Output:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.Table/getRow