📜  p5.Table getString() 方法

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

p5.Table getString() 方法

p5.js 中 p5.Table 的getString() 方法用于从表的给定行和列中检索 String 值。行可以由行 ID 指定,列可以由其列 ID 或列名指定。

句法:

getString( row, column )

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

  • row:它是一个数字,表示行的行ID。
  • column:它是一个字符串或数字,表示列的名称或列的 ID。

返回值:此方法返回一个字符串,该字符串是从表的给定行和列中检索到的值。

以下示例说明了 p5.js 中的getString() 方法

示例 1:

Javascript
function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the button to get all " +
  "string values of the table", 20, 20);
  
  setBtn =
    createButton("Get all table values");
  setBtn.position(30, 40);
  setBtn.mouseClicked(showTable);
  
  // Create the table
  table = new p5.Table();
  
  setTableData();
}
  
function setTableData() {
  table.addColumn('Waterfall');
  table.addColumn('Height');
  
  let tableRow = table.addRow();
  tableRow.setString('Waterfall',
                     'Angel Falls');
  tableRow.setString('Height',
                     '979 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tugela Falls');
  tableRow.setString('Height', '948 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tres Hermanas Falls');
  tableRow.setString('Height', '914 metres');
}
  
function showTable() {
  clear();
  text("The values of the table are retrieved " +
  "using the getString() method", 20, 20);
  
  // Show all the columns present
  for (let c = 0; c < table.getColumnCount(); c++) {
    text(table.columns, 20 + 200 * c, 80);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < table.getRowCount(); r++) {
    for (let c = 0; c < table.getColumnCount(); c++) {
  
      // Get the string at the given row and column
      text(table.getString(r, c),
           20 + 200 * c,
           110 + 20 * r);
    }
  }
}


Javascript
function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the button to get the " +
       "specific string value of the table",
       20, 20);
  
  text("Enter row and column", 20, 60);
  
  rowInp = createInput();
  rowInp.position(30, 80);
  rowInp.size(30, 20);
  
  colInp = createInput();
  colInp.position(70, 80);
  colInp.size(30, 20);
  
  setBtn =
    createButton("Get value at row and column");
  setBtn.position(30, 110);
  setBtn.mouseClicked(getValueAt);
  
  // Create the table
  table = new p5.Table();
  
  setTableData()
}
  
function setTableData() {
  
  table.addColumn('Waterfall');
  table.addColumn('Height');
  
  let tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Angel Falls');
  tableRow.setString('Height', '979 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tugela Falls');
  tableRow.setString('Height', '948 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tres Hermanas Falls');
  tableRow.setString('Height', '914 metres');
  
}
  
function getValueAt() {
  clear();
  let rowToGet = int(rowInp.value());
  let colToGet = int(colInp.value());
  
  if (rowToGet < table.getRowCount() &&
      colToGet < table.getColumnCount()) {
    text("The value at row " + rowToGet +
         " and column " + colToGet + " is:",
         20, 160);
  
    // Get the string at the given row and column
    text(table.getString(rowToGet, colToGet),
         20, 180);
  }
  else
    text("Please enter correct row and " +
         "column values", 20, 160);
  
  
  text("Click on the button to get the " +
       "specific string value of the table",
       20, 20);
  text("Enter row and column", 20, 60);
}


输出:

示例 2:

Javascript

function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the button to get the " +
       "specific string value of the table",
       20, 20);
  
  text("Enter row and column", 20, 60);
  
  rowInp = createInput();
  rowInp.position(30, 80);
  rowInp.size(30, 20);
  
  colInp = createInput();
  colInp.position(70, 80);
  colInp.size(30, 20);
  
  setBtn =
    createButton("Get value at row and column");
  setBtn.position(30, 110);
  setBtn.mouseClicked(getValueAt);
  
  // Create the table
  table = new p5.Table();
  
  setTableData()
}
  
function setTableData() {
  
  table.addColumn('Waterfall');
  table.addColumn('Height');
  
  let tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Angel Falls');
  tableRow.setString('Height', '979 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tugela Falls');
  tableRow.setString('Height', '948 metres');
  
  tableRow = table.addRow();
  tableRow.setString('Waterfall', 'Tres Hermanas Falls');
  tableRow.setString('Height', '914 metres');
  
}
  
function getValueAt() {
  clear();
  let rowToGet = int(rowInp.value());
  let colToGet = int(colInp.value());
  
  if (rowToGet < table.getRowCount() &&
      colToGet < table.getColumnCount()) {
    text("The value at row " + rowToGet +
         " and column " + colToGet + " is:",
         20, 160);
  
    // Get the string at the given row and column
    text(table.getString(rowToGet, colToGet),
         20, 180);
  }
  else
    text("Please enter correct row and " +
         "column values", 20, 160);
  
  
  text("Click on the button to get the " +
       "specific string value of the table",
       20, 20);
  text("Enter row and column", 20, 60);
}

输出:

在线编辑器: https://editor.p5js.org/

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

参考: https://p5js.org/reference/#/p5.Table/getString