Last active
April 22, 2016 15:44
-
-
Save mangeshpawar/9435c4c8c3f00fbdf64753ec177df096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ts = ts || {}; | |
var sortStatus = {}; | |
ts.getValue = function(tdElem){ | |
var inputElemLst = $(tdElem).find("input:not([type='hidden']):first"); | |
return inputElemLst.length ? inputElemLst.val() : $(tdElem).text(); | |
}; | |
ts.handleSortClick = function(event){ | |
var columnIndex = $(this).index() + 1; | |
var tdList = $('table.table-sorter tbody tr td:nth-child(' + columnIndex + ')'); | |
var curValue; | |
for(var index=0;index<tdList.length;++index){ | |
if( $(this).hasClass("number") ){ | |
curValue = window.parseFloat(ts.getValue(tdList[index])) || 0; | |
} else { | |
curValue = ts.getValue(tdList[index]) || ""; | |
} | |
tdList[index].sortValue = curValue; | |
} | |
if( $(this).hasClass("number") ){ | |
if(sortStatus[columnIndex] == 1){ | |
tdList.sort(function(tdA,tdB){ | |
return tdB.sortValue - tdA.sortValue ; | |
}); | |
sortStatus[columnIndex] = 0; | |
} else { | |
tdList.sort(function(tdA,tdB){ | |
return tdA.sortValue - tdB.sortValue; | |
}); | |
sortStatus[columnIndex] = 1; | |
} | |
} else { | |
if(sortStatus[columnIndex] == 1){ | |
tdList.sort(function(tdA,tdB){ | |
return tdA.sortValue < tdB.sortValue ? 1 : -1; | |
}); | |
sortStatus[columnIndex] = 0; | |
} else { | |
tdList.sort(function(tdA,tdB){ | |
return tdA.sortValue > tdB.sortValue ? 1 : -1; | |
}); | |
sortStatus[columnIndex] = 1; | |
} | |
} | |
var finalSortedRows = []; | |
for(var index=0;index<tdList.length;++index){ | |
finalSortedRows.push( $( tdList[index] ).closest("tr")[0]); | |
} | |
$("table.table-sorter").append(finalSortedRows); | |
//uncomment to add formatting to highlight column on which sorting is performed. | |
//$(this).css("color", "#F00"); | |
}; | |
$("body").on("click","table.table-sorter thead th.sortable",ts.handleSortClick); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment