使用Ajax将动态数据填充到下拉列中的问题

时间:2019-02-20 07:10:09

标签: javascript jquery html ajax datatable

我正在尝试使用jquery和ajax动态填充并追加一列例如:Id与以下列数据。

数据将通过其余Web服务填充。但是没有填充数据。

代码段如下。

由于缺少Web服务的动态数据,因此粘贴的代码可能无法正常工作。

因此,问题在于将数据填充/附加到标题的列中。

$(document).ready(function() {

  // DO GET
  $.ajax({
    type: "GET",
    url: "api/customer/all",
    success: function(result) {
      $.each(result, function(i, customer) {

        var customerRow = "<tr><td><input type='checkbox' name='record'></td><td>" +
          customer.id + "</td><td>" +
          customer.name.toUpperCase() + "</td><td>" +
          customer.age + "</td><td>" +
          customer.address.street + "</td><td>" +
          customer.address.postcode + "</td></tr>";

        $('#IdProof').append('<option value="' + customer.id + '">' + customer.age + '</option>');

        $('#customerTable tbody').append(customerRow);
      });

    },
    error: function(e) {
      alert("ERROR: ", e);
      console.log("ERROR: ", e);
    }
  });

  $('#select-all').click(function(event) {
    if (this.checked) {
      // Iterate each checkbox
      $(':checkbox').each(function() {
        this.checked = true;
      });
    } else {
      $(':checkbox').each(function() {
        this.checked = false;
      });
    }
  });

  $('#reboot').click(function() {
    $('#customerTable').find('tr').each(function(i) {
      var row = $(this);
      if (row.find('input[type="checkbox"]').is(':checked')) {
        var $tds = $(this).find('td'),
          Id = $tds.eq(1).text(),
          name = $tds.eq(2).text(),
          age = $tds.eq(3).text();
        // do something with productId, product, Quantity
        alert('Row ' + (i + 1) + ':\nId: ' + Id +
          '\nname: ' + name +
          '\nage: ' + age);

      }
    });
  });

  $(function() {
    $("#inputFilter").on("keyup", function() {
      $("#select-all").hide();
      var value = $(this).val().toLowerCase();
      $("#customerTable > tbody > tr").filter(function() {
        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)

      });
    });
  });
})
<!DOCTYPE HTML>

<html>

<head>
  <title>Spring Boot - DELETE-UPDATE AJAX Example</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="/js/jqueryScript.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="@{/css/main.css}" />
</head>

<body>
  <div class="container">
    <h2>Filter Table</h2>
    <div class="row col-md-7 ">
      <div style="margin-bottom: 20px; padding: 10px; background-color: green; color: white;">
        <p>
          Type some text to search the table for <strong>Id</strong>, <strong>Name</strong>,
          <strong>Age</strong>, <strong>Street</strong>, <strong>PostCode</strong>:
        </p>
        <input class="form-control" id="inputFilter" type="text" placeholder="Search.." />
      </div>
      <table id="customerTable" class="table table-bordered table-hover table-responsive ">
        <thead>
          <tr>
            <th><input type="checkbox" name="select-all" id="select-all" /></th>
            <th>
              <select name="IdProof" id="id" class="form-control">
                <option value="">Id</option>
              </select>
            </th>
            <th>
              <select name="Name" id="name" class="form-control">
                <option value="">Name</option>
              </select>
            </th>
            <th>
              <select name="Age" id="age" class="form-control">
                <option value="">Age</option>
              </select>
            </th>
            <th>
              <select name="Street" id="street" class="form-control">
                <option value="">Street</option>
              </select>
            </th>
            <th>
              <select name="Postcode" id="postcode" class="form-control">
                <option value="">Postcode</option>
              </select>
            </th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
      <button id="reboot">Reboot</button>
      <button id="logs">Logs</button>
    </div>
  </div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

尝试像这样修复您的代码。我认为问题出在您的掩饰中。像这样进行所有串联

var customerRow = "<tr><td><input type='checkbox' name='record'></td><td>" +
  customer.id + "'</td><td>'" +
  customer.name.toUpperCase() + "'</td><td>'" +
  customer.age + "'</td><td>'" +
  customer.address.street + "'</td><td>'" +
  customer.address.postcode + "'</td></tr>'";