(function($){
var tablesortPrototype={
options:{
header:'thead',
body:'tbody',
sortableColumns:[0],
sortableClass:'sortable'
},
_create:function(){
console.log('create');
var widget=this;
var $domTable=widget.element;
var options=widget.options;
var $headings=$('th',options.header);
//For each column index as specified in the sortable columns, add a click event handler
//and the necessary css
$.each(options.sortableColumns,function(index){
var columnIndex=this;
$headings.eq(columnIndex).click(function(){
widget._toggleSort(columnIndex, this);
})
.addClass(options.sortableClass);
});
},
_init:function(){
console.log('init');
},
_toggleSort:function(columnIndex,srcDomHeader){
var widget=this;
var $domTable=widget.element;
var options=widget.options;
//Get the column which has to be sorted, i.e. get all the td elements of that particular column
var column=$(options.body,$domTable).find('tr>td:nth-child('+(columnIndex+1)+')');
//Convert the jQuery object into an array
var columnArray=[];
$.each(column,function(index){
columnArray[index]=$(this);
});
//Get the heading
var $currentHeading=$(srcDomHeader);
if($currentHeading.data('sortstatus')){
//If the column is already sorted, just reverse the rows
columnArray.reverse();
}
else{
//If not already sorted
//Clear the sort status on all the other columns
$currentHeading.siblings().data('sortstatus',false);
//Determine the datatype for the column(whether string or numeric)
$.each(columnArray,function(index){
if(isNaN($(this).text())){
columnArray.dataType='string';
return false;
}
});
if(columnArray.dataType!='string'){
columnArray.dataType='number';
}
//Sort the array using Arrays.prototype.sort by passing a custom function
columnArray.sort(function(a,b){
var value1='';
var value2='';
if(columnArray.dataType=='string'){
value1=$(a).text();
value2=$(b).text();
}
else{
value1=parseFloat($(a).text());
value2=parseFloat($(b).text());
}
return (value1 -->