Last active
December 23, 2015 12:19
-
-
Save fastcodecoq/6634531 to your computer and use it in GitHub Desktop.
searcTable un plugin jquery para realizar busquedas/filtrado de datos en tablas html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($){ | |
| var _this; | |
| var btn_search; | |
| var temp; | |
| var rows; | |
| var evt = window.onTouch ? 'touch' : 'click'; // compatibilidad con móviles | |
| var data = { | |
| table : "#files", | |
| row_element : "tr", | |
| onSearchEnd: {}, | |
| onBeforeSearch: {}, | |
| btn_search: ".btn-search", | |
| strict : false, | |
| live : true, | |
| filter : "td:first" | |
| }; | |
| $.fn.tableSearch = function(_data) | |
| { | |
| if(data instanceof Object) | |
| $.extend(data, _data); | |
| _this = $(this); | |
| data.table = $(data.table); | |
| data.btn_search = $(data.btn_search) | |
| data.table.css({"transition" : "all 1s"}); | |
| data.table.css({"-webkit-transition" : "all 1s"}); | |
| data.table.css({"-moz-transition" : "all 1s"}); | |
| data.table.css({"-o-transition" : "all 1s"}); | |
| temp = data.btn_search.html(); | |
| rows = data.table.find(data.row_element); | |
| _this.attr("spellcheck","false"); | |
| if(data.live) | |
| liveOn(); | |
| submit(); | |
| listenSearch(); | |
| console.log("tableSearch Started"); | |
| } | |
| var search = function(isQuit){ | |
| var makeSearch = function( callback){ | |
| var table = data.table; | |
| table.html(""); | |
| var query =compileQuery( _this.val() ); | |
| var regexp = (!data.strict) ? new RegExp(query,"i") : new RegExp("^" + query + "$","i"); | |
| var results = new Array(); | |
| if(!isQuit) | |
| data.btn_search.html("<span> × </span>").addClass("quitSearch"); | |
| console.log(query); | |
| var x = 0; | |
| for(; x < rows.length; x++){ | |
| var name = $(rows[x]).find(data.filter).text(); | |
| if(regexp.test(name)) | |
| results.push(data.row_element + $(rows[x]).html() + data.row_element); | |
| } | |
| table.html( results.join("\n") ); | |
| if(callback instanceof Function) | |
| exec(data.onSearchEnd); | |
| } | |
| if(data.onBeforeSearch instanceof Function) | |
| exec(data.onBeforeSearch(makeSearch)); | |
| else if(!data.onSearchEnd instanceof Function) | |
| makeSearch(); | |
| else | |
| makeSearch(data.onSearchEnd); | |
| } | |
| var listenSearch = function(){ | |
| data.btn_search.off(evt).on(evt, function(e){ | |
| if(data.btn_search.hasClass("quitSearch")){ | |
| resetSearch(e); | |
| }else{ | |
| search(); | |
| } | |
| }); | |
| } | |
| var resetSearch = function(e){ | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| console.log(temp) | |
| _this.val(""); | |
| data.btn_search.html(temp).removeClass("quitSearch"); | |
| search(true); | |
| } | |
| var liveOn = function(){ | |
| _this.on("keydown", function(){ | |
| if(_this.val().split("").length > 1) | |
| search(); | |
| }); | |
| _this.on("keyup", function(e){ | |
| if(_this.val().split("").length === 0) | |
| resetSearch(e) | |
| }); | |
| } | |
| var submit = function(){ | |
| _this.parents("form:first").on("submit", function(e){ | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| search(); | |
| return false; | |
| }); | |
| } | |
| var exec = function(doo, callback){ | |
| if(!doo instanceof Function) | |
| return false; | |
| if(callback instanceof Function) | |
| doo(callback); | |
| else | |
| doo(); | |
| return true; | |
| } | |
| var compileQuery = function (query) { | |
| return (query + '').replace(/[\\"'+.-_]/g, '\\$&').replace(/\u0000/g, '\\0'); | |
| } | |
| })(jQuery); | |
| // USO | |
| // $("#search").tableSearch(); | |
| // HTML | |
| // <form method="POST"> <input type="text" id="search" /> <button class="btn-search">Buscar</button> </form> | |
| //@params | |
| /* | |
| { | |
| table : String [ "table.busqueda" || "li#busqueda" ] Selector de la tabla donde se hará la búsqueda, | |
| onBeforeSearch : Function [function(){ alert("busqueda iniciada") }] acción que hará antes de cada cada búsqueda) , | |
| onSearchEnd : Function [ function(){ alert("busqueda finalizada") }]acción que hará una vez finalice cada búsqueda, | |
| btn_search : String ["button.buscar" || "button#buscar"] Selector del botón de busqueda, | |
| strict : Boolean [Por defecto false] Esto indica si debe buscarse estrictamente la palabra, es decir, que inicie y acabe tal cual como el query, y sea sensible | |
| live : Boolean [Por defecto true] Esto indica si la busqueda se hará cada vez que el usuario teclee un carácter | |
| filter: String [ "td:first" || "td.name" || td.category ... ] Es el selector para el campo en el cual realizará la busqueda el script | |
| row_element : String ["tbody tr" || "ul li.row "] Es el selector para el elemento que será tomado como fila | |
| } | |
| llamado con parametros personalizados | |
| var params = { | |
| table : "#usurios", | |
| onBeforeSearch : function(){ console.log("Busqueda inciada") }, | |
| strict : true, | |
| live : false, | |
| } | |
| USO | |
| $("input#search").taleSearch( params ); -> donde input#search es el campo donde se realiza la busqueda | |
| */ | |
| // HAPPY CODING | |
| /* | |
| LICENCIA | |
| copyrights @gomosoft | |
| This program is free software: you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation, either version 3 of the License, or | |
| (at your option) any later version. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment