You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Plugins/features/alphabetSearch/dataTables.alphabetSearch.m...

4 lines
5.8 KiB
JavaScript

/*! AlphabetSearch for DataTables v1.1.0
* 2014 SpryMedia Ltd - datatables.net/license
*/
(function(){$.fn.dataTable.Api.register("alphabetSearch()",function(searchTerm){this.iterator("table",function(settings){settings.alphabetSearch.letter=searchTerm;});return this;});$.fn.dataTable.Api.register("alphabetSearch.recalc()",function(searchTerm){this.iterator("table",function(settings){draw(new $.fn.dataTable.Api(settings),$("div.alphabet",this.table().container()),settings);});return this;});$.fn.dataTable.ext.search.push(function(settings,searchData){if(!settings.alphabetSearch.letterSearch){return true;}var letter=searchData[settings.alphabetSearch.column].toString().replace(/<.*?>/g,"").charAt(0).toUpperCase();if(settings.alphabetSearch.letterSearch!=="#"){if(letter===settings.alphabetSearch.letterSearch){return true;}}else{if(/\d/.test(letter)){return true;}}return false;});$.fn.dataTable.ext.order["alphabetSearch"]=function(settings,col){var order_col=this.api().order()[0][0];var order_method=this.api().order()[0][1];if(order_col!==settings.alphabetSearch.column){settings.alphabetSearch.pass=0;}var data=this.api().column(col,{order:"index"}).data().map(function(value,index){return(order_col===settings.alphabetSearch.column)?((!settings.alphabetSearch.pass)?"":((order_method==="asc")?value.charAt(0):String.fromCharCode(65535-value.charCodeAt(0)))):value.charAt(0);});if(order_col===settings.alphabetSearch.column){if(!settings.alphabetSearchPass){settings.alphabetSearch.pass=0;}settings.alphabetSearch.pass=(settings.alphabetSearch.pass+1)%2;}return data;};function bin(data){var letter,bins={};for(var i=0,ien=data.length;i<ien;i++){letter=data[i].toString().replace(/<.*?>/g,"").charAt(0).toUpperCase();if(/\d/.test(letter)){letter="#";}if(bins[letter]){bins[letter]++;}else{bins[letter]=1;}}return bins;}function draw(table,alphabet,settings){alphabet.empty();alphabet.append(settings.oLanguage.alphabetSearch.infoDisplay+": ");var columnData=table.column(settings.alphabetSearch.column,{search:"applied"}).data();var bins=bin(columnData);$('<span class="alphabet-clear'+((!settings.alphabetSearch.letter)?" active":"")+'"/>').data("letter","").data("match-count",columnData.length).html(settings.oLanguage.alphabetSearch.infoAll).appendTo(alphabet);for(var i=0;i<settings.oLanguage.alphabetSearch.alphabet.length;i++){var letter=settings.oLanguage.alphabetSearch.alphabet[i];$("<span/>").data("letter",letter).data("match-count",bins[letter]||0).addClass((!bins[letter]?"empty":"")+((settings.alphabetSearch.letter===letter)?" active":"")).html((letter==="#")?"0-9":letter).appendTo(alphabet);}$('<div class="alphabet_info"></div>').appendTo(alphabet);if(settings.alphabetSearch.letter){settings.alphabetSearch.letterSearch=settings.alphabetSearch.letter;table.draw();settings.alphabetSearch.letterSearch="";}table.one("search",function(e,settings){var api=new $.fn.dataTable.Api(settings);api.alphabetSearch.recalc();});}$.fn.dataTable.AlphabetSearch=function(settings){var table=new $.fn.dataTable.Api(settings);var alphabet=$('<div class="alphabet"/>');settings.oLanguage.alphabetSearch=$.extend({"alphabet":"#ABCDEFGHIJKLMNOPQRSTUVXYZ","infoDisplay":"Display","infoAll":"All"},((settings.oLanguage.alphabetSearch)?settings.oLanguage.alphabetSearch:{}));settings.oLanguage.alphabetSearch.alphabet.toUpperCase();settings.alphabetSearch=$.extend({column:0},$.isPlainObject(settings.oInit.alphabetSearch)?settings.oInit.alphabetSearch:{},{letter:"",letterSearch:"",pass:0});if(settings.alphabetSearch.column>=0&&settings.alphabetSearch.column<settings.aoColumns.length){settings.aoColumns[settings.alphabetSearch.column].sSortDataType="alphabetSearch";}if(settings.hasOwnProperty("aaSortingFixed")&&typeof settings.aaSortingFixed==="object"){if($.isArray(settings.aaSortingFixed)){if(settings.aaSortingFixed.length&&!$.isArray(settings.aaSortingFixed[0])){settings.aaSortingFixed=[[settings.alphabetSearch.column,"asc"],settings.aaSortingFixed];}else{settings.aaSortingFixed.unshift([settings.alphabetSearch.column,"asc"]);}}else{if(!settings.aaSortingFixed.hasOwnProperty("pre")){settings.aaSortingFixed.pre=[];}if(settings.aaSortingFixed.pre.length&&!$.isArray(settings.aaSortingFixed.pre[0])){settings.aaSortingFixed.pre=[[settings.alphabetSearch.column,"asc"],settings.aaSortingFixed.pre];}else{settings.aaSortingFixed.pre.unshift([settings.alphabetSearch.column,"asc"]);}}}else{settings.aaSortingFixed=[settings.alphabetSearch.column,"asc"];}draw(table,alphabet,settings);alphabet.on("click","span",function(){alphabet.find(".active").removeClass("active");$(this).addClass("active");table.alphabetSearch($(this).data("letter")).draw();});alphabet.on("mouseenter","span",function(){alphabet.find("div.alphabet_info").css({opacity:1,left:$(this).position().left,width:$(this).width()}).html($(this).data("match-count"));}).on("mouseleave","span",function(){alphabet.find("div.alphabet_info").css("opacity",0);});table.on("draw",function(e,settings){var api=new $.fn.dataTable.Api(settings);var col_total=api.columns().nodes().length;var rows=api.rows({page:"current"}).nodes();var group_last=null;api.column(settings.alphabetSearch.column,{page:"current"}).data().each(function(name,index){var group=name.charAt(0).toUpperCase();if(group_last!==group){$(rows).eq(index).before('<tr class="alphabet_group"><td colspan="'+col_total+'">'+group+"</td></tr>");group_last=group;}});if(!rows.length&&settings.alphabetSearch){var letter=(settings.alphabetSearch.letter==="#")?"0-9":settings.alphabetSearch.letter;$(api.table().body()).prepend('<tr class="alphabet_group"><td colspan="'+col_total+'">'+letter+"</td></tr>");}});this.node=function(){return alphabet;};};$.fn.DataTable.AlphabetSearch=$.fn.dataTable.AlphabetSearch;$.fn.dataTable.ext.feature.push({fnInit:function(settings){var search=new $.fn.dataTable.AlphabetSearch(settings);return search.node();},cFeature:"A"});}());