📜  p5.TableRow set() 方法

📅  最后修改于: 2022-05-13 01:58:10.623000             🧑  作者: Mango

p5.TableRow set() 方法

p5.js 中 p5.TableRow 的set() 方法用于将给定值存储到表的给定列中。列可以由其列 ID 或名称指定。

句法:

set( column, value )

参数:此方法接受上面提到的两个参数,如下所述:

  • column:它是一个字符串或数字,表示列的名称或列的 ID。
  • value:它是一个字符串或数字,指定必须存储的值。

下面的示例说明了 p5.js 中的set() 方法

示例 1:

javascript
function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  text("Click on the button to set " +
       "the values of the table",
       20, 20);
  
  setBtn =
    createButton("Add Table Values");
  setBtn.position(30, 40);
  setBtn.mouseClicked(setTableData);
  
  // Create the table
  table = new p5.Table();
  
  // Add 5 columns and rows to the table
  for (let i = 0; i < 5; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
}
  
function setTableData() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 3; c++) {
  
      // Get the row of the table
      let tableRow = table.rows[r];
  
      // Set the value at the given
      // column of the row
      tableRow.set(c, r + "::" + c);
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The values of the table are " +
       "added using the set() method",
       20, 20);
  
  // Show all the columns present
  for (let c = 0; c < 3; c++) {
    text(table.columns,
         20 + 100 * c, 80);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 3; c++) {
      text(table.getString(r, c),
           20 + 100 * c, 120 + 20 * r);
    }
  }
}


javascript
function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  changeBtn =
    createButton("Modify Table");
  changeBtn.position(30, 40);
  changeBtn.mouseClicked(modifyTable);
  
  // Create the table
  table = new p5.Table();
  
  // Add 5 columns and rows to the table
  for (let i = 0; i < 5; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
  
  setTableData();
  
  // Display the table
  showTable();
}
  
function setTableData() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      // Set the value at the given
      // column and row of the table
      table.set(r, c, 0 + " " + 0);
    }
  }
  
  showTable();
}
  
function modifyTable() {
  for (let r = 0; r < 5; r++) {
  
    // Get the row of the table
    let tableRow = table.rows[r];
  
    for (let c = 0; c < 5; c++) {
  
      // If the index of the table is
      // a diagonal, set it to "XX"
      if (r == c) 
        tableRow.set(c, "XX");
      if (r + c == 4)
        tableRow.set(c, "YY");
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The values are added and " +
       "modified using the set() method",
       20, 20);
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      text(table.getString(r, c),
           20 + 80 * c, 80 + 20 * r);
    }
  }
}


输出:


示例 2:

javascript

function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  changeBtn =
    createButton("Modify Table");
  changeBtn.position(30, 40);
  changeBtn.mouseClicked(modifyTable);
  
  // Create the table
  table = new p5.Table();
  
  // Add 5 columns and rows to the table
  for (let i = 0; i < 5; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
  
  setTableData();
  
  // Display the table
  showTable();
}
  
function setTableData() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      // Set the value at the given
      // column and row of the table
      table.set(r, c, 0 + " " + 0);
    }
  }
  
  showTable();
}
  
function modifyTable() {
  for (let r = 0; r < 5; r++) {
  
    // Get the row of the table
    let tableRow = table.rows[r];
  
    for (let c = 0; c < 5; c++) {
  
      // If the index of the table is
      // a diagonal, set it to "XX"
      if (r == c) 
        tableRow.set(c, "XX");
      if (r + c == 4)
        tableRow.set(c, "YY");
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The values are added and " +
       "modified using the set() method",
       20, 20);
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      text(table.getString(r, c),
           20 + 80 * c, 80 + 20 * r);
    }
  }
}

输出:

在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TableRow/set