Javascript 在ajax中按日期对表列排序
我有一个使用ajax在按钮点击时显示的表。下面是一段代码片段:Javascript 在ajax中按日期对表列排序,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个使用ajax在按钮点击时显示的表。下面是一段代码片段: myBtn.on("click", function() { displayTable(); }); function displayTable(){ $.ajax({ url:'url to a function in controller', type: "GET", //data: {val : val}, dataType: 'json',
myBtn.on("click", function() {
displayTable();
});
function displayTable(){
$.ajax({
url:'url to a function in controller',
type: "GET",
//data: {val : val},
dataType: 'json',
success: function(data){
// some codes here
$.each(data.documents, function(key, value){
$("#myTable")
.append(
"<tr class='" + rowClass + "'><td class='text-center'>" +
value.title +
"</td><td class='text-center'>" +
value.time1.replace(/-/g, "/") +
"</td><td class='text-center'>" +
value.time2.replace(/-/g, "/") +
"</td></tr>"
);
});
}
});
}
你知道怎么做吗?如何按日期排序(
value.time2
)?最好的方法是请求服务器为您排序值。但是,如果需要在客户端执行此操作,只需在将数据添加到页面之前对数据文档进行排序即可。例如:
data.documents = data.documents.map(function(item) {
// Fix display
item.time1 = item.time1.replace(/-/g, "/");
item.time2 = item.time2.replace(/-/g, "/");
return item;
});
data.documents.sort(function(a, b) {
// Custom sorting function
return new Date(a.time2) > new Date(b.time2);
});
$.each(data.documents, function(key, value){
$("#myTable")
.append(
"<tr class='" + rowClass + "'><td class='text-center'>" +
value.title +
"</td><td class='text-center'>" +
value.time1 +
"</td><td class='text-center'>" +
value.time2 +
"</td></tr>"
);
});
data.documents=data.documents.map(函数(项){
//固定显示
item.time1=item.time1.替换(/-/g,“/”);
item.time2=item.time2.替换(/-/g,“/”);
退货项目;
});
数据.文件.排序(函数(a,b){
//自定义排序功能
返回新日期(a.time2)>新日期(b.time2);
});
$.each(data.documents,function(key,value){
$(“我的表格”)
.附加(
"" +
价值、头衔+
"" +
值。时间1+
"" +
值。时间2+
""
);
});
最好的方法是请求服务器为您排序值。但是,如果需要在客户端执行此操作,只需在将数据添加到页面之前对数据文档进行排序即可。例如:
data.documents = data.documents.map(function(item) {
// Fix display
item.time1 = item.time1.replace(/-/g, "/");
item.time2 = item.time2.replace(/-/g, "/");
return item;
});
data.documents.sort(function(a, b) {
// Custom sorting function
return new Date(a.time2) > new Date(b.time2);
});
$.each(data.documents, function(key, value){
$("#myTable")
.append(
"<tr class='" + rowClass + "'><td class='text-center'>" +
value.title +
"</td><td class='text-center'>" +
value.time1 +
"</td><td class='text-center'>" +
value.time2 +
"</td></tr>"
);
});
data.documents=data.documents.map(函数(项){
//固定显示
item.time1=item.time1.替换(/-/g,“/”);
item.time2=item.time2.替换(/-/g,“/”);
退货项目;
});
数据.文件.排序(函数(a,b){
//自定义排序功能
返回新日期(a.time2)>新日期(b.time2);
});
$.each(data.documents,function(key,value){
$(“我的表格”)
.附加(
"" +
价值、头衔+
"" +
值。时间1+
"" +
值。时间2+
""
);
});
谢谢。我要试试这个。我会再打给你的。埃瑟琳·拉维恩,请看最新消息。我添加了缺少的返回项
tomap
call.@EtheleneLaverne另外,您可以展示一些从服务器上获得的JSON示例吗?谢谢。我要试试这个。我会再打给你的。埃瑟琳·拉维恩,请看最新消息。我添加了缺少的返回项
tomap
call.@EtheleneLaverne另外,您能展示一些从服务器上获得的JSON示例吗?