HTML表格无法正确呈现

时间:2019-04-02 06:20:50

标签: javascript jquery html html-table

我有一个带JSON数据的HTML表,我正在做的是创建一列作为输入字段,我的表头是 @Override public void onBindViewHolder(ViewHolder holder, int position) { Log.d(TAG, "onBindViewHolder called"); ContentItem item = mContentItems.get(position); if(item.getName()!=null){ holder.textName.setVisibility(View.Visible); holder.textName.setText(item.getName()); }else{ holder.textName.setVisibility(View.GONE); } if(item.getPreviewImageDefault()!=null){ holder.imageIcon.setVisibility(View.Visible) Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon); }else{ holder.imageIcon.setVisibility(View.GONE) } } CodeItem Name,{{1 }}还有Unitcode,其中我将“仅接受的数量”输入字段,但“数量”字段也转换为“输入字段”,我不知道自己在做什么错

Quantity
AcceptedQty

为什么我不知道数量字段也显示为输入字段

因为我的代码有点长,因为我要添加HTML表单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),因为我想将所有数据都发送到后端,所以我在ajax调用时对表单进行了序列化

我已对我正在做的所有行进行了评论,以便所有人都更容易理解

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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)
<table id="table"></table>