【问题标题】:HTML table not rendering correctlyHTML 表格未正确呈现
【发布时间】:2019-04-02 16:45:28
【问题描述】:

我有一个带有 JSON 数据的 HTML 表,我正在做的是创建一列作为输入字段,我的表头是 Code,Item Name,Unitcode,QuantityAcceptedQty 其中我正在制作仅接受数量的输入字段,但数量字段也被转换为输入字段我不知道我做错了什么

var tableDataDraft = [{
    "Code": "1326",
    "Item Name": "PINEAPPLE KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "3.00"
  },
  {
    "Code": "1494",
    "Item Name": "2D CAKE CHARGES PER KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "3.00"
  }
]


function addTableDraft(tableDataDraft) {
  var col = Object.keys(tableDataDraft[0]);
  var countNum = col.filter(i => !isNaN(i)).length;
  var num = col.splice(0, countNum);
  col = col.concat(num);
  var table = document.createElement("table");
  var tr = table.insertRow(-1);
  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th");
    th.innerHTML = col[i];
    tr.appendChild(th);
    tr.classList.add("text-center");
    tr.classList.add("head")
  }
  for (var i = 0; i < tableDataDraft.length; i++) {
    tr = table.insertRow(-1);
    for (var j = 0; j < col.length; j++) {
      let tabCell = tr.insertCell(-1);
      var hiddenField = document.createElement("input"); //creating input field hidden
      hiddenField.style.display = "none";
      var tabledata = tableDataDraft[i][col[j]];
      if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) {
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Name');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['Unitcode'] === tableDataDraft[i][col[j]]) {
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Unit_code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['Quantity'] === tableDataDraft[i][col[j]]) { //this quantity field i don't want to be input field

        hiddenField.setAttribute('name', 'Quantity');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['AcceptedQty'] === tableDataDraft[i][col[j]]) { //this one i want to be a input field

        var quantityField = document.createElement("input");
        quantityField.style.border = "none";
        quantityField.style["text-align"] = "right";
        quantityField.setAttribute("name", "AcceptedQty");
        quantityField.setAttribute("autocomplete", "on");
        quantityField.setAttribute("value", tabledata);
        quantityField.setAttribute("type", "tel");
        quantityField.setAttribute("required", "required");
        quantityField.classList.add("dataReset");
        quantityField.toLocaleString('en-IN');
        tabCell.appendChild(quantityField);
      }

      if (j > 1)
        tabCell.classList.add("text-right");
    }
  }
  var divContainer = document.getElementById("table");
  divContainer.innerHTML = "";
  divContainer.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");



}

addTableDraft(tableDataDraft)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div class="table-responsive" id="commonDvScroll">
  <table id=table></table>
</div>

为什么 Quantity 字段也显示为我不知道的输入字段

因为我的代码有点长,因为我要添加 HTML 表单 Attributes 因为我希望所有数据都到我的后端,所以我在 ajax 调用时序列化我的表单

我已经评论了我正在做什么的所有行,以便大家更容易理解

【问题讨论】:

  • '为什么 Quantity 字段也显示为我不知道的输入字段',这是因为var quantityField = document.createElement("input");
  • @designtocode 我创建的一个将AcceptedQty 显示为输入字段,这里我说的是Quantity 输入字段
  • @dheerajkumar 因为QuantityAcceptedQty的值是一样的
  • @CarstenLøvboAndersen 我也这么认为,但问题是总是一样的,一个是可编辑的,另一个是不可食用的
  • @dheerajkumar 这个部分的问题是tableDataDraft[i]['AcceptedQty'] === tableDataDraft[i][col[j]]。尝试将 AcceptedQty 的值更改为 4 即可。还为Quantity if 块添加table.innerHTML = tabledata

标签: javascript jquery html html-table


【解决方案1】:

问题是因为在每行插入值时,您比较的是列值,而不是列名。当值相同时,这将导致问题,例如QuantityAcceptedQty 具有相同的值3.0。尝试将其中一个更改为4.0,您会发现它有效。

这是您的代码的简化版本,它检查当前列是否为AcceptedQty,并仅显示一个输入字段。您仍然可以拥有其他 if 块,但请确保条件类似于 if (col[j] === 'Code')(col[j] === 'Quantity') 等。

var tableDataDraft = [{
    "Code": "1326",
    "Item Name": "PINEAPPLE KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "3.00"
  },
  {
    "Code": "1494",
    "Item Name": "2D CAKE CHARGES PER KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "3.00"
  }
]


