Skip to content

Commit 7f576bb

Browse files
implement options for onRowClick, as well as extend the plugin to allow for chaining and various events
1 parent 2d6b812 commit 7f576bb

5 files changed

+129
-16
lines changed

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ jquery.tabelizer
33

44
Multi level grouping indicators for tables.
55

6-
Tabelizer 1.0.2 - multi level grouping indicators for tables
7-
Version 1.0.2
6+
Tabelizer 1.0.3 - multi level grouping indicators for tables
7+
Version 1.0.3
88
Requires jQuery v1.6+ and jQuery.ui core
99

1010
Copyright (c) 2014 Rafael Huisman
@@ -18,3 +18,35 @@ http://powerconsulting.co/Samples/Tabelizer
1818
Example Usage:
1919

2020
$('#table1').tabelize();
21+
22+
23+
V1.0.3
24+
----------
25+
Added public object to return methods to caller
26+
27+
When calling tabelize after initial setup, it will return an object with usable methods, thus providing the ability to chain commands
28+
$('#table1').tabelize()
29+
30+
Added ability to pass in the following options to the plugin
31+
32+
onRowClick : function(evt){
33+
//Event to occur when row is clicked, this will override the build in onRowClick
34+
//evt is the event passed in for the click occurance. this could be a row or just the expander depending on the fullRowClickable option
35+
}
36+
37+
fullRowClickable : true, //When this is set to false, only the expander will be clickable
38+
39+
onReady : function(){
40+
//When the plugin finishes it's initialization, this callback method will be called
41+
//this refers to the public object self.getPublicObj()
42+
},
43+
onBeforeRowClick : function(evt){
44+
//Anything that you want to run before the onRowClick code gets handled goes in here.
45+
//evt is the event passed in for the click occurance. this could be a row or just the expander depending on the fullRowClickable option
46+
//this refers to the public object self.getPublicObj()
47+
},
48+
onAfterRowClick : function(evt){
49+
//Anything that you want to run after the onRowClick code gets handled goes in here.
50+
//evt is the event passed in for the click occurance. this could be a row or just the expander depending on the fullRowClickable option
51+
//this refers to the public object self.getPublicObj()
52+
},

example.html

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
22
<script src="jquery-ui-1.10.4.custom.min.js"></script>
3-
<script src="jquery.tabelizer.min.js"></script>
3+
<script src="jquery.tabelizer.js"></script>
44
<link href="tabelizer.min.css" media="all" rel="stylesheet" type="text/css" />
55
<script>
66
$(document).ready(function(){
7-
var table1 = $('#table1').tabelize();
7+
var table1 = $('#table1').tabelize({
8+
/*onRowClick : function(){
9+
alert('test');
10+
}*/
11+
fullRowClickable : true,
12+
onReady : function(){
13+
console.log('ready');
14+
},
15+
onBeforeRowClick : function(){
16+
console.log('onBeforeRowClick');
17+
},
18+
onAfterRowClick : function(){
19+
console.log('onAfterRowClick');
20+
},
21+
});
822
});
923
</script>
1024

jquery.tabelizer.js

