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.7 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(context){context.alphabetSearch.letter=searchTerm;});return this;});$.fn.dataTable.Api.register("alphabetSearch.recalc()",function(searchTerm){this.iterator("table",function(context){draw(new $.fn.dataTable.Api(context),$("div.alphabet",this.table().container()),context);});return this;});$.fn.dataTable.ext.search.push(function(context,searchData){if(!context.alphabetSearch.letterSearch){return true;}var letter=searchData[context.alphabetSearch.column].toString().replace(/<.*?>/g,"").charAt(0).toUpperCase();if(context.alphabetSearch.letterSearch!=="#"){if(letter===context.alphabetSearch.letterSearch){return true;}}else{if(/\d/.test(letter)){return true;}}return false;});$.fn.dataTable.ext.order["alphabetSearch"]=function(context,col){var order_col=this.api().order()[0][0];var order_method=this.api().order()[0][1];if(order_col!==context.alphabetSearch.column){context.alphabetSearch.pass=0;}var data=this.api().column(col,{order:"index"}).data().map(function(value,index){return(order_col===context.alphabetSearch.column)?((!context.alphabetSearch.pass)?"":((order_method==="asc")?value.charAt(0):String.fromCharCode(65535-value.charCodeAt(0)))):value.charAt(0);});if(order_col===context.alphabetSearch.column){if(!context.alphabetSearchPass){context.alphabetSearch.pass=0;}context.alphabetSearch.pass=(context.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,context){alphabet.empty();alphabet.append(context.oLanguage.alphabetSearch.infoDisplay+": ");var columnData=table.column(context.alphabetSearch.column,{search:"applied"}).data();var bins=bin(columnData);$('<span class="alphabet-clear'+((!context.alphabetSearch.letter)?" active":"")+'"/>').data("letter","").data("match-count",columnData.length).html(context.oLanguage.alphabetSearch.infoAll).appendTo(alphabet);for(var i=0;i<context.oLanguage.alphabetSearch.alphabet.length;i++){var letter=context.oLanguage.alphabetSearch.alphabet[i];$("<span/>").data("letter",letter).data("match-count",bins[letter]||0).addClass((!bins[letter]?"empty":"")+((context.alphabetSearch.letter===letter)?" active":"")).html((letter==="#")?"0-9":letter).appendTo(alphabet);}$('<div class="alphabet_info"></div>').appendTo(alphabet);if(context.alphabetSearch.letter){context.alphabetSearch.letterSearch=context.alphabetSearch.letter;table.draw();context.alphabetSearch.letterSearch="";}table.one("search",function(e,context){var api=new $.fn.dataTable.Api(context);api.alphabetSearch.recalc();});}$.fn.dataTable.AlphabetSearch=function(context){var table=new $.fn.dataTable.Api(context);var alphabet=$('<div class="alphabet"/>');context.oLanguage.alphabetSearch=$.extend({"alphabet":"#ABCDEFGHIJKLMNOPQRSTUVXYZ","infoDisplay":"Display","infoAll":"All"},((context.oLanguage.alphabetSearch)?context.oLanguage.alphabetSearch:{}));context.oLanguage.alphabetSearch.alphabet.toUpperCase();context.alphabetSearch=$.extend({column:0},$.isPlainObject(context.oInit.alphabetSearch)?context.oInit.alphabetSearch:{},{letter:"",letterSearch:"",pass:0});if(context.alphabetSearch.column>=0&&context.alphabetSearch.column<context.aoColumns.length){context.aoColumns[context.alphabetSearch.column].sSortDataType="alphabetSearch";}if(context.hasOwnProperty("aaSortingFixed")&&typeof context.aaSortingFixed==="object"){if($.isArray(context.aaSortingFixed)){if(context.aaSortingFixed.length&&!$.isArray(context.aaSortingFixed[0])){context.aaSortingFixed=[[context.alphabetSearch.column,"asc"],context.aaSortingFixed];}else{context.aaSortingFixed.unshift([context.alphabetSearch.column,"asc"]);}}else{if(!context.aaSortingFixed.hasOwnProperty("pre")){context.aaSortingFixed.pre=[];}if(context.aaSortingFixed.pre.length&&!$.isArray(context.aaSortingFixed.pre[0])){context.aaSortingFixed.pre=[[context.alphabetSearch.column,"asc"],context.aaSortingFixed.pre];}else{context.aaSortingFixed.pre.unshift([context.alphabetSearch.column,"asc"]);}}}else{context.aaSortingFixed=[context.alphabetSearch.column,"asc"];}draw(table,alphabet,context);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,context){var api=new $.fn.dataTable.Api(context);var col_total=api.columns().nodes().length;var rows=api.rows({page:"current"}).nodes();var group_last=null;api.column(context.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&&context.alphabetSearch){var letter=(context.alphabetSearch.letter==="#")?"0-9":context.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"});}());