1- /*! Checkboxes 1.0.4
1+ /*! Checkboxes 1.1.0-dev
22 * Copyright (c) Gyrocode (www.gyrocode.com)
33 * License: MIT License
44 */
55
66/**
77 * @summary Checkboxes
88 * @description Checkboxes extension for jQuery DataTables
9- * @version 1.0.4
9+ * @version 1.1.0-dev
1010 * @file dataTables.checkboxes.js
1111 * @author Gyrocode (http://www.gyrocode.com/projects/jquery-datatables-checkboxes/)
1212 * @contact http://www.gyrocode.com/contacts
@@ -78,13 +78,17 @@ Checkboxes.prototype = {
7878 var self = this ;
7979 var dt = self . s . dt ;
8080 var ctx = dt . settings ( ) [ 0 ] ;
81+ var hasCheckboxes = false ;
82+ var hasCheckboxesSelectRow = false ;
8183
8284 for ( var i = 0 ; i < ctx . aoColumns . length ; i ++ ) {
8385 if ( ctx . aoColumns [ i ] . checkboxes ) {
8486 //
8587 // INITIALIZATION
8688 //
8789
90+ hasCheckboxes = true ;
91+
8892 if ( ! $ . isPlainObject ( ctx . aoColumns [ i ] . checkboxes ) ) {
8993 ctx . aoColumns [ i ] . checkboxes = { } ;
9094 }
@@ -108,7 +112,7 @@ Checkboxes.prototype = {
108112 'className' : 'dt-body-center' ,
109113 'render' : function ( data , type , full , meta ) {
110114 if ( type === 'display' ) {
111- data = '<input type="checkbox">' ;
115+ data = '<input type="checkbox" class="dt-checkboxes" >' ;
112116 }
113117 return data ;
114118 }
@@ -147,77 +151,82 @@ Checkboxes.prototype = {
147151 //
148152
149153 if ( ctx . aoColumns [ i ] . checkboxes . selectRow ) {
150- $ ( dt . table ( ) . node ( ) ) . addClass ( 'checkboxes-select' ) ;
154+ hasCheckboxesSelectRow = true ;
155+
156+ $ ( dt . table ( ) . node ( ) ) . addClass ( 'dt-checkboxes-select' ) ;
151157 }
152158
153159 if ( ctx . aoColumns [ i ] . checkboxes . selectAll ) {
154160 $ ( dt . column ( i ) . header ( ) )
155161 . html ( '<input type="checkbox">' )
156- . addClass ( 'checkboxes-select-all' ) ;
162+ . addClass ( 'dt- checkboxes-select-all' ) ;
157163 }
164+ }
165+ }
158166
159167
160- //
161- // EVENT HANDLERS
162- //
163- var $table = $ ( dt . table ( ) . node ( ) ) ;
164- var $table_body = $ ( dt . table ( ) . body ( ) ) ;
165- var $table_header = $ ( dt . table ( ) . header ( ) ) ;
168+ // If table has at least one checkbox
169+ if ( hasCheckboxes ) {
170+ //
171+ // EVENT HANDLERS
172+ //
173+ var $table = $ ( dt . table ( ) . node ( ) ) ;
174+ var $table_body = $ ( dt . table ( ) . body ( ) ) ;
175+ var $table_header = $ ( dt . table ( ) . header ( ) ) ;
166176
167- // Handles checkbox click event
168- $table_body . on ( 'click' , 'input[type="checkbox"] ' , function ( e ) {
169- self . onClick ( e , this ) ;
170- } ) ;
177+ // Handles checkbox click event
178+ $table_body . on ( 'click' , 'input.dt-checkboxes ' , function ( e ) {
179+ self . onClick ( e , this ) ;
180+ } ) ;
171181
172- // Handle row select/deselect event
173- if ( ctx . aoColumns [ i ] . checkboxes . selectRow ) {
174- // If Select extension is available
175- if ( DataTable . select ) {
176- // Handle row selection event
177- $table . on ( 'select.dt deselect.dt' , function ( e , api , type , indexes ) {
178- self . onSelect ( e , type , indexes ) ;
179- } ) ;
180-
181- // Disable Select extension information display
182- dt . select . info ( false ) ;
183-
184- // Otherwise, if Select extension is not available
185- } else {
186- $table_body . on ( 'click' , 'td' , function ( ) {
187- var $row = $ ( this ) . closest ( 'tr' ) ;
188- var e = {
189- type : ( $row . hasClass ( 'selected' ) ? 'deselect' : 'select' )
190- } ;
191-
192- self . onSelect ( e , 'row' , [ dt . row ( $row ) . index ( ) ] ) ;
193-
194- $row . toggleClass ( 'selected' ) ;
195-
196- $table . trigger ( e . type ) ;
197- } ) ;
198- }
182+ // Handle row select/deselect event
183+ if ( hasCheckboxesSelectRow ) {
184+ // If Select extension is available
185+ if ( DataTable . select ) {
186+ // Handle row selection event
187+ $table . on ( 'select.dt deselect.dt' , function ( e , api , type , indexes ) {
188+ self . onSelect ( e , type , indexes ) ;
189+ } ) ;
190+
191+ // Disable Select extension information display
192+ dt . select . info ( false ) ;
199193
200- // Update the table information element with selected item summary
201- $table . on ( 'draw.dt select.dt deselect.dt' , function ( ) {
202- self . showInfoSelected ( ) ;
194+ // Otherwise, if Select extension is not available
195+ } else {
196+ $table_body . on ( 'click' , 'td' , function ( ) {
197+ var $row = $ ( this ) . closest ( 'tr' ) ;
198+ var e = {
199+ type : ( $row . hasClass ( 'selected' ) ? 'deselect' : 'select' )
200+ } ;
201+
202+ self . onSelect ( e , 'row' , [ dt . row ( $row ) . index ( ) ] ) ;
203+
204+ $row . toggleClass ( 'selected' ) ;
205+
206+ $table . trigger ( e . type ) ;
203207 } ) ;
204208 }
205209
206- // Handle table draw event
207- $table . on ( 'draw.dt' , function ( e , ctx ) {
208- self . onDraw ( e , ctx ) ;
210+ // Update the table information element with selected item summary
211+ $table . on ( 'draw.dt select.dt deselect.dt ' , function ( ) {
212+ self . showInfoSelected ( ) ;
209213 } ) ;
214+ }
210215
211- // Handle click on "Select all" control
212- $table_header . on ( 'click' , 'th.checkboxes-select-all input[type="checkbox"] ', function ( e ) {
213- self . onClickSelectAll ( e , this ) ;
214- } ) ;
216+ // Handle table draw event
217+ $table . on ( 'draw.dt ' , function ( e , ctx ) {
218+ self . onDraw ( e , ctx ) ;
219+ } ) ;
215220
216- // Handle click on heading containing "Select all" control
217- $table_header . on ( 'click' , 'th.checkboxes-select-all' , function ( e ) {
218- $ ( 'input[type="checkbox"]' , this ) . trigger ( 'click' ) ;
219- } ) ;
220- }
221+ // Handle click on "Select all" control
222+ $table_header . on ( 'click' , 'th.dt-checkboxes-select-all input[type="checkbox"]' , function ( e ) {
223+ self . onClickSelectAll ( e , this ) ;
224+ } ) ;
225+
226+ // Handle click on heading containing "Select all" control
227+ $table_header . on ( 'click' , 'th.dt-checkboxes-select-all' , function ( e ) {
228+ $ ( 'input[type="checkbox"]' , this ) . trigger ( 'click' ) ;
229+ } ) ;
221230 }
222231 } ,
223232
@@ -306,7 +315,7 @@ Checkboxes.prototype = {
306315 }
307316 } ) ;
308317 }
309-
318+
310319 if ( nodes . length ) {
311320 // If Select extension is available
312321 if ( DataTable . select ) {
0 commit comments