+73-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
3-
* Tabelizer 1.0.2 - multi level grouping indicators for tables
4-
* Version 1.0.2
3+
* Tabelizer 1.0.3 - multi level grouping indicators for tables
4+
* Version 1.0.3
55
* @requires jQuery v1.6+ and jQuery.ui core
66
*
77
* Copyright (c) 2014 Rafael Huisman
@@ -21,8 +21,22 @@
2121
(function($){
2222
var self = {};
2323

24+
self.isFunction = function(func) {
25+
return func && {}.toString.call(func) === '[object Function]';
26+
}
27+
2428
self.rowClicker = function(evt){
25-
var $row = $(evt.currentTarget);
29+
30+
if (typeof self.conf.onBeforeRowClick != 'undefined' && self.isFunction(self.conf.onBeforeRowClick))
31+
self.conf.onBeforeRowClick.apply(self.getPublicObj(), [evt]);
32+
33+
var $elm = $(evt.currentTarget);
34+
35+
if (!$elm.is('tr'))
36+
$elm = $elm.parentsUntil('tr').parent()
37+
38+
$row = $elm;
39+
2640
var id = $row.attr('id');
2741

2842
//Simple toggle for contract/expand logic
@@ -36,6 +50,11 @@
3650

3751
//After any contraction or expansion we need to resetup the lines since they will likely change.
3852
self.updateLines();
53+
54+
if (typeof self.conf.onAfterRowClick != 'undefined' && self.isFunction(self.conf.onAfterRowClick))
55+
self.conf.onAfterRowClick.apply(self.getPublicObj(), [evt]);
56+
57+
return self.getPublicObj();
3958
}
4059

4160
self.updateLines = function(){
@@ -81,6 +100,8 @@
81100
$prevRow.addClass(' l' + (parseInt(x)) + '-last')
82101
}
83102
}
103+
104+
return self.getPublicObj();
84105
}
85106

86107
//this method toggles the children on or off, including a sliding motion
@@ -139,6 +160,12 @@
139160

140161
prevRowLevel = rowLevel;
141162
});
163+
164+
return self.getPublicObj();
165+
}
166+
167+
self.updateData = function(){
168+
self.caller.data('tabelizer', self);
142169
}
143170

144171
self.maxLevel = 0;
@@ -177,18 +204,56 @@
177204
//Add the expanded class, which through css adds in the arrow image
178205
$firstCol.html(levelLines + ' <div class="expander"></div> ' + firstColVal);
179206

180-
//apply the method to be called on each row click
181-
$row.on('click', self.rowClicker);
207+
//apply the method to be called on each row click, if fullRowClickable is set to false, then only the expander is clickable
208+
if (self.conf.fullRowClickable)
209+
$row.on('click', self.conf.onRowClick);
210+
else
211+
$row.find('.expander').on('click', self.conf.onRowClick);
182212
prevLevel = currentLevel;
183213
$prevRow= $row;
184214
}
185215
});
186216

187217
self.updateLines();
218+
219+
if (typeof self.conf.onReady != 'undefined' && self.isFunction(self.conf.onReady))
220+
self.conf.onReady.apply(self.getPublicObj(), []);
221+
222+
return self.getPublicObj();
188223
}
189-
224+
225+
self.getPublicObj = function(){ return {
226+
options : self.conf,
227+
toggleChildren : self.toggleChildren,
228+
updateLines : self.updateLines,
229+
rowClicker : self.rowClicker,
230+
maxLevel : self.maxLevel,
231+
updateData : self.updateData
232+
}};
233+
234+
self.conf = {
235+
onRowClick : self.rowClicker,
236+
fullRowClickable : true,//must be set before init
237+
onBeforeRowClick : null,
238+
onAfterRowClick : null,
239+
onReady : null
240+
};
241+
190242
$.fn.tabelize = function(confProp){
191-
self.caller = this;
192-
self.init();
243+
244+
var existingSelf = this.data('tabelizer');
245+
if (typeof existingSelf == 'undefined'){
246+
$.extend(self.conf, confProp);
247+
self.caller = this;
248+
self.init();
249+
250+
}else{
251+
self = existingSelf;
252+
$.extend(self.conf, confProp);
253+
}
254+
//Store copy of self in data for repeat calls, update it after any repeating call
255+
self.updateData();
256+
257+
return self.getPublicObj()
193258
};
194259
})(jQuery);

jquery.tabelizer.min.js

+5-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tabelizer.jquery.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"grouping",
88
"subtotal"
99
],
10-
"version": "1.0.2",
10+
"version": "1.0.3",
1111
"author": {
1212
"name": "Rafael Huisman",
1313
"url": "http://powerconsulting.co"

0 commit comments

Comments
 (0)