Javascript 如何在jquery中对动态生成的表进行排序
我在一个网站上工作,当选择某个选项时,页面会在表格中显示数据。该页面通过将AJAX调用的json输出转换为html表输出来动态创建和显示表中的数据。单击后续的表标题时,我需要按照字母顺序/日期顺序/打开-关闭顺序对表中的各个列进行排序 我的代码是这样的:-Javascript 如何在jquery中对动态生成的表进行排序,javascript,jquery,html,Javascript,Jquery,Html,我在一个网站上工作,当选择某个选项时,页面会在表格中显示数据。该页面通过将AJAX调用的json输出转换为html表输出来动态创建和显示表中的数据。单击后续的表标题时,我需要按照字母顺序/日期顺序/打开-关闭顺序对表中的各个列进行排序 我的代码是这样的:- if (json.d[0].length > 0){ htmlTable += "<table class='table table-striped' id='myTable'>"+ "<tr>
if (json.d[0].length > 0){
htmlTable += "<table class='table table-striped' id='myTable'>"+
"<tr>"+
"<th style='background-color: #df820a;'>Customer</button></th>"+
"<th style='background-color: #df820a;'>Survey date</th>"+
"<th style='background-color: #df820a;'>Contact by</th>"+
"<th style='background-color: #df820a;'>Assigned to</th>"+
"<th style='background-color: #df820a;'>Status</th>"+
"<th style='background-color: #df820a;'>Resolution</th>"+
"<th style='background-color: #df820a;'>Coupon offered</th>"+
"</tr>";
for (var i = 0; i < json.d[0].length; i++) {
htmlTable += "<tr>"+
"<td>" + json.d[0][i].customerName1 + "</td>"+
"<td>" + json.d[0][i].createdDt1 + "</td>"+
"<td>" + json.d[0][i].customerContactBy1 + "</td>"+
"<td>" + json.d[0][i].AssignedTo1 + "</td>"+
"<td>" + json.d[0][i].alertStatus1 + "</td>"+
"<td>" + json.d[0][i].resolution1 + "</td>"+
"<td>" + json.d[0][i].couponOffered1 + "</td>"+;
"</tr>";
}
htmlTable += "</table>";
$('#AlertManagement').html(htmlTable);
}
if(json.d[0].length>0){
htmlTable+=“”+
""+
“客户”+
“调查日期”+
“联系人”+
“分配给”+
“地位”+
“决议”+
“提供优惠券”+
"";
对于(var i=0;i
有什么方法可以对这些动态生成的列进行排序吗 我使用Christian Bach的tablesorter插件。它非常适合ajax调用。就这么简单:
$.post('script.php', formData, function(data){
$("#myTable").html(data).tablesorter();
});
当然,我知道您使用的是json,因此与此略有不同,但我认为您看到了这一点
希望这有帮助 HTML意义上的表与编程意义上的DataTable不同,它所做的只是提供一个布局,您必须通过将数据存储在数组中并在每次“排序”后重新填充表来进行排序。这有助于解决您的问题吗?你还有什么问题吗?只是跟进,因为我看到没有选定的答案。:)