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

p5.Table getRowCount() Method

The getRowCount() method of p5.Table in p5.js is used to return the total number of rows in a table object.

Syntax:

getRowCount()

Parameters: This function does not accept any parameters.

Return Value: It returns an integer value which specifies the number of rows in the table.

Below example illustrates the getRowCount() method in p5.js:

Example:




let rowCount = 1;
  
function setup() {
  createCanvas(500, 400);
  textSize(16);
  
  addRowBtn = createButton("Add Row");
  addRowBtn.position(30, 50);
  addRowBtn.mouseClicked(addOneRow);
  
  removeRowBtn =
    createButton("Clear Last Row");
  removeRowBtn.position(160, 50);
  removeRowBtn.mouseClicked(clearLastRow);
  
  // Create the table
  table = new p5.Table();
  
  // Add columns
  table.addColumn("book");
  table.addColumn("price");
  
  // Display the table
  showTable();
}
  
function addOneRow() {
  let newRow = table.addRow();
  newRow.set('book', "Book " + rowCount);
  newRow.set('price', "Price " +
    (rowCount * random(1, 10)).toFixed(1));
  
  rowCount++;
  
  showTable();
}
  
function clearLastRow() {
  let lastRow = table.getRowCount() - 1;
  if (lastRow >= 0) 
    table.removeRow(lastRow);
  
  showTable();
}
  
function showTable() {
  clear();
  text("Click on the buttons to change" +
       " the number of rows in the table",
       20, 20);
  
  // Get the number of rows in the table
  let rowCount = table.getRowCount();
  
  // Display the total rows
  // present in the table
  text("There are " + rowCount +
       " rows in the table", 20, 100);
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < rowCount; r++) {
    let currRow = table.rows[r].arr.toString();
    currRow = currRow.split(", ").join("  ");
  
    text(currRow, 30, 140 + r * 20);
  }
}


Output:
getRowCount-ex

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/getRowCount

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