當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js Table getColumnCount()用法及代碼示例

p5.j​​s中p5.Table的getColumnCount()方法用於返回表對象中的列總數。

用法:

getColumnCount()

參數:該函數不接受任何參數。

返回值:它返回一個整數值,該值指定表中的列數。

下麵的示例說明了p5.js中的getColumnCount()方法:



例:

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); 
}

輸出:
getColumnCount-ex

在線編輯: https://editor.p5js.org/

環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

參考: https://p5js.org/reference/#/p5.Table/getColumnCount




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.Table getColumnCount() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。