function addTableDraft(tableDataDraft) {
  var col = Object.keys(tableDataDraft[0]);
  var countNum = col.filter(i => !isNaN(i)).length;
  var num = col.splice(0, countNum);
  col = col.concat(num);
  var table = document.createElement("table");
  var tr = table.insertRow(-1);
  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th");
    th.innerHTML = col[i];
    tr.appendChild(th);
    tr.classList.add("text-center");
    tr.classList.add("head")
  }
  for (var i = 0; i < tableDataDraft.length; i++) {
    tr = table.insertRow(-1);
    for (var j = 0; j < col.length; j++) {
      let tabCell = tr.insertCell(-1);
      var hiddenField = document.createElement("input"); //creating input field hidden
      hiddenField.style.display = "none";
      var tabledata = tableDataDraft[i][col[j]];
      if (col[j] === 'AcceptedQty') { 
      //this one i want to be a input field
        
        var quantityField = document.createElement("input");
        quantityField.style.border = "none";
        quantityField.style["text-align"] = "right";
        quantityField.setAttribute("name", "AcceptedQty");
        quantityField.setAttribute("autocomplete", "on");
        quantityField.setAttribute("value", tabledata);
        quantityField.setAttribute("type", "tel");
        quantityField.setAttribute("required", "required");
        quantityField.classList.add("dataReset");
        quantityField.toLocaleString('en-IN');
        tabCell.appendChild(quantityField);
      }
      else
       { //now setting html attributes
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      
      if (j > 1)
        tabCell.classList.add("text-right");
    }
  }
  var divContainer = document.getElementById("table");
  divContainer.innerHTML = "";
  divContainer.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");



}

addTableDraft(tableDataDraft)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div class="table-responsive" id="commonDvScroll">
  <table id=table></table>
</div>

【讨论】:

【解决方案2】:

更改检查返回数据的方式。不要检查值是否相同,而是像这样检查数据键

if (col[j] === 'Quantity')

然后您可以为该特定列或数据键插入数据。

var tableDataDraft = [{
    "Code": "1326",
    "Item Name": "PINEAPPLE KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "7.00"
  },
  {
    "Code": "1494",
    "Item Name": "2D CAKE CHARGES PER KG",
    "Unitcode": "NOS",
    "Quantity": "3.00",
    "AcceptedQty": "10.00"
  }
]


function addTableDraft(tableDataDraft) {
  var col = Object.keys(tableDataDraft[0]);
  var countNum = col.filter(i => !isNaN(i)).length;
  var num = col.splice(0, countNum);
  col = col.concat(num);
  var table = document.createElement("table");
  var tr = table.insertRow(-1);
  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th");
    th.innerHTML = col[i];
    tr.appendChild(th);
    tr.classList.add("text-center");
    tr.classList.add("head")
  }
  for (var i = 0; i < tableDataDraft.length; i++) {
    tr = table.insertRow(-1);
    for (var j = 0; j < col.length; j++) {
      let tabCell = tr.insertCell(-1);
      var hiddenField = document.createElement("input"); //creating input field hidden
      hiddenField.style.display = "none";
      var tabledata = tableDataDraft[i][col[j]];
      if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) {
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Name');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableDataDraft[i]['Unitcode'] === tableDataDraft[i][col[j]]) {
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Unit_code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (col[j] === 'Quantity') { //this quantity field i don't want to be input field
        var quantityNoEdit = document.createElement("input");
        quantityNoEdit.style.border = "none";
        quantityNoEdit.style["text-align"] = "right";
        quantityNoEdit.setAttribute("name", "Quantity");
        quantityNoEdit.setAttribute("autocomplete", "on");
        quantityNoEdit.setAttribute("value", tabledata);
        quantityNoEdit.setAttribute("type", "tel");
        quantityNoEdit.setAttribute("required", "required");
        quantityNoEdit.classList.add("dataReset");
        quantityNoEdit.toLocaleString('en-IN');
        quantityNoEdit.disabled = true;
        tabCell.appendChild(quantityNoEdit);
        hiddenField.setAttribute('name', 'Quantity');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
        console.log('Quantity:' + tableDataDraft[i][col[j]]);
      }
      if (tableDataDraft[i]['AcceptedQty'] === tableDataDraft[i][col[j]]) { //this one i want to be a input field

        var quantityField = document.createElement("input");
        quantityField.style.border = "none";
        quantityField.style["text-align"] = "right";
        quantityField.setAttribute("name", "AcceptedQty");
        quantityField.setAttribute("autocomplete", "on");
        quantityField.setAttribute("value", tabledata);
        quantityField.setAttribute("type", "tel");
        quantityField.setAttribute("required", "required");
        quantityField.classList.add("dataReset");
        quantityField.toLocaleString('en-IN');
        tabCell.appendChild(quantityField);
        console.log('AcceptedQty:' + col[j]);
      }

      if (j > 1)
        tabCell.classList.add("text-right");
    }
  }
  var divContainer = document.getElementById("table");
  divContainer.innerHTML = "";
  divContainer.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");



}

addTableDraft(tableDataDraft)
&lt;table id="table"&gt;&lt;/table&gt;

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多