内部的HML表仅给出由完整数据插入的单个数据

时间:2019-01-11 12:51:52

标签: javascript java servlets

我在表单中有一个HTML表,对于我的每一列,我都给了一个基本上是input类型的名称,因此在服务器端获取数据时,它只给我一个数据

我的HTML表单(表格)的摘要*

var tableData = [{
    "Item Code": "C001",
    "Item Name": "Beverages",
    "Quantity": "0"

  },
  {
    "Item Code": "C003",
    "Item Name": "Juices",
    "Quantity": "0"
  },
  {
    "Item Code": "C004",
    "Item Name": "Soups",
    "Quantity": "0"

  },
  {
    "Item Code": "C005",
    "Item Name": "Cookies",
    "Quantity": "0"

  },

]

function addTable(tableValue) {
  var col = Object.keys(tableValue[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); // TABLE ROW.
  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th"); // TABLE HEADER.
    th.innerHTML = col[i];
    tr.appendChild(th);
    tr.classList.add("text-center");
    tr.classList.add("head")
  }
  for (var i = 0; i < tableValue.length; i++) {
    tr = table.insertRow(-1);
    for (var j = 0; j < col.length; j++) {

      let tabCell = tr.insertCell(-1);
      var hiddenField = document.createElement("input");
      hiddenField.style.display = "none";
      var tabledata = tableValue[i][col[j]];
      if (tabledata && !isNaN(tabledata)) {
        tabledata = parseInt(tabledata).toLocaleString('en-in')
      }
      if (tableData[i]['Item Code'] === tableData[i][col[j]]) {
        tabCell.innerHTML = tabledata;
        hiddenField.setAttribute('name', 'Item_Code');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableData[i]['Item Name'] === tableData[i][col[j]]) {

        tabCell.innerHTML = tabledata;

        hiddenField.setAttribute('name', 'Item_Name');
        hiddenField.setAttribute('value', tabledata);
        tabCell.appendChild(hiddenField);
      }
      if (tableData[i]['Quantity'] === tableData[i][col[j]]) {
        var quantityField = document.createElement("input");

        quantityField.style.border = "none";
        quantityField.style["text-align"] = "center";
        quantityField.setAttribute('name', 'Quantity');
        quantityField.setAttribute('value', tabledata);
        tabCell.appendChild(quantityField);
        /* console.log(quantityField) */

      }

      /* else {
        span = document.createElement("span");
        span.innerHTML = tabledata;
        tabCell.appendChild(span)
      } */
      if (j > 1)

        tabCell.classList.add("text-right");

    }
  }

  var divContainer = document.getElementById("HourlysalesSummary");
  divContainer.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");
}
addTable(tableData);
<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">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<form action="InsertQuantityIndent" method="post" id="form1">
  <div class="container" align="center">

    <div class="row">
      <div class="col-lg-12">
        <h6>OUTLET :</h6>
        <select id="myselect" name="outlet">
          <option>S001</option>
          <option>S002</option>
          <option>S003</option>
        </select>
      </div>
    </div>
    <div class="row">
      <div class="col-lg-12 table table-responsive" style="margin-bottom: 1px;">
        <table id="HourlysalesSummary"></table>
      </div>
    </div>
    <div>
      <button type="submit" class="btn btn-success" id="save">
					<i class="fas fa-save"></i> Save
				</button>
      <button type="button" class="btn btn-warning" id="clear">
					<i class="fas fa-eraser"></i> Clear
				</button>

    </div>
  </div>
</form>

当我将东西放进桌子并拿到服务器时,它只给我一个数据

我的servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      String outlet = request.getParameter("outlet");
      String itemCode = request.getParameter("Item_Code");
      String quantity = request.getParameter("Quantity");  
      System.out.println("outlet name :"+outlet);
      System.out.println("item code :"+itemCode);
      System.out.println("quantity"+quantity);
}

上面的代码只给我一个值

 outlet name :S001
item code :C001
quantity45

我做错了事还是需要做其他事?

1 个答案:

答案 0 :(得分:1)

您必须使用request.getParameterValues("outlet"):这将为您提供String数组。