问题描述
我有一个HTML表格:
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?
编辑1:
我使用 .append()
但它给出了我都记录在一行..我怎么能把它分成不同的行?
I used .append()
but it gives me both record in one row.. how can i separate it in different row?
编辑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:屏幕
Edit 3: Screen
推荐答案
您可以动态克隆
tbody
中的第一行,然后将
追加到表中,其中包含较新的行数据,如下所示:
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>
注意:为简单起见,我只显示了2列。请根据您的列数进行修改。
Note: for simplicity I am just showing 2 columns. Please modify it according to your number of columns.
这篇关于如何使用jquery将数据附加到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!