1
- /*! AlphabetSearch for DataTables v1.1.4
1
+ /*! AlphabetSearch for DataTables v1.2.0-dev
2
2
* 2014 SpryMedia Ltd - datatables.net/license
3
3
* Gyrocode - MIT License
4
4
*/
7
7
* @summary AlphabetSearch
8
8
* @description Show an set of alphabet buttons alongside a table providing
9
9
* search input options
10
- * @version 1.1.4
10
+ * @version 1.2.0-dev
11
11
* @file dataTables.alphabetSearch.js
12
12
* @author SpryMedia Ltd (www.sprymedia.co.uk)
13
13
* @contact www.sprymedia.co.uk/contact
19
19
* License MIT - http://datatables.net/license/mit
20
20
*
21
21
* For more detailed information please see:
22
- * http ://www.gyrocode.com/articles /jquery-datatables-alphabetical-search
22
+ * https ://www.gyrocode.com/projects /jquery-datatables-alphabetsearch/
23
23
*/
24
24
( function ( ) {
25
25
@@ -50,8 +50,8 @@ $.fn.dataTable.Api.register( 'alphabetSearch.recalc()', function ( searchTerm )
50
50
// Search plug-in
51
51
$ . fn . dataTable . ext . search . push ( function ( context , searchData ) {
52
52
// Ensure that table has alphabet search feature enabled
53
- if ( ! context . hasOwnProperty ( 'alphabetSearch' ) ) {
54
- return true ;
53
+ if ( ! context . hasOwnProperty ( 'alphabetSearch' ) ) {
54
+ return true ;
55
55
}
56
56
57
57
// Ensure that there is a search applied to this table before running it
@@ -166,25 +166,29 @@ function draw ( table, alphabet, context )
166
166
var columnData = table . column ( context . alphabetSearch . column , { search : 'applied' } ) . data ( ) ;
167
167
var bins = bin ( columnData ) ;
168
168
169
- $ ( '<span class="alphabet_letter' + ( ( ! context . alphabetSearch . letter ) ? ' active' : '' ) + '"/>' )
169
+ $ ( '<a/>' )
170
+ . attr ( 'href' , 'javascript:;' )
170
171
. data ( 'letter' , '' )
171
172
. data ( 'match-count' , columnData . length )
173
+ . addClass (
174
+ ( ( ! context . alphabetSearch . letter ) ? 'active' : '' )
175
+ )
172
176
. html ( context . oLanguage . alphabetSearch . infoAll )
173
177
. appendTo ( alphabet ) ;
174
178
175
179
for ( var i = 0 ; i < context . oLanguage . alphabetSearch . alphabet . length ; i ++ ) {
176
180
var letter = context . oLanguage . alphabetSearch . alphabet [ i ] ;
177
181
178
- $ ( '<span/>' )
182
+ $ ( '<a/>' )
183
+ . attr ( 'href' , 'javascript:;' )
179
184
. data ( 'letter' , letter )
180
185
. data ( 'match-count' , bins [ letter ] || 0 )
181
186
. addClass (
182
- 'alphabet_letter'
183
- + ( ! bins [ letter ] ? ' empty' : '' )
187
+ ( ! bins [ letter ] ? 'empty' : '' )
184
188
+ ( ( context . alphabetSearch . letter === letter ) ? ' active' : '' )
185
189
)
186
190
. html (
187
- ( letter === '#' ) ? '0-9' : letter
191
+ letter
188
192
)
189
193
. appendTo ( alphabet ) ;
190
194
}
@@ -299,7 +303,10 @@ $.fn.dataTable.AlphabetSearch = function ( context ) {
299
303
300
304
301
305
// Trigger a search
302
- alphabet . on ( 'click' , 'span.alphabet_letter' , function ( ) {
306
+ alphabet . on ( 'click' , 'a' , function ( e ) {
307
+ // Prevent default behavior
308
+ e . preventDefault ( ) ;
309
+
303
310
alphabet . find ( '.active' ) . removeClass ( 'active' ) ;
304
311
$ ( this ) . addClass ( 'active' ) ;
305
312
@@ -310,17 +317,23 @@ $.fn.dataTable.AlphabetSearch = function ( context ) {
310
317
311
318
// Mouse events to show helper information
312
319
alphabet
313
- . on ( 'mouseenter' , 'span.alphabet_letter' , function ( ) {
314
- alphabet
315
- . find ( 'div.alphabet_info' )
320
+ . on ( 'mouseenter' , 'a' , function ( ) {
321
+ var $el = $ ( this ) ;
322
+ var el_pos = $el . position ( ) ;
323
+
324
+ var $alphabet_info = $ ( '.alphabet_info' , alphabet ) ;
325
+
326
+ $alphabet_info . html ( $el . data ( 'match-count' ) ) ;
327
+
328
+ // Display helper centered horizontally under the letter
329
+ $alphabet_info
316
330
. css ( {
317
331
opacity : 1 ,
318
- left : $ ( this ) . position ( ) . left ,
319
- width : $ ( this ) . width ( )
320
- } )
321
- . html ( $ ( this ) . data ( 'match-count' ) ) ;
332
+ left : el_pos . left + Math . round ( ( $el . outerWidth ( ) - $alphabet_info . outerWidth ( ) ) / 2 ) ,
333
+ top : $ ( this ) . position ( ) . top + $el . outerHeight ( )
334
+ } ) ;
322
335
} )
323
- . on ( 'mouseleave' , 'span.alphabet_letter ' , function ( ) {
336
+ . on ( 'mouseleave' , 'a ' , function ( ) {
324
337
alphabet
325
338
. find ( 'div.alphabet_info' )
326
339
. css ( 'opacity' , 0 ) ;
@@ -349,7 +362,7 @@ $.fn.dataTable.AlphabetSearch = function ( context ) {
349
362
350
363
// If there are no rows found and letter is selected
351
364
if ( ! rows . length && context . alphabetSearch ) {
352
- var letter = ( context . alphabetSearch . letter === '#' ) ? '0-9' : context . alphabetSearch . letter ;
365
+ var letter = context . alphabetSearch . letter ;
353
366
354
367
$ ( api . table ( ) . body ( ) ) . prepend (
355
368
'<tr class="alphabet_group"><td colspan="' + col_total + '">' + letter + '</td></tr>'
0 commit comments