1/* 2 * $Id: ntg_datatables_filtros.js 809 2017-03-17 22:13:27Z hrodrigu $ 3 * written by Hrodrigu 4 * http://www.newtenberg.com 5 * GPL (GPL-LICENSE.txt) licenses. 6 * Built for jQuery library 7 * http://jquery.com 8 * Built for DataTables 9 * https://datatables.net/ 10 */ 11(function($){ 12 $.fn.ntg_datatables_filtros = function(options){ 13 var params = []; 14 for(var i=1; i<arguments.length;i++){ 15 params.push(arguments[i]); 16 } 17 var defaults = { 18 dataTablesOptions:{ 19 "language":{ 20 "sProcessing": "Procesando...", 21 "sLengthMenu": "Mostrar _MENU_ registros", 22 "sZeroRecords": "No se encontraron resultados", 23 "sEmptyTable": "Ningún dato disponible en esta tabla", 24 "sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros", 25 "sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros", 26 "sInfoFiltered": "(filtrado de un total de _MAX_ registros)", 27 "sInfoPostFix": "", 28 "sSearch": "Buscar:", 29 "sUrl": "", 30 "sInfoThousands": ",", 31 "sLoadingRecords": "Cargando...", 32 "oPaginate": { 33 "sFirst": "Primero", 34 "sLast": "Ãltimo", 35 "sNext": "Siguiente", 36 "sPrevious": "Anterior" 37 }, 38 "oAria": { 39 "sSortAscending": ": Activar para ordenar la columna de manera ascendente", 40 "sSortDescending": ": Activar para ordenar la columna de manera descendente" 41 } 42 }, 43 "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "Todos"] ], 44 "dom": "<'row'<'col-sm-6'l><'col-sm-4'r><'col-sm-2'B>>" + 45 "<'row'<'col-sm-5'i><'col-sm-7'p>>"+ 46 "<'row'<'col-sm-12'tr>>" , 47 "buttons": [ 48 "excel" 49 ] 50 }, 51 useForm:false, 52 submitButton:"" , // id del botón enviar cuando se usa formulario 53 clearButton:"" , // id del botón enviar cuando se usa formulario 54 filterColumns:[ 55 /* 56 { 57 id:"col0", 58 type:"select", 59 defaultValue:"", 60 noFilterLabel:"", 61 order:"desc", 62 filterType:"exact" 63 }, 64 { 65 id:"col1", 66 type:"input", 67 defaultValue:"", 68 noFilterLabel:"ingrese los términos de búsqueda", 69 filterType:"phrase" 70 }, 71 { 72 id:"col2", 73 type:"select", 74 defaultValue:"", 75 noFilterLabel:"*", 76 filterType:"or", 77 order:"asc", 78 wordSeparator:"" 79 }, 80 { 81 id:"col3", 82 type:"input", 83 defaultValue:"", 84 noFilterLabel:"ingrese los términos de búsqueda", 85 filterType:"and" 86 }, 87 */ 88 { 89 type:"none" 90 } 91 ], 92 debug:false 93 }; 94 var tr= {"%C3%A1":"a", "%C3%A9":"e", "%C3%AD":"i", "%C3%B3":"o", "%C3%BA":"u"}; 95 if (typeof(options) ==='object' || typeof(options) ==='undefined' ){ 96 return this.each(function(){ 97 var obj = {}; 98 obj.dom = this; 99 obj.opts = $.extend(true,defaults, options); 100 obj.init = function(force){ 101 if (this.nodeName != "TABLE" && typeof($(obj.dom).data("NTG_DATATABLES_FILTROS_INSTANCE")) != "undefined" && typeof(force) == "undefined"){ 102 return; 103 } 104 //init plugin 105 debug("init ntg_datatables_filtros"); 106 obj.coldef = obj.opts.filterColumns.slice(); 107 for(var i=0; i < obj.coldef.length;i++){ 108 if(obj.coldef[i].type == "select" ){ 109 if("undefined" != typeof(obj.coldef[i].values) && $.isArray(obj.coldef[i].values)){ 110 obj.coldef[i].predefvals = true; 111 }else{ 112 obj.coldef[i].predefvals = false; 113 obj.coldef[i].values={}; 114 } 115 } 116 } 117 var filas = $("tbody > tr",this.dom).each(function(){ 118 $("td",this).each(function(i,el){ 119 if(i < obj.coldef.length && obj.coldef[i].type == "select"){ 120 if( !obj.coldef[i].predefvals){ 121 // identificar si contiene info de clasificandos 122 var estilo = " " + $("*[class]",this) 123 .filter(function(){ 124 return $(this).attr("class").indexOf("pnid-") != -1; 125 }).attr("class"); 126 var pnid= (estilo.search(/pnid-(\d*)/)!= -1 ? estilo.match(/pnid-(\d*)/)[1] : undefined); 127 var pvid= (estilo.search(/pvid-(\d*)/)!= -1 ? estilo.match(/pvid-(\d*)/)[1] : undefined); 128 var pid = (estilo.search(/pid-(\d*)/)!= -1 ? estilo.match(/pv-pid-(\d*)/)[1] : undefined); 129 var texto = $(this).text().trim(); 130 if(texto.length > 0){ 131 if(typeof(obj.coldef[i].wordSeparator) != "undefined" && 132 obj.coldef[i].wordSeparator.length>0 && 133 texto.indexOf(obj.coldef[i].wordSeparator) != -1){ 134 var palabras = texto.split(obj.coldef[i].wordSeparator); 135 for(var j=0;j<palabras.length;j++){ 136 obj.coldef[i].values[palabras[j].trim()]={}; 137 obj.coldef[i].values[palabras[j].trim()].selected = 1; 138 if("undefined" != typeof(pnid)){ 139 obj.coldef[i].values[palabras[j].trim()].pnid = pnid; 140 obj.coldef[i].values[palabras[j].trim()].pvid = pvid; 141 obj.coldef[i].values[palabras[j].trim()].pid = pid; 142 } 143 } 144 }else{ 145 obj.coldef[i].values[texto]={}; 146 obj.coldef[i].values[texto].selected = 1 ; 147 if("undefined" != typeof(pnid)){ 148 obj.coldef[i].values[texto].pnid = pnid; 149 obj.coldef[i].values[texto].pvid = pvid; 150 obj.coldef[i].values[texto].pid = pid; 151 } 152 } 153 } 154 } 155 } 156 }); 157 }); 158 debug("init ntg_datatables_filtros filas="+filas.length); 159 var tr = $("<tr></tr>"); 160 for(var i=0; i < obj.coldef.length;i++){ 161 var estilo = $("thead > tr > th:eq("+i+")",this.dom).attr("class"); 162 estilo = ("undefined" == typeof(estilo)?'':estilo.trim()); 163 obj.coldef[i].className = estilo; 164 obj.coldef[i].filter = ("undefined" != typeof(obj.coldef[i].defaultValue)? obj.coldef[i].defaultValue :''); 165 if(obj.coldef[i].type == "input"){ 166 if(obj.opts.useForm){ 167 obj.coldef[i].input = $("#"+obj.coldef[i].id) 168 .attr("placeholder",obj.coldef[i].noFilterLabel); 169 }else{ 170 obj.coldef[i].input = $("<input id='"+obj.coldef[i].id+"' type='text' class='filtro_input' placeholder='"+obj.coldef[i].noFilterLabel+"' \>"); 171 } 172 $(obj.coldef[i].input) 173 .data("NTG_COL_NUMBER",i) 174 .data("NTG_INSTANCE",obj) 175 .keyup(function(ev){ 176 var _this_instance = $(ev.target).data("NTG_INSTANCE") 177 _this_instance.coldef[$(ev.target).data("NTG_COL_NUMBER")].filter = $(this).val(); 178 if(!_this_instance.opts.useForm){ 179 _this_instance.table.draw(); 180 } 181 }); 182 } else if (obj.coldef[i].type == "select"){ 183 if(obj.opts.useForm){ 184 obj.coldef[i].input = $("#"+obj.coldef[i].id); 185 $(obj.coldef[i].input).children().remove(); 186 }else{ 187 obj.coldef[i].input = $("<select class='filtro_select' id='" +obj.coldef[i].id+"'></select>"); 188 } 189 var optiondefault = $("<option value=''>"+obj.coldef[i].noFilterLabel+"</option>") 190 .data("NTG_DATA",{pvid:0,pid:0}); 191 $(obj.coldef[i].input) 192 .data("NTG_COL_NUMBER",i) 193 .data("NTG_INSTANCE",obj) 194 .change(function(ev){ 195 var _this_instance = $(ev.target).data("NTG_INSTANCE") 196 _this_instance.coldef[$(ev.target).data("NTG_COL_NUMBER")].filter = $(this).val(); 197 if(!_this_instance.opts.useForm){ 198 _this_instance.table.draw(); 199 } 200 }) 201 .append(optiondefault); 202 if (!obj.coldef[i].predefvals){ 203 var palabras = []; 204 for (var palabra in obj.coldef[i].values){ 205 if(palabra.trim().length > 0 && obj.coldef[i].values.hasOwnProperty(palabra)){ 206 palabras.push({"w":palabra, "d":obj.coldef[i].values[palabra]}); 207 } 208 } 209 palabras.sort(function(a, b){ 210 var x = a.w.toLowerCase(),y = b.w.toLowerCase(); 211 return x<y ? -1 : x>y ? 1 : 0 ; 212 }); 213 if( obj.coldef[i].order == "desc"){ 214 palabras.reverse(); 215 } 216 for (var j=0; j< palabras.length;j++){ 217 var selected = ""; 218 if( palabras[j].selected == obj.coldef[i].filter ){ 219 selected = "selected='selected'"; 220 } 221 $("<option "+selected+" value='"+palabras[j].w+"'>"+palabras[j].w+"</option>") 222 .data("NTG_DATA",palabras[j].d) 223 .appendTo(obj.coldef[i].input); 224 } 225 }else{ 226 for (var j=0; j< obj.coldef[i].values.length;j++){ 227 var selected = ""; 228 if( obj.coldef[i].values[j] == obj.coldef[i].filter ){ 229 selected = "selected='selected'"; 230 } 231 $("<option "+selected+" value='"+obj.coldef[i].values[j]+"'>"+obj.coldef[i].values[j]+"</option>") 232 .appendTo(obj.coldef[i].input); 233 } 234 } 235 } 236 } // fin de for 237 if(!obj.opts.useForm){ 238 var th = $("<th></th>") 239 .addClass(estilo); 240 if (obj.defcol[i].type == "none"){ 241 $(th).text(" "); 242 }else{ 243 $(th).append(obj.coldef[i].input); 244 } 245 $(tr).append(th); 246 }else{ 247 $("#"+obj.opts.submitButton) 248 .data("NTG_INSTANCE",obj) 249 .click(function(){ 250 var _this_instance = $(this).data("NTG_INSTANCE") 251 _this_instance.table.draw(); 252 }); 253 $("#"+obj.opts.clearButton) 254 .data("NTG_INSTANCE",obj) 255 .click(function(){ 256 var _this_instance = $(this).data("NTG_INSTANCE") 257 for(var i=0; i < _this_instance.coldef.length;i++){
258 _this_instance.coldef[i].filter=""; 259 if( _this_instance.coldef[i].type == "input"){ 260 $( _this_instance.coldef[i].input).val(""); 261 } else if( _this_instance.coldef[i].type == "select"){ 262 $( _this_instance.coldef[i].input).val(""); 263 } 264 } 265 _this_instance.table.draw(); 266 }); 267 } 268 obj.table = $(obj.dom).DataTable(obj.opts.dataTablesOptions); 269 $.fn.dataTable.ext.search.push(obj.filter); 270 if(!obj.opts.useForm){ 271 $(tr).insertBefore("thead >tr", obj.dom); 272 } 273 }; 274 obj.hideTable = function(){ 275 $(obj.table.table().container()).hide(); 276 } 277 obj.showTable = function(){ 278 $(obj.table.table().container()).show(); 279 } 280 obj.getFiltros = function(){ 281 return obj.coldef; 282 }; 283 obj.filter = function( settings, data, dataIndex ) { 284 var ret = true; 285 var o = $(settings.oInstance).data("NTG_DATATABLES_FILTROS_INSTANCE"); 286 if(typeof(o) !== "undefined" && typeof(o.coldef) !== "undefined"){ 287 for(var i=0; i < o.coldef.length ;i++){ 288 if(o.coldef[i].type != "none" && o.coldef[i].filter.length > 0){ 289 var palabras = toASCII(o.coldef[i].filter.toLowerCase()); 290 var dato = toASCII(data[i].toLowerCase()); 291 if(o.coldef[i].filterType == "or"){ 292 var palabras_arr = palabras.split(" "); 293 var r = false; 294 for(var j=0; j<palabras_arr.length;j++){ 295 r = r || (dato.indexOf( palabras_arr[j] ) != -1); 296 } 297 ret = ret && r; 298 }else if(o.coldef[i].filterType == "and"){ 299 var palabras_arr = palabras.split(" "); 300 var r = true; 301 for(var j=0; j<palabras_arr.length;j++){ 302 r = r && (dato.indexOf( palabras_arr[j] ) != -1); 303 } 304 ret = ret && r; 305 }else if(o.coldef[i].filterType == "phrase"){ 306 ret = ret && (dato.indexOf(palabras) != -1); 307 }else{ 308 ret = ret && (dato == palabras); 309 } 310 } 311 } 312 } 313 return ret; 314 }; 315 // convención 316 // metodos externos 317 // obj.metodo = function(){}; 318 // metodos internos 319 // function interno(){} 320 function toASCII(txt){ 321 var tx= txt.split(""); 322 for(var i=0,l=tx.length ; i<l;i++){ 323 var enc= encodeURIComponent(tx[i]); 324 if(typeof(tr[enc]) !== "undefined"){ 325 tx[i] = tr[enc]; 326 } 327 } 328 return tx.join(""); 329 } 330 function debug(msg){ 331 if(obj.opts.debug && window.console){ 332 window.console.log(msg); 333 } 334 }; 335 obj.init(); 336 //se guarda la instancia el propio nodo DOM 337 $(this).data("NTG_DATATABLES_FILTROS_INSTANCE",obj); 338 obj.table.draw(); 339 }); 340 } else if(typeof(options)==='string'){ 341 ret = this; 342 this.each(function(){ 343 var instancia = $(this).data("NTG_DATATABLES_FILTROS_INSTANCE"); 344 if('undefined' != typeof(instancia) && 'function' === typeof(instancia[options])){ 345 ret = instancia[options].apply(instancia,params); 346 return false; 347 } 348 }); 349 return ret; 350 } else { 351 return this; 352 } 353 }; 354})(jQuery); 355
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.