12

这是我的问题。

我有这个 html 表:

<table class="flexme" id="table" border="1">
  <thead>
    <tr>
      <th width="100">Usuario</th>
      <th width="100">Nombre</th>
      <th width="100">Apellido</th>
      <th width="100">Cedula/Rif</th>
      <th width="140">Direccion</th>
      <th width="100">E-mail</th>
      <th width="100">Telefono1</th>
      <th width="100">Telefono2</th>
      <th width="100">Status</th>
      <th width="150">Acci&oacute;n</th>
    </tr>
  </thead>
  <tbody>
    <tr id="test">
      <td></td>
    </tr>
   </tbody>
</table>

我有这个ajax请求:

$.ajax({ 
   type    : "POST",
   url     : "service.php",
   dataType: "json",
   data: {
       action:"search",
       type: 'users',
       parameter: parameter,
       parameterContent: parameterContent,
   },           
   success:function(data) {
       $('#searchResults').show();
       var len = data.length;
       for (var i = 0; i< len; i++) {
       var username  = data[i].username;
       var name  = data[i].uname;
       var lastname  = data[i].lastname;

   }
})

用来自 JSON 的信息填充 html 表的正确方法是什么?我一直在尝试没有成功。我已经完成了测试append() html()但根本没有成功,有人可以指出我正确的方向吗?

我想要的是获取通过 JSON 提供的信息并使用此信息动态填充表。

4

4 回答 4

20

你可以试试这个:

    var table = $("#table tbody");
    $.each(data, function(idx, elem){
        table.append("<tr><td>"+elem.username+"</td><td>"+elem.name+"</td>   <td>"+elem.lastname+"</td></tr>");
    });

更多信息可以在这里找到:http: //api.jquery.com/jQuery.each/

于 2012-08-21T05:45:46.633 回答
3

尝试这个:

for (var i = 0; i < len; i++) {
   var username = data[i].username;
   var name  = data[i].name;
   var lastname = data[i].lastname;
   $('#table tbody').append('<tr><td>'+username+'</td><td>'+name+'</td><td>'+lastname+'</td></tr>')
}
于 2012-08-21T05:39:37.880 回答
1

谢谢,最后我这样做了:

  $.ajax({ 
                        type    : "POST",
                        url     : "service.php",
                        dataType: "json",
                        data: {
                        action:"search",
                        type: 'users',
                        parameter: parameter,
                        parameterContent: parameterContent,
                        },          
                            success:function(data) {

                                $('#searchResults').show();

                                    var len = data.length;
                                    for (var i = 0; i< len; i++) {

                                    var username  = data[i].username;
                                    var name  = data[i].name;
                                    var lastname  = data[i].lastname;
                                    var idnumber  = data[i].idnumber;
                                    var address  = data[i].address;
                                    var email  = data[i].email;
                                    var phone1 = data[i].phone1;
                                    var phone2 = data[i].phone2;
                                    var active = data[i].active;

                                    $("#generated").append("<tr><td>"+ username +"</td><td>"+ name +"</td><td>"+ lastname +"</td><td>"+ idnumber +"</td><td>"+ address +"</td><td>"+ email +"</td><td>"+ phone1 +"</td><td>"+ phone2 +"</td><td>"+ active +"</td></tr>");

                                }

                            }
于 2012-08-21T12:57:15.550 回答
1

JS

$(document).ready(function() {
  $.ajax({
    type: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*'
    },
    url: 'https://localhost:44387/api/employee/employeelist',
    dataType: "json",
    success: function(data) {
      var employeeTable = $('#tblEmployee tbody');
      employeeTable.empty();
      $('#tblEmployee').show();
      var len = data.length;
      for (var i = 0; i < len; i++) {
        var EmpId = data[i].EmpId;
        var FirstName = data[i].FirstName;
        var LastName = data[i].LastName;
        var Contact = data[i].Contact;
        var Address = data[i].Address;
        var Gender = data[i].Gender;
        var Salary = data[i].Salary;
        var Gender = data[i].Gender;
        var ActiveHours = data[i].ActiveHours;
        var Remark = data[i].Remark;
        var IsAvailable = data[i].IsAvailable;
        var Hiredate = data[i].Hiredate;
        var IsActive = data[i].IsActive;
        employeeTable.append('<tr><td>' + EmpId + '</td><td>' +
          FirstName + '</td><td>' + LastName + '</td><td>' + Contact +
          '</td><td>' + Address + '</td><td>' + Gender +
          '</td><td>' + Salary + '</td><td>' + ActiveHours +
          '</td><td>' + Remark + '</td><td>' + IsAvailable +
          '</td><td>' + Hiredate + '</td><td>' + IsActive +
          '</td><td><button type="button" class="btn btn-sm btn-primary editingTRbutton fas fa-pencil-alt noUnderlineCustom text-white" data-toggle="modal" data-target="#editModal">' + 'Edit' + '</button>' + '</td></tr>');
      }

    },
    error: function(err) {
      alert(err);
    }
  })
});

HTML

<form id="Employeeform">
  <div class="container">
    <table id="tblEmployee" class="paginated table table-bordered">
      <thead class="bg-primary text-white">
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Contact</th>
          <th>Address</th>
          <th>Gender</th>
          <th>Salary</th>
          <th>ActiveHours</th>
          <th>Remark</th>
          <th>IsAvailable</th>
          <th>Hiredate</th>
          <th>IsActive</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody id="tabldata"></tbody>
    </table>
  </div>
</form>
于 2020-08-11T10:51:22.560 回答