Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

javascript - Save a dynamic HTML table in JSON

In my code below user choose a color and types customer name and adds the data to a dynamic table. My problem is that I want to save my table in JSON but my code saves only the first row of the table and it doesn't adds rest of the rows

function colorddRow() {
  var colorpartsrows = "";
  var colparts = $("#color-part-list").val();
  var customer = $("#customer-name").val();

  var colpartssum = $("#color-part-list :selected").length;

  colorpartsrows +=
    "<tr><td>" +
    colparts +
    "</td><td>" +
    customer +
    "</td><td>" +
    colpartssum +
    "</td></tr>";
  $(colorpartsrows).appendTo("#dataTable1-color-parts tbody");
}

function showValues() {
  var fields = $(":input").serializeArray();
  $("#results").empty();
  jQuery.each(fields, function(i, field) {
    $("#results").append(field.value + " ");
  });
}
$("select").change(showValues);
showValues();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th name="color[]" id="color">Color</th>
    <th name="Customer[]" id="customer">Color</th>
  </tr>
  <tr>
    <td id="color-des">
      <select id="color-part-list" data-placeholder="Rejected Parts">
        <option value="Red">Red</option>
        <option value="Grey">Grey</option>
        <option value="Blue">Blue</option>
        <option value="Yellow">Yellow</option>
        <option value="White">White</option>
        <option value="Black">Black</option>
      </select>
    </td>
    <td id="customer">
      <input id="customer-name" type="text" name="customer">
    </td>
  </tr>
</table>

<input type="button" value="Add The Color" onclick="colorddRow()" />

<table id="dataTable1-color-parts">
 <tr>
    <th>Color</th>
    <th>Customer Name</th>
</tr>  
</table>
<button type="button" onclick="showValues()">Save to JSON</button>
question from:https://stackoverflow.com/questions/65896460/save-a-dynamic-html-table-in-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have done that...

const   // ------------------------------------------------------- ↓↓↓↓↓ added!
    result_Table = document.querySelector('#dataTable1-color-parts tbody')
  , select_Color = document.querySelector('#color-part-list')
  , custom_Color = document.querySelector('#customer-name')
  ;
function colorddRow()
  {
  let newRow = result_Table.insertRow()
  newRow.insertCell().textContent = select_Color.value
  newRow.insertCell().textContent = custom_Color.value
  newRow.insertCell().textContent = "1"
  }
function showValues()
  {
  let jsonTable = []
  for (let r=0; r<result_Table.rows.length; ++r)
    {
    let colparts = result_Table.rows[r].cells[0].textContent
      , customer = result_Table.rows[r].cells[1].textContent
      , sum      = result_Table.rows[r].cells[2].textContent
      ;
    jsonTable.push( {colparts,customer,sum} )
    }
  console.clear()
  console.log( jsonTable )
  }
#dataTable1-color-parts  {
  border-collapse : collapse;
  margin          : 2em 1em;
  }
#dataTable1-color-parts th,
#dataTable1-color-parts td {
  padding    : .2em .8em;
  border     : 1px solid darkblue;
  cursor     : pointer;
  }
#dataTable1-color-parts thead {
  background-color:  #c3e3ee;
  }
  
.as-console-wrapper { max-height:100% !important; top:0; left:50% !important; width:50%; }
.as-console-row         { background-color: yellow; }
.as-console-row::after  { display:none !important; }
.as-console-row::before { content: 'JSON'; font-size: .8em; }
<table>
  <tr>
    <th name="color[]" id="color">Color</th>
    <th name="Customer[]" id="customer">Color</th>
  </tr>
  <tr>
    <td id="color-des">
      <select id="color-part-list">
        <option value="Red">Red</option>
        <option value="Grey">Grey</option>
        <option value="Blue">Blue</option>
        <option value="Yellow">Yellow</option>
        <option value="White">White</option>
        <option value="Black">Black</option>
      </select>
    </td>
    <td id="customer">
      <input id="customer-name" type="text" name="customer">
    </td>
  </tr>
</table>

<input type="button" value="Add The Color" onclick="colorddRow()" />

<table id="dataTable1-color-parts">
  <thead>
    <tr><th>colparts</th><th>customer</th><th>sum</th></tr>
  </thead>
  <tbody></tbody>
</table>

<button type="button" onclick="showValues()">Save to JSON</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...