Merge pull request #513 from PrzemyslawKlys/master

Enhances AlphabetSearch
pull/514/head
Allan Jardine 4 years ago committed by GitHub
commit 175f113d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,6 @@ div.alphabet span {
color: #3174c7; color: #3174c7;
cursor: pointer; cursor: pointer;
text-align: center; text-align: center;
width: 3.5%
} }
div.alphabet span:hover { div.alphabet span:hover {

@ -11,7 +11,7 @@
* @author SpryMedia Ltd (www.sprymedia.co.uk) * @author SpryMedia Ltd (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact * @contact www.sprymedia.co.uk/contact
* @copyright Copyright 2014 SpryMedia Ltd. * @copyright Copyright 2014 SpryMedia Ltd.
* *
* License MIT - http://datatables.net/license/mit * License MIT - http://datatables.net/license/mit
* *
* For more detailed information please see: * For more detailed information please see:
@ -49,9 +49,15 @@ $.fn.dataTable.ext.search.push( function ( context, searchData ) {
return true; return true;
} }
if ( searchData[0].charAt(0) === context.alphabetSearch ) { if ($.fn.dataTable.AlphabetSearch.caseSensitive) {
return true; if ( searchData[$.fn.dataTable.AlphabetSearch.column].charAt(0) === context.alphabetSearch ) {
} return true;
}
} else {
if ( searchData[$.fn.dataTable.AlphabetSearch.column].charAt(0).toUpperCase() === context.alphabetSearch ) {
return true;
}
}
return false; return false;
} ); } );
@ -62,11 +68,17 @@ function bin ( data ) {
var letter, bins = {}; var letter, bins = {};
for ( var i=0, ien=data.length ; i<ien ; i++ ) { for ( var i=0, ien=data.length ; i<ien ; i++ ) {
letter = data[i] if ($.fn.dataTable.AlphabetSearch.caseSensitive) {
.toString() letter = data[i]
.replace(/<.*?>/g, '') .toString()
.charAt(0).toUpperCase(); .replace(/<.*?>/g, '')
.charAt(0);
} else {
letter = data[i]
.toString()
.replace(/<.*?>/g, '')
.charAt(0).toUpperCase();
}
if ( bins[letter] ) { if ( bins[letter] ) {
bins[letter]++; bins[letter]++;
} }
@ -83,7 +95,7 @@ function draw ( table, alphabet )
alphabet.empty(); alphabet.empty();
alphabet.append( 'Search: ' ); alphabet.append( 'Search: ' );
var columnData = table.column(0).data(); var columnData = table.column($.fn.dataTable.AlphabetSearch.column).data();
var bins = bin( columnData ); var bins = bin( columnData );
$('<span class="clear active"/>') $('<span class="clear active"/>')
@ -92,6 +104,18 @@ function draw ( table, alphabet )
.html( 'None' ) .html( 'None' )
.appendTo( alphabet ); .appendTo( alphabet );
if ($.fn.dataTable.AlphabetSearch.addNumbers) {
for (var i = 0; i < 10; i++) {
var letter = String.fromCharCode(48 + i);
$('<span/>')
.data('letter', letter)
.data('match-count', bins[letter] || 0)
.addClass(!bins[letter] ? 'empty' : '')
.html(letter)
.appendTo(alphabet);
}
}
for ( var i=0 ; i<26 ; i++ ) { for ( var i=0 ; i<26 ; i++ ) {
var letter = String.fromCharCode( 65 + i ); var letter = String.fromCharCode( 65 + i );
@ -102,6 +126,18 @@ function draw ( table, alphabet )
.html( letter ) .html( letter )
.appendTo( alphabet ); .appendTo( alphabet );
} }
if ($.fn.dataTable.AlphabetSearch.caseSensitive === true) {
for (var i = 0; i < 26; i++) {
var letter = String.fromCharCode(97 + i);
$('<span/>')
.data('letter', letter)
.data('match-count', bins[letter] || 0)
.addClass(!bins[letter] ? 'empty' : '')
.html(letter)
.appendTo(alphabet);
}
}
$('<div class="alphabetInfo"></div>') $('<div class="alphabetInfo"></div>')
.appendTo( alphabet ); .appendTo( alphabet );
@ -149,7 +185,7 @@ $.fn.dataTable.AlphabetSearch = function ( context ) {
}; };
$.fn.DataTable.AlphabetSearch = $.fn.dataTable.AlphabetSearch; $.fn.DataTable.AlphabetSearch = $.fn.dataTable.AlphabetSearch;
$.fn.dataTable.AlphabetSearch.column = 0;
// Register a search plug-in // Register a search plug-in
$.fn.dataTable.ext.feature.push( { $.fn.dataTable.ext.feature.push( {

Loading…
Cancel
Save