p5.js | p5.Table clearRows() Method

The clearRows() method of p5.Table in p5.js is used to clear all rows in a table. It does not affect columns and their titles. It can be used to clear a table without affecting its column structure. It has no parameters.
Syntax:
 

clearRows()

Parameters: This function has no parameter.
The example below illustrates the clearRows() function in p5.js:
Example:
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  getColBtn = createButton("Get Table Row Details");
  getColBtn.position(30, 50);
  getColBtn.mouseClicked(getTableRows);
  
  getColBtn = createButton("Clear Rows");
  getColBtn.position(30, 80);
  getColBtn.mouseClicked(clearAllRows);
  
  text("Click on the button to clear the"+
       " rows in the table", 20, 20);
  
  // Create the table
  table = new p5.Table();
  
  // Add columns
  table.addColumn("author");
  table.addColumn("book");
  
  // Add two rows
  let newRow = table.addRow();
  newRow.setString("author", "Marcel Proust");
  newRow.setString("book", "In Search of Lost Time");
  
  newRow = table.addRow();
  newRow.setString("author", "James Joyce");
  newRow.setString("book", "Ulysses");
}
  
function clearAllRows() {
  clear();
  text("Click on the button to clear"+
       " the rows in the table", 20, 20);
    
  // Use the clearRow() method to
  // clear all rows in the table
  table.clearRows();
  
  text("All rows cleared!", 20, 140);
}
  
function getTableRows() {
  clear();
  text("Click on the button to clear the rows "+
       "in the table", 20, 20);
  
  // Display all the rows present in the table
  text("There are " + table.getRowCount() + 
       " rows in the table", 20, 140);
  for (let i = 0; i < table.getRowCount(); i++) {
    let rowContents = table.rows[i].arr.toString();
    text("Row " + i + ": " + rowContents, 20, 160 + i * 20);
  }
}

chevron_right


Output:
 

clearRows-btn

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

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.