Open In App

p5.js Table getRows() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The getRows() method of p5.Table in p5.js is used to return a reference to all rows in the table as an array of p5.TableRow objects. Each of the returned row objects can be used to get and set values as required.
 

Syntax:  

getRows()

Parameters: This function does not accept any parameters.
 

Below example illustrates the getRows() function in p5.js:
Example: 

javascript




function setup() {
    createCanvas(500, 400);
    textSize(16);
 
    text("Click on the button to display" +
        "all the rows in the table", 20, 20);
 
    getColBtn = createButton("Show all rows");
    getColBtn.position(30, 50);
    getColBtn.mouseClicked(showAllRows);
 
    // Create the table
    table = new p5.Table();
 
    // Add two columns
    table.addColumn("movie");
    table.addColumn("rating");
    table.addColumn("price");
 
    // 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)));
        newRow.setString("price",
            "$" + floor(random(10, 100)));
    }
}
 
function showAllRows() {
    clear();
 
    let currentRows = table.getRows();
 
    // Display the total rows
    // present in the table
    text("There are " +
        currentRows.length +
        " rows in the table", 20, 100);
 
    for (let r = 0; r < currentRows.length; r++)
        text(currentRows[r].arr.toString(),
            20, 140 + r * 20);
 
    text("Click on the button to display" +
        "all the rows in the table", 20, 20);
}


Output: 
 

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



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