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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
  }
}

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.