user6689472 user6689472 - 2 months ago 26
Ajax Question

How to append data to table using jquery

I've one HTML table:

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




Now I've to append all data to the table, I've tried like following:

$.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);
}


It's successfully bind data to the table.

But for more than one record it's get only last record.(override)

How can i bind all rows with table?

EDIT 1:

I used
.append()
but it gives me both record in one row.. how can i separate it in different row?

EDIT 2: Here is my handler:

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;
}
}


Edit 3: Screen
enter image description here

Answer

You can dynamically clone the first row present in tbody and then append it back in table with newer row data like this:

$(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>

Note: for simplicity I am just showing 2 columns. Please modify it according to your number of columns.