jquery实现table按时间排序
$(document).ready(function(){
sortTable();
});
var sortTable = function(){
$("#tableList").find("#cell_startDate").bind("click",function(){
var sortType = changSign("#tableList","#cell_startDate");
var sortByStartDateFun = sortByType(sort,".list_startDate label");
$("#tableList tbody").html($("#tableList tbody tr").sort(sortByStartDateFun));
});
}
var changSign = function(tableSign,rowSign)
{
var idtext = $(tableSign).find(rowSign).text();
var sortType = "asc";
if(idtext.indexOf("↑") > 0)
{
$(tableSign).find(rowSign).text(function(i,origText){
return origText.substring(0,4) + "↓";
});
sortType = "asc";
}else{
$(tableSign).find(rowSign).text(function(i,origText){
return origText.substring(0,4) + "↑";
});
sortType = "desc";
}
return sortType;
}
var sortType = function(sortType,list_sort){
return function sortByDate(a,b){
var aVal = $(a).find(list_sort).text();
var bVal = $(b).find(list_sort).text();
if("" == aVal)
{
return -1;
}
if("" == bVal)
{
return 1;
}
var aDateTemp = aVal.split(" ");
var bDateTemp = bVal.split(" ");
var arrStartDate = aDateTemp[0].split("-");
var arrEndDate = bDateTemp[0].split("-");
var arrStartTime = aDateTemp[1].split(":");
var arrEndTime = bDateTemp[1].split(":");
var allStartDate = new Date(arrStartDate[0],arrStartDate[1],arrStartTime[0],arrStartTime[1],arrStartTime[2]);
var allEndDate = new Date(arrEndDate[0],arrEndDate[1],arrEndTime[0],arrEndTime[1],arrEndTime[2]);
if(sortType == "asc")
{
if(allStartDate >= allEndDate)
{
return 1;
}else{
return -1;
}
}else{
if(allStartDate > allEndDate)
{
return -1;
}else{
return 1;
}
}
}
}