代码之家  ›  专栏  ›  技术社区  ›  hannacreed

JavaScript保存用户添加的表行

  •  1
  • hannacreed  · 技术社区  · 8 年前

    我正在制作一个有表格的电子应用程序,用户可以添加表格并更改字段的内容,但我希望保存输入,并且只有用户在其中输入了任何内容。

    我该怎么做?我需要一个数据库吗?可以用饼干做吗?我试图学习如何使用cookies,但没有找到如何保存添加的元素以及内容。

    function appendRow(id) {
      var table = document.getElementById(id); // table reference
      length = table.length,
      row = table.insertRow(table.rows.length);      // append table row
      var i;
      // insert table cells to the new row
      for (i = 0; i < table.rows[0].cells.length; i++) {
          createCell(row.insertCell(i), i, 'row');
      }
    }
    
    function createCell(cell, text, style) {
      var div = document.createElement('div'), // create DIV element
          txt = document.createTextNode('_'); // create text node
      div.appendChild(txt);               // append text node to the DIV
      div.setAttribute('id', style);        // set DIV class attribute
      div.setAttribute('idName', style);    // set DIV class attribute for IE (?!)
      cell.appendChild(div);                   // append DIV to the table cell
    }
    table {
      text-align: center;
      width: 400px;
    }
    tr:nth-child(even) {
      background-color: #ccc;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <title>Test</title>
      <link rel="stylesheet" href="test.css">
    </head>
    
    <body>
      <button id="addCust" class="addSort" onclick="appendRow('custList')">add customer</button>
      <table id="custListTop" contenteditable="false" style="background-color: #ccc;">
        <tr>
          <td style="border-top-left-radius: 5px;">Customers</td>
          <td style="border-top-right-radius: 5px;">Main Location</td>
        </tr>
      </table>
      <table id="custList" contenteditable="true">
        <tr>
          <td>Someone</td>
          <td>something</td>
        </tr>
      </table>
    <script src="test.js"></script>
    
    </body>
    
    </html>
    1 回复  |  直到 8 年前
        1
  •  2
  •   SourceOverflow    8 年前

    那么,你将如何挽救它取决于你想做什么。如果您希望它在程序退出后继续存在,则需要将其保存在磁盘或服务器上。不过,您可能只想将其保存到文件中。(JSON是最明显的选择)。否则,只需将其保存到js变量中。

    为了获取数据,我要么使用一个读取单元格文本的保存按钮,要么使用数据绑定。后者对于电子应用程序非常有用。您可以使用框架(如 vue.js , react.js 或更多)或 DIY .

    前者可能更容易,您需要一个按钮将其保存到磁盘上。点击按钮,你就可以浏览所有 <tr>

    function save(id) {
      var table = document.getElementById(id);
      var trs = table.getElementsByTagName('tr');  // list of all rows
      
      var values = [];  // will be a (potentially jagged) 2D array of all values
      for (var i = 0; i < trs.length; i++) {
        // loop through all rows, each will be one entrie in values
        var trValues = [];
        var tds = trs[i].getElementsByTagName('td');  // list of all cells in this row
        
        for (var j = 0; j < tds.length; j++) {
          trValues[j] = tds[j].innerText;
          // get the value of the cell (preserve newlines, if you don't want that use .textContent)
        }
        
        values[i] = trValues;
      }
      // save values
      console.log(values);
    }
    
    function appendRow(id) {
      var table = document.getElementById(id); // table reference
      length = table.length,
      row = table.insertRow(table.rows.length);      // append table row
      var i;
      // insert table cells to the new row
      for (i = 0; i < table.rows[0].cells.length; i++) {
          createCell(row.insertCell(i), i, 'row');
      }
    }
    
    function createCell(cell, text, style) {
      var div = document.createElement('div'), // create DIV element
          txt = document.createTextNode('_'); // create text node
      div.appendChild(txt);               // append text node to the DIV
      div.setAttribute('id', style);        // set DIV class attribute
      div.setAttribute('idName', style);    // set DIV class attribute for IE (?!)
      cell.appendChild(div);                   // append DIV to the table cell
    }
    table {
      text-align: center;
      width: 400px;
    }
    tr:nth-child(even) {
      background-color: #ccc;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <title>Test</title>
      <link rel="stylesheet" href="test.css">
    </head>
    
    <body>
      <button id="addCust" class="addSort" onclick="appendRow('custList')">add customer</button>
      <button id="save" class="save" onclick="save('custList')">save</button>
      <table id="custListTop" contenteditable="false" style="background-color: #ccc;">
        <tr>
          <td style="border-top-left-radius: 5px;">Customers</td>
          <td style="border-top-right-radius: 5px;">Main Location</td>
        </tr>
      </table>
      <table id="custList" contenteditable="true">
        <tr>
          <td>Someone</td>
          <td>something</td>
        </tr>
      </table>
    <script src="test.js"></script>
    
    </body>
    
    </html>