Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extend with SHIFT- and CTRL-selection

  • Loading branch information...
commit 6b69da460552145fd1d73f4f9824de653b29f186 1 parent f28bd99
Kjel Delaey authored
View
18 README
@@ -1 +1,17 @@
-[jQuery tableSelect Plugin - Row Selection made easy
+== jQuery tableSelect Plugin - Row Selection made easy
+
+Allow selection of just one single row in a table's tbody section:
+$("#tableSelectOne").tableSelectOne();
+
+Allow selection of multiple rows in a table's tbody section:
+$("#tableSelectMany").tableSelectMany();
+
+It is possible to execute a function each time a row has been changed:
+$(document).bind('rowchange', function(event, table) {});
+
+Some useful methods:
+ table.allSelected() // Are all rows selected?
+ table.getFocusedRow() // Get last active row
+ table.getSelections() // Get all selected rows
+ table.isSelected(row) // Is this row already selected?
+
View
12 demo.html
@@ -4,8 +4,8 @@
<title>jQuery tableSelect Plugin</title>
<!-- <script type="text/javascript" src="jquery-1.2.min.js"></script> -->
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
- <script type="text/javascript" src="jquery.tableSelect.min.js"></script>
- <!-- <script type="text/javascript" src="jquery.tableSelect.js"></script> -->
+ <!-- <script type="text/javascript" src="jquery.tableSelect.min.js"></script> -->
+ <script type="text/javascript" src="jquery.tableSelect.js"></script>
<style type="text/css">
table {
border: 1px solid black;
@@ -33,7 +33,7 @@
<h1>jQuery tableSelect Plugin &ndash; Row Selection made easy</h1>
<ol>
- <li><strong>Current Version:</strong> 1.0.0</li>
+ <li><strong>Current Version:</strong> 1.1.0</li>
<li><strong>License:</strong> MIT/GPL</li>
</ol>
@@ -82,7 +82,7 @@
$(document).bind('rowchange', function(event, table) {
if(tableOne == table) {
- var row = table.lastActive();
+ var row = table.getFocusedRow();
table.isSelected(row) ?
$(row).find('input').attr('checked', 'checked') :
$(row).find('input').removeAttr('checked');
@@ -136,7 +136,7 @@
// Do something once a row has changed
$(document).bind('rowchange', function(event, table) {
if(tableMany == table) {
- var row = table.lastActive();
+ var row = table.getFocusedRow();
table.isSelected(row) ?
$(row).find('input').attr('checked', 'checked') :
$(row).find('input').removeAttr('checked');
@@ -149,4 +149,4 @@
});
</script>
</body>
-</html>
+</html>
View
87 jquery.tableSelect.js
@@ -1,5 +1,5 @@
/*
- * jQuery tableSelect plugin 1.0.0
+ * jQuery tableSelect plugin 1.1.0
*
* Copyright (c) 2010 Kjel Delaey
*
@@ -18,7 +18,7 @@
$.extend($.fn, {
tableSelectOne: function(options) {
- var options = $.extend({keepSelections: false}, options);
+ var options = $.extend({multiSelect: false}, options);
tableSelector = new $.tableSelector(options, this)
return tableSelector;
}
@@ -26,7 +26,7 @@
$.extend($.fn, {
tableSelectMany: function(options) {
- var options = $.extend({keepSelections: true}, options);
+ var options = $.extend({multiSelect: true}, options);
tableSelector = new $.tableSelector(options, this)
return tableSelector;
}
@@ -42,7 +42,7 @@
defaults: {
tableClass: "tableselect",
rowSelectClass: "selected",
- keepSelections: false
+ multiSelect: false
},
prototype: {
@@ -54,16 +54,27 @@
$(this.currentTable).addClass(this.options.tableClass);
},
- lastActive: function() {
- return this.rows[this.lastActiveRow];
+ allSelected: function() {
+ return (this.selections.length == this.rows.length);
},
- allSelected: function() {
- return (this.selections.length == this.rows.length);
+ getSelections: function() {
+ return this.selections;
},
- selectedRows: function() {
- return this.selections;
+ getFocusedRow: function() {
+ return this.rows[this.lastActiveRow];
+ },
+
+ isSelected: function(row) {
+ var bool = false
+ for(var i=0; i<this.selections.length; i++) {
+ if(this.selections[i] == row) {
+ bool = true;
+ break;
+ }
+ }
+ return bool;
},
collectRows: function() {
@@ -84,33 +95,42 @@
},
handleMouseDown: function(event) {
- var table = this.parentThis;
- var rowIndex = this.rowIndex-1;
- if(table.isSelected(this) == false) {
- table.selectRow(rowIndex, table.options.keepSelections);
+ var table = this.parentThis;
+ if(table.options.multiSelect) {
+ table.handleKeyDown(event, this);
}
else {
- table.deselectRow(rowIndex, table.options.keepSelections);
+ table.handleSingleSelect(this);
}
},
- /*handleKeyDown: function(event) {
- var table = event.data.row.parentThis;
- var rowIndex = event.data.row.rowIndex-1;
-
- if(event.ctrlKey) {
- if(table.isSelected(event.data.row) == false) {
- table.selectRow(rowIndex, true);
+ handleKeyDown: function(event, row) {
+ var rowIndex = row.rowIndex-1;
+ if(event.shiftKey) {
+ if(typeof(this.lastActiveRow) == "undefined") this.focusRow(rowIndex);
+ this.lockedRow = this.lastActiveRow;
+ if(event.ctrlKey) {
+ this.selectRange(this.lastActiveRow, rowIndex, true);
}
else {
- table.deselectRow(rowIndex, true);
+ this.selectRange(this.lockedRow, rowIndex, false);
+ this.focusRow(this.lockedRow);
}
}
+ else {
+ this.handleSingleSelect(row);
+ }
+ },
- if(event.shiftKey) {
- table.selectRange(table.lastActiveRow, rowIndex, true);
+ handleSingleSelect: function(row) {
+ var rowIndex = row.rowIndex-1;
+ if(this.isSelected(row)) {
+ this.deselectRow(rowIndex);
+ }
+ else {
+ this.selectRow(rowIndex, this.options.multiSelect);
}
- },*/
+ },
selectRow: function(rowIndex, keepSelections) {
var row = this.rows[rowIndex];
@@ -136,17 +156,6 @@
}
},
- isSelected: function(row) {
- var bool = false
- for(var i=0; i<this.selections.length; i++) {
- if(this.selections[i] == row) {
- bool = true;
- break;
- }
- }
- return bool;
- },
-
focusRow: function(rowIndex) {
this.lastActiveRow = rowIndex;
},
@@ -164,10 +173,10 @@
},
selectAll: function() {
- if(this.options.keepSelections) {
+ if(this.options.multiSelect) {
this.clearSelections();
$(this.rows).each(function() {
- this.parentThis.selectRow(this.rowIndex-1, this.parentThis.options.keepSelections);
+ this.parentThis.selectRow(this.rowIndex-1, true);
});
}
},
View
4 jquery.tableSelect.min.js
@@ -1,5 +1,5 @@
/*
- * jQuery tableSelect plugin 1.0.0
+ * jQuery tableSelect plugin 1.1.0
*
* Copyright (c) 2010 Kjel Delaey
*
@@ -8,4 +8,4 @@
* 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({keepSelections:false},b);tableSelector=new a.tableSelector(b,this);return tableSelector}});a.extend(a.fn,{tableSelectMany:function(b){var b=a.extend({keepSelections: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",keepSelections:false},prototype:{init:function(){this.selections=[];this.collectRows();this.initRowEvents();a(this.currentTable).addClass(this.options.tableClass)},lastActive:function(){return this.rows[this.lastActiveRow]},allSelected:function(){return(this.selections.length==this.rows.length)},selectedRows:function(){return this.selections},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;var d=this.rowIndex-1;if(b.isSelected(this)==false){b.selectRow(d,b.options.keepSelections)}else{b.deselectRow(d,b.options.keepSelections)}},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)}}},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},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.keepSelections){this.clearSelections();a(this.rows).each(function(){this.parentThis.selectRow(this.rowIndex-1,this.parentThis.options.keepSelections)})}},clearSelections:function(){a(this.rows).each(function(){this.parentThis.deselectRow(this.rowIndex-1)})},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);
+(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.rowIndex-1;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.rowIndex-1;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.rowIndex-1,true)})}},clearSelections:function(){a(this.rows).each(function(){this.parentThis.deselectRow(this.rowIndex-1)})},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.