gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:06:25 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com