Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptp5.js Table getRow() Method

p5.js Table getRow() Method

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:
getRow-specified

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments