如何使用jquery将数据附加到表

时间:2016-09-21 13:04:43

标签: 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);
            }

它成功地将数据绑定到表格。

但是对于不止一个记录,它只能获得最后一个记录。(覆盖)
如何用表绑定所有行?

编辑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

2 个答案:

答案 0 :(得分:3)

您可以动态clone tbody中的第一行,然后append将其更新回到表中,并使用较新的行数据,如下所示:

$(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列。请根据您的列数进行修改。

答案 1 :(得分:1)

试试这个 -

在您的代码中,您没有创建任何<tr><td>元素,而只是替换现有元素的html内容。

tbody之后为ajax收到的每组数据附加表格行。

$.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) {
        $.each(data, function(i, v) {
            $('#tableOrderDetail').find('tbody')
                .append('<tr>').append('<td width="25%" class="ProductName">' + v.ProductName + '</td>')
                .append('<td width="25%" class="NoOfCarton">' + v.NoOfCarton + '</td>')
                .append('<td width="25%" class="ProductQuantity">' + v.ProductQuantity + '</td>')
                .append('<td width="25%" class="OriginalPrice">' + v.OriginalPrice + '</td>')
                .append('<td width="25%" class="OrderPrice">' + v.OrderPrice + '</td>')
                .append('<td width="25%" class="Discount">' + v.Discount + '</td>')
                .append('<td width="25%" class="TotalPrice">' + v.TotalPrice + '</td>')
                .append('<tr>');
        });
    }
});