【发布时间】:2016-09-21 13:04:43
【问题描述】:
我有一个 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;
}
}
【问题讨论】:
-
使用 .append() insted of .html()
-
只有在实际修改 html 时才使用
.html(),否则使用text()。不过,克里斯是对的,你想在这里.append() -
我使用
.append(),但它给了我一行记录..我怎样才能将它分开在不同的行? -
@Chris,问题已更新
-
.append(ProductQuantity + "
");或使用任何其他 html 标签,例如: .append("" + ProductQuantity + "
");
标签: jquery asp.net ajax data-binding