Permalink
Browse files

TableSorter should support columns that are not sortable.

Contributed by guido.tapia.
Fixes issue 222.

Revision created by MOE tool push_codebase.

R=pupius
DELTA=48  (32 added, 1 deleted, 15 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=342233


git-svn-id: http://closure-library.googlecode.com/svn/trunk@362 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
1 parent 94f1d3f commit 63b1be74ebaf7b08cf07b7e679b1898ac7c70ef0 nicksantos@google.com committed Oct 15, 2010
Showing with 45 additions and 15 deletions.
  1. +1 −0 AUTHORS
  2. +1 −1 closure/goog/deps.js
  3. +21 −3 closure/goog/ui/tablesorter.js
  4. +22 −11 closure/goog/ui/tablesorter_test.html
View
@@ -7,3 +7,4 @@ Google Inc.
Mohamed Mansour <hello@mohamedmansour.com>
Bjorn Tipling <bjorn.tipling@gmail.com>
SameGoal LLC <help@samegoal.com>
+Guido Tapia <guido.tapia@gmail.com>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -28,6 +28,7 @@ goog.require('goog.dom.TagName');
goog.require('goog.dom.classes');
goog.require('goog.events');
goog.require('goog.events.EventType');
+goog.require('goog.functions');
goog.require('goog.ui.Component');
@@ -180,8 +181,9 @@ goog.ui.TableSorter.prototype.sort_ = function(e) {
// Perform the sort.
if (this.dispatchEvent(goog.ui.TableSorter.EventType.BEFORESORT)) {
- this.sort(col, reverse);
- this.dispatchEvent(goog.ui.TableSorter.EventType.SORT);
+ if (this.sort(col, reverse)) {
+ this.dispatchEvent(goog.ui.TableSorter.EventType.SORT);
+ }
}
};
@@ -190,8 +192,14 @@ goog.ui.TableSorter.prototype.sort_ = function(e) {
* Sort the table contents by the values in the given column.
* @param {number} column The column to sort by.
* @param {boolean=} opt_reverse Whether to sort in reverse.
+ * @return {boolean} Whether the sort was executed.
*/
goog.ui.TableSorter.prototype.sort = function(column, opt_reverse) {
+ var sortFunction = this.getSortFunction(column);
+ if (sortFunction === goog.ui.TableSorter.noSort) {
+ return false;
+ }
+
// Get some useful DOM nodes.
var table = this.getElement();
var tBody = table.tBodies[0];
@@ -222,7 +230,6 @@ goog.ui.TableSorter.prototype.sort = function(column, opt_reverse) {
}
// Sort the array.
- var sortFunction = this.getSortFunction(column);
var multiplier = this.reversed_ ? -1 : 1;
goog.array.stableSort(values,
function(a, b) {
@@ -248,9 +255,20 @@ goog.ui.TableSorter.prototype.sort = function(column, opt_reverse) {
goog.dom.classes.add(header, this.reversed_ ?
goog.getCssName('goog-tablesorter-sorted-reverse') :
goog.getCssName('goog-tablesorter-sorted'));
+
+ return true;
};
+/**
+ * Disables sorting on the specified column
+ * @param {*} a First sort value.
+ * @param {*} b Second sort value.
+ * @return {number} Negative if a < b, 0 if a = b, and positive if a > b.
+ */
+goog.ui.TableSorter.noSort = goog.functions.error('no sort');
+
+
/**
* A numeric sort function.
* @param {*} a First sort value.
@@ -22,29 +22,31 @@
<div id="content">
<table id="sortable">
<thead>
- <tr><th>alpha</th><th>number</th></tr>
+ <tr><th>alpha</th><th>number</th><th>not sortable</th></tr>
</thead>
<tbody>
- <tr><td>C</td><td>10</td></tr>
- <tr><td>A</td><td>10</td></tr>
- <tr><td>C</td><td>17</td></tr>
- <tr><td>B</td><td>0</td></tr>
- <tr><td>C</td><td>3</td></tr>
+ <tr><td>C</td><td>10</td><td></td></tr>
+ <tr><td>A</td><td>10</td><td></td></tr>
+ <tr><td>C</td><td>17</td><td></td></tr>
+ <tr><td>B</td><td>0</td><td></td></tr>
+ <tr><td>C</td><td>3</td><td></td></tr>
</tbody>
</table>
</div>
<script>
var oldHtml = goog.dom.getElement('content').innerHTML;
- var alphaHeader, numberHeader, table, tableSorter;
+ var alphaHeader, numberHeader, notSortableHeader, table, tableSorter;
function setUp() {
goog.dom.getElement('content').innerHTML = oldHtml;
table = goog.dom.getElement('sortable');
alphaHeader = table.getElementsByTagName('TH')[0];
numberHeader = table.getElementsByTagName('TH')[1];
+ notSortableHeader = table.getElementsByTagName('TH')[2];
tableSorter = new goog.ui.TableSorter();
tableSorter.setSortFunction(0, goog.ui.TableSorter.alphaSort);
+ tableSorter.setSortFunction(2, goog.ui.TableSorter.noSort);
tableSorter.decorate(table);
}
@@ -103,11 +105,20 @@
assertFalse(tableSorter.isSortReversed());
}
+ function testNotSortableUnchanged() {
+ goog.testing.events.fireClickEvent(notSortableHeader);
+ assertEquals(0, goog.dom.classes.get(notSortableHeader).length);
+ assertEquals(-1, tableSorter.getSortColumn());
+ }
+
function assertOrder(arr) {
- var actual = goog.array.map(table.getElementsByTagName('TD'),
- function(td) {
- return goog.dom.getTextContent(td);
- });
+ var actual = [];
+ goog.array.forEach(table.getElementsByTagName('TD'), function(td, idx) {
+ var txt = goog.dom.getTextContent(td);
+ if (txt) {
+ actual.push(txt);
+ }
+ });
assertArrayEquals(arr, actual);
}

0 comments on commit 63b1be7

Please sign in to comment.