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


p5.js TableRow getString()用法及代碼示例

p5.j​​s中p5.TableRow的getString()方法用於從表行的給定列中檢索字符串值。可以通過列ID或列名指定該列。

用法:

getString( column )

參數:該函數接受如上所述和以下描述的單個參數:

  • column:它是一個字符串或數字,表示列的名稱或列的ID。

返回值:此方法返回一個String,它是從表的給定列中檢索到的值。

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



範例1:

Javascript

function setup() { 
  createCanvas(600, 300); 
  textSize(18); 
  
  text("Click on the button to get all " +  
       "the 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(); 
  
  // Add two columns 
  table.addColumn("name"); 
  table.addColumn("id"); 
  
  // Add some rows to the table 
  let newRow = table.addRow(); 
  newRow.setString("name", "Mary"); 
  newRow.setString("id", "id-1"); 
  
  newRow = table.addRow(); 
  newRow.setString("name", "Nezuko"); 
  newRow.setString("id", "id-12"); 
  
  newRow = table.addRow(); 
  newRow.setString("name", "Ishigaki"); 
  newRow.setString("id", "id-121"); 
  
  newRow = table.addRow(); 
  newRow.setString("name", "Sam"); 
  newRow.setString("id", "id-23"); 
  
  newRow = table.addRow(); 
  newRow.setString("name", "Max"); 
  newRow.setString("id", "id-45"); 
  
  newRow = table.addRow(); 
  newRow.setString("name", "Mikasa"); 
  newRow.setString("id", "id-132"); 
} 
  
function showTable() { 
  clear(); 
  text("The values each tableRow is " + 
       "retrieved using the getString() method", 
     20, 20); 
  
  // Show all the columns present 
  for (let c = 0; c < table.getColumnCount(); c++) { 
    text(table.columns, 20 + 160 * c, 80); 
  } 
  
  // Show all the rows currently 
  // present in the table 
  for (let r = 0; r < table.getRowCount(); r++) { 
      
    // Get the table row from the table 
    let table_row = table.rows[r]; 
  
    // Getting the name column from 
    // the table row 
    text(table_row.getString("name"), 
      20, 120 + 20 * r); 
  
    // Getting the id column from 
    // the table row 
    text(table_row.getString("id"), 
      180, 120 + 20 * r); 
  } 
}

輸出:

範例2:

Javascript

function setup() { 
  createCanvas(500, 300); 
  textSize(18); 
  
  text("Click on the button to get " + 
       "that value in 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() { 
  
  // Add 5 columns and rows to the table 
  for (let i = 0; i < 5; i++) { 
    table.addColumn("Column " + i); 
    table.addRow(); 
  } 
  
  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, r + ":" + c); 
    } 
  } 
} 
  
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 table row from the table 
    let table_row = table.rows[rowToGet]; 
      
    // Print the column from the table row 
    text(table_row.getString(colToGet), 
         20, 180); 
  } 
  else
    text("Please enter correct row " +  
         "and column values", 20, 160); 
  
  text("Click on the button to get " +  
       "that value in 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.TableRow/getString

相關用法


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