Open In App

p5.Table getColumnCount() Method

Last Updated : 08 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The getColumnCount() method of p5.Table in p5.js is used to return the total number of columns in a table object.

Syntax:

getColumnCount()

Parameters: This function does not accept any parameters.

Return Value: It returns an integer value which specifies the number of columns in the table.

Below example illustrates the getColumnCount() method in p5.js:

Example:




let colCount = 3;
  
function setup() {
  createCanvas(500, 400);
  textSize(16);
  
  addColBtn = createButton("Add Column");
  addColBtn.position(30, 50);
  addColBtn.mouseClicked(addOneColumn);
  
  removeColBtn =
    createButton("Clear Last Column");
  removeColBtn.position(160, 50);
  removeColBtn.mouseClicked(clearLastColumn);
  
  // Create the table
  table = new p5.Table();
  
  // Add columns
  table.addColumn("Column 1");
  table.addColumn("Column 2");
  
  // Display the table
  showTable();
}
  
function clearLastColumn() {
  let lastColumn =
      table.getColumnCount() - 1;
  if (lastColumn >= 0)
    table.removeColumn(lastColumn);
  
  showTable();
}
  
function addOneColumn() {
  table.addColumn("Column " + colCount);
  colCount++;
  
  showTable();
}
  
function showTable() {
  clear();
  text("Click on the buttons to change" +
       " the number of columns in the table",
       20, 20);
  
  // Get the number of columns
  // currently in the table
  let columnCount = table.getColumnCount();
  
  // Display the total columns
  // present in the table
  text("There are " + columnCount + 
       " columns in the table",
       20, 100);
  
  // Show all the column names
  // currently present in the table
  for (let c = 0; c < columnCount; c++)
    text(table.columns, 30, 140 + c * 20);
}


Output:
getColumnCount-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/getColumnCount



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads