Skip to content
Related Articles

Related Articles

Improve Article

p5.Table findRows() Method

  • Last Updated : 07 Jul, 2020

The findRows() method of p5.Table in p5.js is used to find all the rows that contain the given and value and returns a reference to those rows. The column that the method searches in can be specified as a parameter.

Syntax:

findRows( value, column )

Parameters: This function accepts two parameters as mentioned above and described below:

  • value: It is a String that specifies the value that has to be matched.
  • column: It is a String or Number that denotes the column name or column ID of the column to be searched.

The example below illustrates the findRows() method in p5.js:

Example 1:






function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  findQueryInput = createInput();
  findQueryInput.position(30, 50);
  
  getColBtn =
    createButton("Get the matching rows");
  getColBtn.position(30, 80);
  getColBtn.mouseClicked(getFindResults);
  
  // Create the table
  table = new p5.Table();
  
  // Add two columns
  table.addColumn("fruit");
  table.addColumn("price");
  
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 100);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Banana");
  newRow.setString("price", 230);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Grapes");
  newRow.setString("price", 50);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 45);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 65);
  
  showTable();
}
  
function getFindResults() {
  clear();
  
  let findQuery = findQueryInput.value();
  
  // Get the row values using findRows()
  if (findQuery != "") {
  
    // Find the results in the column of 'fruit' 
    findResults =
      table.findRows(findQuery, 'fruit');
  
    if (findResults.length > 0) {
      text("The rows that match the query are",
           20, 120);
  
      // Display the matched rows
      for (let i = 0; i < findResults.length; i++) {
          
        text(findResults[i].arr[0],
             20, 140 + i * 20);
        text(findResults[i].arr[1],
             120, 140 + i * 20);
      }
    }
    else text("No Results Found", 20, 120); 
      
  } else
    text("The query string is empty", 20, 120);
  
    text("Enter a string to find it" +
         " in the table", 20, 20);
}
  
function showTable() {
  clear();
  
  // Display the total rows
  // present in the table
  text("There are " +
       table.getRowCount() +
       " rows in the table", 20, 120);
  
  for (let r = 0; r < table.getRowCount(); r++)
    for (let c = 0; c < table.getColumnCount(); c++)
      text(table.getString(r, c),
           20 + c * 100, 140 + r * 20);
  
      text("Enter a string to find it" +
           " in the table", 20, 20);
}

Output:

findRows-ex1

Example 2:




function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  findQueryInput = createInput();
  findQueryInput.position(30, 50);
  
  getColBtn = 
    createButton("Get the matching rows");
  getColBtn.position(30, 80);
  getColBtn.mouseClicked(getFindResults);
  
  // Create the table
  table = new p5.Table();
  
  // Add two columns
  table.addColumn("fruit");
  table.addColumn("price");
  
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 100);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Banana");
  newRow.setString("price", 100);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Grapes");
  newRow.setString("price", 100);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 50);
  
  newRow = table.addRow();
  newRow.setString("fruit", "Apple");
  newRow.setString("price", 65);
  
  showTable();
}
  
function getFindResults() {
  clear();
  
  let findQuery =
      findQueryInput.value();
  
  // Get the row values using findRows()
  if (findQuery != "") {
  
    // Find the results in the column of 'price' 
    findResults =
      table.findRows(findQuery, 'price');
  
    if (findResults.length > 0) {
      text("The rows that match the query are",
           20, 120);
  
      // Display the matched rows
      for (let i = 0; i < findResults.length; i++) {
          
        text(findResults[i].arr[0],
             20, 140 + i * 20);
        text(findResults[i].arr[1],
             120, 140 + i * 20);
      }
    }
    else text("No Results Found", 20, 120);
      
  } else
    text("The query string is empty", 20, 120);
  
  text("Enter a string to find it"
       " in the table", 20, 20);
}
  
function showTable() {
  clear();
  
  // Display the total rows
  // present in the table
  text("There are " +
       table.getRowCount() +
       " rows in the table",
       20, 120);
  
  for (let r = 0; r < table.getRowCount(); r++)
    for (let c = 0; c < table.getColumnCount(); c++)
      text(table.getString(r, c),
           20 + c * 100,
           140 + r * 20);
  
  text("Enter a string to find it"
       " in the table", 20, 20);
}

Output:

findRows-ex2

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :