p5.js中p5.Table的removeColumn()方法用於從表中刪除給定的列。可以通過使用其標題或索引值(列ID)來指定要刪除的列。
用法:
removeColumn( column )
參數:該函數接受如上所述和以下描述的單個參數:
- column:它是一個字符串或數字,表示要刪除的列名稱或列ID。
下麵的示例說明了p5.js中的removeColumn()函數:
範例1:
function setup() {
createCanvas(500, 400);
textSize(16);
removeColBtn =
createButton("Clear Last Column");
removeColBtn.position(30, 60);
removeColBtn.mouseClicked(clearLastColumn);
// Create the table
table = new p5.Table();
// Add columns
table.addColumn("author");
table.addColumn("book");
table.addColumn("price");
table.addColumn("rating");
// Add 10 random rows to the table
for (let i = 0; i < 10; i++) {
let newRow = table.addRow();
newRow.setString("author",
"Author " + floor(random(1, 100)));
newRow.setString("book",
"Book " + floor(random(1, 100)));
newRow.setString("price",
"$" + floor(random(10, 100)));
newRow.setString("rating",
random(1, 5).toFixed(2));
}
// Display the rows currently present
getTableRows();
}
function clearLastColumn() {
clear();
text("Click on the button to clear "
+ "the last column in the table",
20, 20);
// Get the index of the last column
// of the table
let lastColumn =
table.getColumnCount() - 1;
// Use the removeColumn() method to
// clear the given column of the table
if (lastColumn >= 0)
table.removeColumn(lastColumn);
text("Last column cleared!", 20, 140);
getTableRows();
}
function getTableRows() {
clear();
text("Click on the button to clear "
+ "the last column in the table",
20, 20);
// Display the total columns
// present in the table
text("There are " +
table.getColumnCount() +
" columns in the table", 20, 120);
// Show the table with the columns and rows
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);
}
輸出:
範例2:
function setup() {
createCanvas(500, 400);
textSize(16);
columnInput = createInput();
columnInput.position(30, 40);
clearColBtn =
createButton("Clear Given Column");
clearColBtn.position(30, 70);
clearColBtn.mouseClicked(clearColumn);
// Create the table
table = new p5.Table();
// Add columns
table.addColumn("author");
table.addColumn("book");
table.addColumn("price");
table.addColumn("rating");
// Add 10 random rows to the table
for (let i = 0; i < 10; i++) {
let newRow = table.addRow();
newRow.setString("author",
"Author " + floor(random(1, 100)));
newRow.setString("book",
"Book " + floor(random(1, 100)));
newRow.setString("price",
"$" + floor(random(10, 100)));
newRow.setString("rating",
random(1, 5).toFixed(2));
}
// Display the rows currently present
getTableRows();
}
function clearColumn() {
clear();
text("Click on the button to clear "
+ "the given column in the table",
20, 20);
// Get the index of the column to be removed
let colToClear =
int(columnInput.value());
// Use the removeColumn() method to
// clear the given column of the table
if (colToClear >= 0 &&
colToClear < table.getColumnCount())
table.removeColumn(colToClear);
text("Last column cleared!", 20, 140);
getTableRows();
}
function getTableRows() {
clear();
text("Click on the button to clear "
+ "the given column in the table",
20, 20);
// Display the total columns
// present in the table
text("There are " +
table.getColumnCount() +
" columns 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);
}
輸出:
在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.Table/removeColumn
相關用法
- HTML DOM console.table()用法及代碼示例
- HTML DOM Table deleteRow()用法及代碼示例
- HTML DOM Table createTFoot()用法及代碼示例
- HTML DOM Table deleteTFoot()用法及代碼示例
- HTML DOM Table insertRow()用法及代碼示例
- HTML DOM Table createTHead()用法及代碼示例
- HTML DOM Table deleteCaption()用法及代碼示例
- HTML DOM Table deleteTHead()用法及代碼示例
- HTML DOM Table createCaption()用法及代碼示例
- p5.js Table addColumn()用法及代碼示例
- p5.js Table getColumn()用法及代碼示例
- p5.js Table clearRows()用法及代碼示例
- p5.js Table removeRow()用法及代碼示例
- p5.js Table addRow()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js Table removeColumn() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。