WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Bug and possible solution in sort() method of SortableTable, Adam Killander, July 15, 2003
     Re:Bug and possible solution in sort() method of SortableTable, Erik Arvidsson, July 16, 2003

Subject: Bug and possible solution in sort() method of SortableTable From: Adam Killander Date: July 15, 2003

 

The otherwise excellent SortableTable script has a bug when you try to use the second parameter (bDescending) in the sort() method.

I wanted to change the default sort order to ascending, so I wrote this method to override the sort() method:

// override sort() to change default sort order to ascending
SortableTable.prototype._sort = SortableTable.prototype.sort;
SortableTable.prototype.sort = function (nColumn, bDescending, sSortType) {
 var descending = bDescending;
 if ( typeof descending != 'boolean' ) {
  descending = (this.sortColumn == nColumn) ? !this.descending : false;
 }

 this._sort(nColumn, descending, sSortType);
};

But the sort() method uses the property descending instead of the parameter bDescending which means that the table will be sorted ascending every time since the default value of the descending is null (count as false). It doesn't matter if I use false or true as the bDescending argument.

To solve the problem I have to set the property myself before sorting:

// override sort to change default sort order to ascending
SortableTable.prototype._sort = SortableTable.prototype.sort;
SortableTable.prototype.sort = function (nColumn, bDescending, sSortType) {
 var descending = bDescending;
 if ( typeof descending != 'boolean' ) {
  descending = (this.sortColumn == nColumn) ? !this.descending : false;
 }

 // BUG WORKAROUND:
 this.descending = descending;
 this._sort(nColumn, descending, sSortType);
};

 

To the next release of the SortableTable you might want to fix the bug so that the bDescending parameter is used:

SortableTable.prototype.sort = function (nColumn, bDescending, sSortType) {
 if (sSortType == null)
  sSortType = this.getSortType(nColumn);

 // exit if None 
 if (sSortType == "None")
  return;
 
 if (bDescending == null) {
  if (this.sortColumn != nColumn)
   this.descending = true;
  else
   this.descending = !this.descending;
 }
 // BUG FIX:
 else {
  this.descending = bDescending;

 }
 .. more code ...

 

/Adam Killander




Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX