Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Release version 1.2.0: supports listeners + prevent a change from hap…

…pening + add CHANGELOG
  • Loading branch information...
commit f646576a5ed84c18da47b0cbe82f9d26c9d2c2fc 1 parent 29acd0c
@trimentor authored
View
21 CHANGELOG.md
@@ -0,0 +1,21 @@
+Version 1.0.0 / 2010-11-10
+--------------------------
+
+This is the first release of the jQuery tableSelect plugin.
+
+* Allow selection of just one single row in a table's tbody section by using `$("table").tableSelectOne()`.
+* Allow selection of multiple rows in a table's tbody section by using `$("table").tableSelectMany()`.
+
+Version 1.1.0 / 2010-11-12
+--------------------------
+
+* Enhancements
+ * Enable SHIFT + CTRL selection
+ * usage of sectionRowIndex
+
+Version 1.2.0 / 2010-12-15
+--------------------------
+
+* Enhancements
+ * Supports listeners: `beforerowselect`, `beforerowdeselect`, `afterrowselect`, and `afterrowdeselect`
+ * Prevent a change from happening by enabling `row.preventChange` in the `beforerowselect` and `beforerowdeselect` listeners.
View
74 demo.html
@@ -33,7 +33,7 @@
<h1>jQuery tableSelect Plugin &ndash; Row Selection made easy</h1>
<ol>
- <li><strong>Current Version:</strong> 1.1.0</li>
+ <li><strong>Current Version:</strong> 1.2.0</li>
<li><strong>License:</strong> MIT/GPL</li>
</ol>
@@ -43,7 +43,7 @@
<li><a href="http://jquery.com/">jQuery 1.2+ (<em>jQuery 1.4.3 recommended</em>)</a></li>
</ul>
- <h2>tableSelectOne()</h2>
+ <h2>tableSelectOne()</h2>
<table id="tableSelectOne">
<thead>
<tr>
@@ -148,5 +148,75 @@
});
});
</script>
+
+ <h3>Listeners</h3>
+ <ul>
+ <li><strong>beforerowselect</strong> fires before a row is selected</li>
+ <li><strong>afterrowselect</strong> fires after a row is selected</li>
+ <li><strong>beforerowdeselect</strong> fires before a row is deselected</li>
+ <li><strong>afterrowdeselect</strong> fires after a row is deselected</li>
+ </ul>
+
+ <table id="tableSelectListeners">
+ <thead>
+ <tr>
+ <th><input id="tableSelectListenersCbAll" type="checkbox" name="all" /></th>
+ <th>Link</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><input type="checkbox" name="one" /></td>
+ <td><a href="#tableSelectListeners">Example 1</a></td>
+ </tr>
+ <tr>
+ <td><input type="checkbox" name="two" /></td>
+ <td><a href="#tableSelectListeners">Example 2</a></td>
+ </tr>
+ <tr class="three">
+ <td><input type="checkbox" name="three" /></td>
+ <td><a href="#tableSelectListeners">Example 3</a></td>
+ </tr>
+ <tr>
+ <td><input type="checkbox" name="four" /></td>
+ <td><a href="#tableSelectListeners">Example 4</a></td>
+ </tr>
+ <tr>
+ <td><input type="checkbox" name="five" /></td>
+ <td><a href="#tableSelectListeners">Example 5</a></td>
+ </tr>
+ </tbody>
+ </table>
+
+ <script type="text/javascript">
+ $(document).ready(function() {
+ var tableMany = $("#tableSelectListeners").tableSelectMany({
+ listeners : {
+ beforerowselect : function(event) {
+ this.preventChange = (this.target === "a") ? true : false;
+ },
+ beforerowdeselect : function(event) {
+ this.preventChange = (this.target === "a") ? true : false;
+ },
+ afterrowselect : function(event) {
+ if(this.parentThis.allSelected()) $("input#tableSelectListenersCbAll").attr('checked', 'checked');
+ var toggle = $(this).find('input');
+ if(toggle.not(":checked")) toggle.attr('checked', 'checked');
+ },
+ afterrowdeselect : function(event) {
+ if(!this.parentThis.allSelected()) $("input#tableSelectListenersCbAll").removeAttr('checked');
+ var toggle = $(this).find('input');
+ if(toggle.is(":checked")) toggle.removeAttr('checked');
+ }
+ }
+ });
+ $("#tableSelectListeners input").each(function() { this.checked = null; });
+
+ // Select/Deselect all rows
+ $("input#tableSelectListenersCbAll").bind("click", function() {
+ this.checked ? tableMany.selectAll() : tableMany.clearSelections();
+ });
+ });
+ </script>
</body>
</html>
View
67 jquery.tableSelect.js
@@ -1,5 +1,5 @@
/*
- * jQuery tableSelect plugin 1.1.0
+ * jQuery tableSelect plugin 1.2.0
*
* Copyright (c) 2010 Kjel Delaey
*
@@ -48,6 +48,7 @@
prototype: {
init: function() {
this.selections = [];
+ this.listeners = this.options.listeners || {};
this.collectRows();
this.initRowEvents();
@@ -59,7 +60,7 @@
},
getSelections: function() {
- return this.selections;
+ return this.selections;
},
getFocusedRow: function() {
@@ -91,21 +92,37 @@
$(this).bind('click', table.handleMouseDown);
$(this).bind('rowselect', table.rowSelectClass);
$(this).bind('rowdeselect', table.rowSelectClass);
+ table.initListeners(table, this);
});
},
+
+ initListeners: function(table, row) {
+ if(table.listeners) {
+ var listeners = table.listeners;
+ if(listeners.beforerowselect) $(row).bind('beforerowselect', listeners.beforerowselect);
+ if(listeners.afterrowselect) $(row).bind('afterrowselect', listeners.afterrowselect);
+ if(listeners.beforerowdeselect) $(row).bind('beforerowdeselect', listeners.beforerowdeselect);
+ if(listeners.afterrowdeselect) $(row).bind('afterrowdeselect', listeners.afterrowdeselect);
+ }
+ },
- handleMouseDown: function(event) {
+ handleMouseDown: function(event) {
var table = this.parentThis;
+ table.storeEventTarget(event, this);
+
if(table.options.multiSelect) {
table.handleKeyDown(event, this);
}
else {
table.handleSingleSelect(this);
}
+
+ table.resetEventTarget(this);
},
handleKeyDown: function(event, row) {
var rowIndex = row.sectionRowIndex;
+
if(event.shiftKey) {
if(typeof(this.lastActiveRow) == "undefined") this.focusRow(rowIndex);
this.lockedRow = this.lastActiveRow;
@@ -121,9 +138,19 @@
this.handleSingleSelect(row);
}
},
+
+ storeEventTarget: function(event, row) {
+ var target = event.target && event.target.nodeName;
+ row.target = target ? target.toLowerCase() : null;
+ },
+
+ resetEventTarget: function(row) {
+ row.target = undefined;
+ },
handleSingleSelect: function(row) {
var rowIndex = row.sectionRowIndex;
+
if(this.isSelected(row)) {
this.deselectRow(rowIndex);
}
@@ -134,25 +161,35 @@
selectRow: function(rowIndex, keepSelections) {
var row = this.rows[rowIndex];
+
if(keepSelections == false) this.clearSelections();
- if(row && this.isSelected(row) == false) {
- this.selections.push(row);
- this.focusRow(rowIndex);
- $(row).trigger('rowselect');
- $(document).trigger('rowchange', this);
+ if(row && this.isSelected(row) == false && $(row).trigger('beforerowselect') !== false) {
+ if(row.preventChange !== true) {
+ this.selections.push(row);
+ this.focusRow(rowIndex);
+ $(row).trigger('rowselect');
+ $(row).trigger('afterrowselect');
+ $(document).trigger('rowchange', this);
+ }
+ row.preventChange = undefined;
}
},
deselectRow: function(rowIndex) {
var row = this.rows[rowIndex];
- if(row && this.isSelected(row)) {
- var index = $.inArray(row, this.selections);
- if(-1 != index) {
- this.selections.splice(index, 1);
- this.focusRow(rowIndex);
- $(row).trigger('rowdeselect');
- $(document).trigger('rowchange', this);
+
+ if(row && this.isSelected(row) && $(row).trigger('beforerowdeselect') !== false) {
+ if(row.preventChange !== true) {
+ var index = $.inArray(row, this.selections);
+ if(-1 != index) {
+ this.selections.splice(index, 1);
+ this.focusRow(rowIndex);
+ $(row).trigger('rowdeselect');
+ $(row).trigger('afterrowdeselect');
+ $(document).trigger('rowchange', this);
+ }
}
+ row.preventChange = undefined;
}
},
View
12 jquery.tableSelect.min.js
@@ -1 +1,11 @@
-(function(a){a.extend(a.fn,{tableSelect:function(b){tableSelector=new a.tableSelector(b,this);return tableSelector}});a.extend(a.fn,{tableSelectOne:function(b){var b=a.extend({multiSelect:false},b);tableSelector=new a.tableSelector(b,this);return tableSelector}});a.extend(a.fn,{tableSelectMany:function(b){var b=a.extend({multiSelect:true},b);tableSelector=new a.tableSelector(b,this);return tableSelector}});a.tableSelector=function(b,c){this.options=a.extend({},a.tableSelector.defaults,b);this.currentTable=c;this.init()};a.extend(a.tableSelector,{defaults:{tableClass:"tableselect",rowSelectClass:"selected",multiSelect:false},prototype:{init:function(){this.selections=[];this.collectRows();this.initRowEvents();a(this.currentTable).addClass(this.options.tableClass)},allSelected:function(){return(this.selections.length==this.rows.length)},getSelections:function(){return this.selections},getFocusedRow:function(){return this.rows[this.lastActiveRow]},isSelected:function(d){var b=false;for(var c=0;c<this.selections.length;c++){if(this.selections[c]==d){b=true;break}}return b},collectRows:function(){var b=this;this.rows=this.currentTable[0].tBodies[0].rows;a(this.rows).each(function(){this.parentThis=b})},initRowEvents:function(){var b=this;a(this.rows).each(function(){a(this).bind("click",b.handleMouseDown);a(this).bind("rowselect",b.rowSelectClass);a(this).bind("rowdeselect",b.rowSelectClass)})},handleMouseDown:function(c){var b=this.parentThis;if(b.options.multiSelect){b.handleKeyDown(c,this)}else{b.handleSingleSelect(this)}},handleKeyDown:function(b,c){var d=c.sectionRowIndex;if(b.shiftKey){if(typeof(this.lastActiveRow)=="undefined"){this.focusRow(d)}this.lockedRow=this.lastActiveRow;if(b.ctrlKey){this.selectRange(this.lastActiveRow,d,true)}else{this.selectRange(this.lockedRow,d,false);this.focusRow(this.lockedRow)}}else{this.handleSingleSelect(c)}},handleSingleSelect:function(b){var c=b.sectionRowIndex;if(this.isSelected(b)){this.deselectRow(c)}else{this.selectRow(c,this.options.multiSelect)}},selectRow:function(d,c){var b=this.rows[d];if(c==false){this.clearSelections()}if(b&&this.isSelected(b)==false){this.selections.push(b);this.focusRow(d);a(b).trigger("rowselect");a(document).trigger("rowchange",this)}},deselectRow:function(d){var c=this.rows[d];if(c&&this.isSelected(c)){var b=a.inArray(c,this.selections);if(-1!=b){this.selections.splice(b,1);this.focusRow(d);a(c).trigger("rowdeselect");a(document).trigger("rowchange",this)}}},focusRow:function(b){this.lastActiveRow=b},rowSelectClass:function(b){switch(b.type){case"rowselect":a(this).addClass(this.parentThis.options.rowSelectClass);break;case"rowdeselect":a(this).removeClass(this.parentThis.options.rowSelectClass);break;default:break}},selectAll:function(){if(this.options.multiSelect){this.clearSelections();a(this.rows).each(function(){this.parentThis.selectRow(this.sectionRowIndex,true)})}},clearSelections:function(){a(this.rows).each(function(){this.parentThis.deselectRow(this.sectionRowIndex)})},selectRange:function(d,c,e){if(e==false){this.clearSelections()}if(d<=c){for(var b=d;b<=c;b++){this.selectRow(b,true)}}else{for(var b=d;b>=c;b--){this.selectRow(b,true)}}}}})})(jQuery);
+/*
+ * jQuery tableSelect plugin 1.2.0
+ *
+ * Copyright (c) 2010 Kjel Delaey
+ *
+ * Dual licensed under the MIT and GPL licenses:
+ * http://www.opensource.org/licenses/mit-license.php
+ * http://www.gnu.org/licenses/gpl.html
+*/
+
+(function(a){a.extend(a.fn,{tableSelect:function(b){tableSelector=new a.tableSelector(b,this);return tableSelector}});a.extend(a.fn,{tableSelectOne:function(b){var b=a.extend({multiSelect:false},b);tableSelector=new a.tableSelector(b,this);return tableSelector}});a.extend(a.fn,{tableSelectMany:function(b){var b=a.extend({multiSelect:true},b);tableSelector=new a.tableSelector(b,this);return tableSelector}});a.tableSelector=function(b,c){this.options=a.extend({},a.tableSelector.defaults,b);this.currentTable=c;this.init()};a.extend(a.tableSelector,{defaults:{tableClass:"tableselect",rowSelectClass:"selected",multiSelect:false},prototype:{init:function(){this.selections=[];this.listeners=this.options.listeners||{};this.collectRows();this.initRowEvents();a(this.currentTable).addClass(this.options.tableClass)},allSelected:function(){return(this.selections.length==this.rows.length)},getSelections:function(){return this.selections},getFocusedRow:function(){return this.rows[this.lastActiveRow]},isSelected:function(d){var b=false;for(var c=0;c<this.selections.length;c++){if(this.selections[c]==d){b=true;break}}return b},collectRows:function(){var b=this;this.rows=this.currentTable[0].tBodies[0].rows;a(this.rows).each(function(){this.parentThis=b})},initRowEvents:function(){var b=this;a(this.rows).each(function(){a(this).bind("click",b.handleMouseDown);a(this).bind("rowselect",b.rowSelectClass);a(this).bind("rowdeselect",b.rowSelectClass);b.initListeners(b,this)})},initListeners:function(c,d){if(c.listeners){var b=c.listeners;if(b.beforerowselect){a(d).bind("beforerowselect",b.beforerowselect)}if(b.afterrowselect){a(d).bind("afterrowselect",b.afterrowselect)}if(b.beforerowdeselect){a(d).bind("beforerowdeselect",b.beforerowdeselect)}if(b.afterrowdeselect){a(d).bind("afterrowdeselect",b.afterrowdeselect)}}},handleMouseDown:function(c){var b=this.parentThis;b.storeEventTarget(c,this);if(b.options.multiSelect){b.handleKeyDown(c,this)}else{b.handleSingleSelect(this)}b.resetEventTarget(this)},handleKeyDown:function(b,c){var d=c.sectionRowIndex;if(b.shiftKey){if(typeof(this.lastActiveRow)=="undefined"){this.focusRow(d)}this.lockedRow=this.lastActiveRow;if(b.ctrlKey){this.selectRange(this.lastActiveRow,d,true)}else{this.selectRange(this.lockedRow,d,false);this.focusRow(this.lockedRow)}}else{this.handleSingleSelect(c)}},storeEventTarget:function(b,d){var c=b.target&&b.target.nodeName;d.target=c?c.toLowerCase():null},resetEventTarget:function(b){b.target=undefined},handleSingleSelect:function(b){var c=b.sectionRowIndex;if(this.isSelected(b)){this.deselectRow(c)}else{this.selectRow(c,this.options.multiSelect)}},selectRow:function(d,c){var b=this.rows[d];if(c==false){this.clearSelections()}if(b&&this.isSelected(b)==false&&a(b).trigger("beforerowselect")!==false){if(b.preventChange!==true){this.selections.push(b);this.focusRow(d);a(b).trigger("rowselect");a(b).trigger("afterrowselect");a(document).trigger("rowchange",this)}b.preventChange=undefined}},deselectRow:function(d){var c=this.rows[d];if(c&&this.isSelected(c)&&a(c).trigger("beforerowdeselect")!==false){if(c.preventChange!==true){var b=a.inArray(c,this.selections);if(-1!=b){this.selections.splice(b,1);this.focusRow(d);a(c).trigger("rowdeselect");a(c).trigger("afterrowdeselect");a(document).trigger("rowchange",this)}}c.preventChange=undefined}},focusRow:function(b){this.lastActiveRow=b},rowSelectClass:function(b){switch(b.type){case"rowselect":a(this).addClass(this.parentThis.options.rowSelectClass);break;case"rowdeselect":a(this).removeClass(this.parentThis.options.rowSelectClass);break;default:break}},selectAll:function(){if(this.options.multiSelect){this.clearSelections();a(this.rows).each(function(){this.parentThis.selectRow(this.sectionRowIndex,true)})}},clearSelections:function(){a(this.rows).each(function(){this.parentThis.deselectRow(this.sectionRowIndex)})},selectRange:function(d,c,e){if(e==false){this.clearSelections()}if(d<=c){for(var b=d;b<=c;b++){this.selectRow(b,true)}}else{for(var b=d;b>=c;b--){this.selectRow(b,true)}}}}})})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.