Open In App

p5.Table getRowCount() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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



Last Updated : 08 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads