jquery - 如何使用 jquery 将数据追加到表中

标签 jquery asp.net ajax data-binding

我有一个 HTML 表格:

<table id="tableOrderDetail" class="table-striped table-bordered" style="align: center; width: 100%;">
<thead>
  <tr>
    <th width="5%">Sr. No.</th>
    <th width="25%">Product Name</th>
    <th width="12%" class="center">No Of Carton</th>
    <th width="12%" class="center">Quantity</th>
    <th width="12%" class="center">Original Price</th>
    <th width="12%" class="center">Order Price</th>
    <th width="10%" class="center">Discount</th>
    <th width="12%" class="center">Total Price</th>
  </tr>
</thead>
  <tbody>
    <tr>
    <td width="25%" class="ProductName"></td>
    <td width="12%" class="NoOfCarton"></td>
    <td width="12%" class="ProductQuantity"></td>
    <td width="12%" class="OriginalPrice"></td>
    <td width="12%" class="OrderPrice"></td>
    <td width="10%" class="Discount"></td>
    <td width="12%" class="TotalPrice"></td>
</tr>
</tbody> 

现在我必须将所有数据附加到表中,我已尝试如下:

            $.ajax({
                type: "POST",
                url: "../handlers/DisplaySpecificOrderDetail.ashx?OrderId=" + Orderid, //+ "tk=" + Date.toLocaleTimeString()
                data: "{ OrderId: " + Orderid + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: "true",
                cache: "false",
                success: function (data) {

                    $("#tableOrderDetail tbody").find("tr:gt(0)").remove();

                    $.each(data, function (i, v) {
                        if (i == 0) {
                            //setting the data in first row itself
                            setDataOnRow($("#tableOrderDetail tbody").find("tr").first(), v);
                        } else {
                            //clonning the first row and setting data over it and then appending in tbody
                            var clonnedRow = $($("#tableOrderDetail tbody").find("tr").first()).clone();
                            setDataOnRow(clonnedRow, v);
                            $("#tableOrderDetail tbody").append(clonnedRow);
                        }
                     });         
                   }); 

function setDataOnRow(rowObject, v) {
               // debugger
                $(rowObject).find(".ProductName").html(v.ProductName);
                $(rowObject).find(".NoOfCarton").html(v.NoOfCarton);
                $(rowObject).find(".ProductQuantity").html(v.ProductQuantity);
                $(rowObject).find(".OriginalPrice").html(v.OriginalPrice);
                $(rowObject).find(".OrderPrice").html(v.OrderPrice);
                $(rowObject).find(".Discount").html(v.Discount);
                $(rowObject).find(".TotalPrice").html(v.TotalPrice);
            }

已成功将数据绑定(bind)到表。

但是对于多个记录,它只能获取最后一条记录。(覆盖)
如何将所有行与表绑定(bind)?

编辑1:

我使用了.append(),但它给了我一行中的两条记录..我怎样才能将它分开在不同的行中?

编辑 2:这是我的处理程序:

public void ProcessRequest(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "application/octet-stream";

            long OrderId;
            Result res = new Result();    
            long.TryParse(context.Request.QueryString["OrderId"].ToString(), out OrderId);

            JavaScriptSerializer TheSerializer = new JavaScriptSerializer();

            SpecificOrderDetailManagement specificordMgr = new SpecificOrderDetailManagement();

                res = specificordMgr.GetOrderDetailOrderID(OrderId);
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(res.ListObject));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        } 

编辑 3:屏幕 enter image description here

最佳答案

您可以动态地克隆tbody中存在的第一行,然后使用更新的行数据将其附加回表中,如下所示:

$(document).ready(function() {
  var testData = [{
    "ProductName": "AAA",
    "NoOfCarton": 10
  }, {
    "ProductName": "BBB",
    "NoOfCarton": 20
  }];

  $("#tableOrderDetail tbody").find("tr:gt(0)").remove(); //remove all old rows except first one

  $.each(testData, function(i, v) {

    if (i == 0) {
      //setting the data in first row itself
      setDataOnRow($("#tableOrderDetail tbody").find("tr").first(), v);

    } else {

      //clonning the first row and setting data over it and then appending in tbody
      var clonnedRow = $($("#tableOrderDetail tbody").find("tr").first()).clone();
      setDataOnRow(clonnedRow, v);

      $("#tableOrderDetail tbody").append(clonnedRow);

    }
  });

});

//set the data in row
function setDataOnRow(rowObject, v) {
  var test = v.ProductName;
  var NoOfCarton = v.NoOfCarton;
  $(rowObject).find(".ProductName").html(test);
  $(rowObject).find(".NoOfCarton").html(NoOfCarton);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="tableOrderDetail" class="table-striped table-bordered" style="align: center; width: 100%;">
  <thead>
    <tr>
      <th width="25%">Product Name</th>
      <th width="12%" class="center">No Of Carton</th>
    </tr>
  </thead>
  <tbody>
    <tr class="">
      <td width="25%" class="ProductName">test</td>
      <td width="12%" class="NoOfCarton">test</td>
    </tr>
  </tbody>

注意:为了简单起见,我只显示 2 列。请根据您的列数进行修改。

关于jquery - 如何使用 jquery 将数据追加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39617396/

相关文章:

javascript - 使用 Javascript 添加的 HTML 表单隐藏字段未发布

Javascript - 在 "No connectivity detected” 时显示消息

javascript - 如何解决ajax数据编码中带有斜杠的查询字符串问题?

javascript - 使用 Ajax 或 Silverlight 调用 Web 服务?哪个表现最好?

javascript - 动态沙箱内联 JavaScript 的最佳方法是什么?

jquery - .change(function() { 更改选择菜单的选项后不提醒选项值

javascript - JQuery - 获取某些div的内容

asp.net - Visual Studio 2010 中的 HTML 格式化

c# - 我在哪里可以获得适用于 Windows 64 位的 memcached

asp.net - VS2010 自动重建缩小的 .js/.css 文件