I've one HTML table:
我有一个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>
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?
我使用了.append()但是它给了我一行记录..我怎么能把它分成不同的行?
EDIT 2: Here is my handler:
编辑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:屏幕
2 个解决方案
#1
3
You can dynamically clone
the first row present in tbody
and then append
it back in table with newer row data like this:
您可以动态克隆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>
Note: for simplicity I am just showing 2 columns. Please modify it according to your number of columns.
注意:为简单起见,我只显示了2列。请根据您的列数进行修改。
#2
1
Try this -
尝试这个 -
in your code your are not creating any <tr>
or <td>
element you are just replacing the existing element's html content.
在您的代码中,您不会创建任何或元素,而只是替换现有元素的html内容。
Append the table row after tbody
for every set of data received by ajax.
在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>');
});
}
});
#1
3
You can dynamically clone
the first row present in tbody
and then append
it back in table with newer row data like this:
您可以动态克隆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>
Note: for simplicity I am just showing 2 columns. Please modify it according to your number of columns.
注意:为简单起见,我只显示了2列。请根据您的列数进行修改。
#2
1
Try this -
尝试这个 -
in your code your are not creating any <tr>
or <td>
element you are just replacing the existing element's html content.
在您的代码中,您不会创建任何或元素,而只是替换现有元素的html内容。
Append the table row after tbody
for every set of data received by ajax.
在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>');
});
}
});