Merge pull request #514 from PrzemyslawKlys/master

Alphabet Search Enhancement - Defines parameters within options
pull/518/head
Allan Jardine 4 years ago committed by GitHub
commit 483f236955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,36 +49,44 @@ $.fn.dataTable.ext.search.push( function ( context, searchData ) {
return true;
}
if ($.fn.dataTable.AlphabetSearch.caseSensitive) {
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;
}
}
if (context.oInit.alphabet !== undefined) {
var columnId = (context.oInit.alphabet.column !== undefined) ? context.oInit.alphabet.column : 0
var caseSensitive = (context.oInit.alphabet.caseSensitive !== undefined) ? context.oInit.alphabet.caseSensitive : false
} else {
var columnId = 0
var caseSensitive = false
}
if (caseSensitive) {
if ( searchData[columnId].charAt(0) === context.alphabetSearch ) {
return true;
}
} else {
if ( searchData[columnId].charAt(0).toUpperCase() === context.alphabetSearch ) {
return true;
}
}
return false;
} );
// Private support methods
function bin ( data ) {
function bin ( data, options ) {
var letter, bins = {};
for ( var i=0, ien=data.length ; i<ien ; i++ ) {
if ($.fn.dataTable.AlphabetSearch.caseSensitive) {
letter = data[i]
.toString()
.replace(/<.*?>/g, '')
.charAt(0);
} else {
letter = data[i]
.toString()
.replace(/<.*?>/g, '')
.charAt(0).toUpperCase();
}
if (options.caseSensitive) {
letter = data[i]
.toString()
.replace(/<.*?>/g, '')
.charAt(0);
} else {
letter = data[i]
.toString()
.replace(/<.*?>/g, '')
.charAt(0).toUpperCase();
}
if ( bins[letter] ) {
bins[letter]++;
}
@ -90,13 +98,13 @@ function bin ( data ) {
return bins;
}
function draw ( table, alphabet )
function draw ( table, alphabet, options )
{
alphabet.empty();
alphabet.append( 'Search: ' );
var columnData = table.column($.fn.dataTable.AlphabetSearch.column).data();
var bins = bin( columnData );
var columnData = table.column(options.column).data();
var bins = bin( columnData, options );
$('<span class="clear active"/>')
.data( 'letter', '' )
@ -104,18 +112,18 @@ function draw ( table, alphabet )
.html( 'None' )
.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);
}
}
if (options.numbers) {
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++ ) {
var letter = String.fromCharCode( 65 + i );
@ -126,18 +134,18 @@ function draw ( table, alphabet )
.html( letter )
.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);
}
}
if (options.caseSensitive) {
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>')
.appendTo( alphabet );
@ -147,8 +155,13 @@ function draw ( table, alphabet )
$.fn.dataTable.AlphabetSearch = function ( context ) {
var table = new $.fn.dataTable.Api( context );
var alphabet = $('<div class="alphabet"/>');
var options = $.extend({
column: 0,
caseSensitive: false,
numbers: false
}, table.init().alphabet);
draw( table, alphabet );
draw( table, alphabet, options );
// Trigger a search
alphabet.on( 'click', 'span', function () {
@ -185,7 +198,6 @@ $.fn.dataTable.AlphabetSearch = function ( context ) {
};
$.fn.DataTable.AlphabetSearch = $.fn.dataTable.AlphabetSearch;
$.fn.dataTable.AlphabetSearch.column = 0;
// Register a search plug-in
$.fn.dataTable.ext.feature.push( {

Loading…
Cancel
Save