gpt4 book ai didi

javascript - 如何使用 Jquery/Ajax 调用将数据显示为表格?

转载 作者:行者123 更新时间:2023-11-28 08:20:54 24 4
gpt4 key购买 nike

我使用 Ajax/Jquery 调用返回一个数据集给客户端。我想将这些数据显示为表格。但我不确定我该如何做到这一点?另外,我想在每一行中添加一个可点击的链接按钮或其他内容。

服务器端:

 [System.Web.Services.WebMethod]
public static string GetPopUpDataWM()
{
//return "Hello " + name + Environment.NewLine + "The Current Time is: "
// + DateTime.Now.ToString();

DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Rows.Add(25, "Indocin", "David");
table.Rows.Add(50, "Enebrel", "Sam");
table.Rows.Add(10, "Hydralazine", "Christoff");
table.Rows.Add(21, "Combivent", "Janet");
table.Rows.Add(100, "Dilantin", "Melanie");
return JsonConvert.SerializeObject(table);
}

客户端:

  <script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "test1.aspx/GetPopUpDataWM",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
Console.log(data.d[i].UserId);
$("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
});
</script>
<script type = "text/javascript">

</script>
<table id="tbDetails">
<tbody>
<tr>
<td>Code</td><td>Description</td>
</tr>
<tr>
<td>100</td><td>abc</td>
<td>101</td><td>mno</td>
<td>102</td><td>xyz</td>
</tr>
</tbody>
</table>

回应:

[{"Dosage":25,"Drug":"Indocin","Patient":"David"},
{"Dosage":50,"Drug":"Enebrel","Patient":"Sam"},
{"Dosage":10,"Drug":"Hydralazine","Patient":"Christoff"},
{"Dosage":21,"Drug":"Combivent","Patient":"Janet"},
{"Dosage":100,"Drug":"Dilantin","Patient":"Melanie"}]

最佳答案

尝试var jObject = $("#tbDetails").find('<tbody>').append(...
jObject.click(yourFunction);

但是您不能使用 data.d[i].UserId 但是:

$.each(data, function( index, value ) {
echo value['Dosage']
});

还有一件事:您可以使用 data.length 测试长度而不是 data.d.length :)

关于javascript - 如何使用 Jquery/Ajax 调用将数据显示为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987507/

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