Created
February 2, 2014 03:10
-
-
Save victorvhpg/8762545 to your computer and use it in GitHub Desktop.
paginacao de table
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
var Paginacao = (function($) { | |
"use strict"; | |
var Paginacao = function() { | |
this.qtdRegistroPorPagina = 10; | |
this.qtdDePaginacao = 7; | |
this.template = ["<ul data-pg=\"container\" class=\"pagination\" > ", | |
"<li data-pg=\"anterior\" ><a href=\"javascript:;\">«</a></li>", | |
"<li data-pg=\"numeros\" > <a href=\"javascript:;\"> {@numero} </a> </li>", | |
"<li data-pg=\"proximo\" ><a href=\"javascript:;\">»</a></li> ", | |
"</ul>" | |
].join(""); | |
this.$tabela = null; | |
this.$navegacao = null; | |
this.$numeros = null; | |
this.paginaAtual = 0; | |
this.$btnAnterior = null; | |
this.$btnProximo = null; | |
}; | |
Paginacao.paginar = function($tabela) { | |
var p = null; | |
if (!$tabela.data("paginacao")) { | |
p = (new Paginacao()).init($tabela); | |
$tabela.data("paginacao", p); | |
} else { | |
p = $tabela.data("paginacao"); | |
p.posicionar(0); | |
// $tabela.data("$nav").remove(); | |
} | |
return p; | |
}; | |
Paginacao.prototype = { | |
posicionar: function(pagina) { | |
var that, totalDeRegistros, totalDePaginas, | |
metade, ini, fim, linhaIni, linhaFinal, $ativos; | |
that = this; | |
totalDeRegistros = this.$tabela.find("tbody tr").length; | |
if (totalDeRegistros === 0) { | |
this.$tabela.data("$nav").hide(); | |
} else { | |
this.$tabela.data("$nav").show(); | |
} | |
totalDePaginas = Math.ceil(totalDeRegistros / this.qtdRegistroPorPagina); | |
if (pagina < 0) { | |
pagina = 0; | |
} else if (pagina >= totalDePaginas) { | |
pagina = totalDePaginas - 1; | |
} | |
metade = ~~ (this.qtdDePaginacao / 2); | |
ini = Math.max(pagina - metade, 0); | |
fim = Math.min(ini + this.qtdDePaginacao, totalDePaginas); | |
if (fim - ini < totalDePaginas) { | |
ini = Math.max(fim - this.qtdDePaginacao, 0); | |
} | |
this.$navegacao.find("[data-pg='numeros']").remove(); | |
for (var i = ini; i < fim; i++) { | |
(function(i) { | |
var $n = that.$numeros.clone(); | |
if (pagina === i) { | |
$n.addClass("active"); | |
} | |
$n.html($n.html().replace("{@numero}", i + 1)); | |
that.$btnProximo.before($n); | |
$n.on("click", function() { | |
that.posicionar(i); | |
}); | |
})(i); | |
} | |
linhaIni = this.qtdRegistroPorPagina * pagina; | |
linhaFinal = linhaIni + this.qtdRegistroPorPagina; | |
$ativos = this.$tabela.find("tbody tr").hide().filter(function(indice) { | |
return (indice >= linhaIni && indice < linhaFinal); | |
}).show(); | |
that.$navegacao.parent().css("width", that.$tabela.width() + "px"); | |
$ativos.css("opacity", 0).animate({ | |
"opacity": 1 | |
}); | |
this.paginaAtual = pagina; | |
}, | |
init: function($tabela) { | |
var that = this; | |
this.$navegacao = $(this.template); | |
this.$numeros = this.$navegacao.find("[data-pg='numeros']").remove().clone(true, true); | |
this.$btnProximo = this.$navegacao.find("[data-pg='proximo']"); | |
this.$btnAnterior = this.$navegacao.find("[data-pg='anterior']"); | |
var $nav = $("<div />", { | |
css: { | |
width: $tabela.width() + "px", | |
"text-align": "center", | |
overflow: "hidden", | |
margin: "0 auto" | |
} | |
}).append(this.$navegacao); | |
$tabela.after($nav); | |
$tabela.data("$nav", $nav); | |
this.$tabela = $tabela; | |
this.$btnProximo.on("click", function() { | |
that.posicionar(that.paginaAtual + 1); | |
return false; | |
}); | |
this.$btnAnterior.on("click", function() { | |
that.posicionar(that.paginaAtual - 1); | |
return false; | |
}); | |
this.posicionar(0); | |
return this; | |
} | |
}; | |
return Paginacao; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment