Foros del Web » Programando para Internet » Javascript »

modificar paginacion de tabla

Estas en el tema de modificar paginacion de tabla en el foro de Javascript en Foros del Web. Hola. Tengo el siguiente código para paginar el contenido de una tabla. Código: function paginador(tableName, itemsPerPage) { this.tableName = tableName; this.itemsPerPage = itemsPerPage; this.currentPage = ...
  #1 (permalink)  
Antiguo 20/10/2009, 00:22
Avatar de mj1984  
Fecha de Ingreso: septiembre-2008
Mensajes: 129
Antigüedad: 15 años, 8 meses
Puntos: 0
modificar paginacion de tabla

Hola. Tengo el siguiente código para paginar el contenido de una tabla.


Código:
function paginador(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;

    this.showRecords = function(from, to) {
        var rows = document.getElementById(tableName).rows;
        // Comienza desde 1 para no tener en cuenta la cabecera de la tabla
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }

    this.showPage = function(pageNumber) {
        if (! this.inited) {
            alert("not inited");
            return;
        }

        var oldPageAnchor = document.getElementById('pg' + this.currentPage);
        oldPageAnchor.className = 'pg-normal';

        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg' + this.currentPage);
        newPageAnchor.className = 'pg-seleccionado';

        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }

    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }

    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }

    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }

    this.showPageNav = function(pagerName, positionId) {
        if (! this.inited) {
            return;
        }
        var element = document.getElementById(positionId);

        var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> « Anterior </span> | ';
        for (var page = 1; page <= this.pages; page++)
        {
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';            
        }
        pagerHtml += '<span onclick="' + pagerName + '.next();" class="pg-normal"> Siguiente »</span>';
        
        element.innerHTML = pagerHtml;
    }
}
La paginación hace que bajo la tabla aparezcan los vínculos así:
<<Anterior | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Siguiente >>
En el caso de que hubiera 9 páginas. Me gustaría que:
- Si me sitúo en la página 1, me desaparezca el vínculo ‘Anterior’.
- Si me sitúo en la página 9 (última página), me desaparezca el vínculo de ‘Siguiente’.
- En lugar de aparecerme un vínculo para cada página, me aparezcan de 3 en 3, es decir, así:
<<Anterior | 1 | 2 | 3 | … Siguiente >> y que al pulsar sobre el 2 pues quedara así:
<<Anterior … | 2 | 3 | 4 | … Siguiente >>
Espero haberme explicado. A ver si alguien puede ayudarme!!!!
Gracias de antemano.

Última edición por mj1984; 20/10/2009 a las 00:37
  #2 (permalink)  
Antiguo 20/10/2009, 10:36
Avatar de netzky  
Fecha de Ingreso: mayo-2007
Mensajes: 56
Antigüedad: 17 años
Puntos: 1
Respuesta: modificar paginacion de tabla

Listo mi chavo aqui te va:

Funcion Modificada
Código:
<script type="text/javascript">
	function paginador(tableName, itemsPerPage) {
	    this.tableName = tableName;
	    this.itemsPerPage = itemsPerPage;
	    this.currentPage = 1;
	    this.pages = 0;
	    this.inited = false;
		this.pagerName = "";
		this.positionId = "";
				
	    this.showRecords = function(from, to) {
	        var rows = document.getElementById(tableName).rows;
	        // Comienza desde 1 para no tener en cuenta la cabecera de la tabla
	        for (var i = 1; i < rows.length; i++) {
	            if (i < from || i > to)
	                rows[i].style.display = 'none';
	            else
	                rows[i].style.display = '';
	        }
	    }
	
	    this.showPage = function(pageNumber) {
	        if (! this.inited) {
	            alert("not inited");
	            return;
	        }
	
	        var oldPageAnchor = document.getElementById('pg' + this.currentPage);
	        oldPageAnchor.className = 'pg-normal';
	
	        this.currentPage = pageNumber;
	        var newPageAnchor = document.getElementById('pg' + this.currentPage);
	        newPageAnchor.className = 'pg-seleccionado';
	
	        var from = (pageNumber - 1) * itemsPerPage + 1;
	        var to = from + itemsPerPage - 1;
	        this.showRecords(from, to);
	        this.showPageNav(this.pagerName, this.positionId);
	    }
	
	    this.prev = function() {
	        if (this.currentPage > 1)
	            this.showPage(this.currentPage - 1);	            
	    }
	
	    this.next = function() {
	        if (this.currentPage < this.pages) {
	            this.showPage(this.currentPage + 1);	            
	        }
	    }
	
	    this.init = function() {
	        var rows = document.getElementById(tableName).rows;
	        var records = (rows.length - 1);
	        this.pages = Math.ceil(records / itemsPerPage);
	        this.inited = true;
	    }
	
	    this.showPageNav = function(pagerName, positionId) {
	    	this.pagerName = pagerName;
			this.positionId = positionId;
			
	        if (! this.inited) {
	            return;
	        }
	        var element = document.getElementById(positionId);
			var pagerHtml = "";
			if (this.currentPage > 1){
	        	pagerHtml += '<span onclick="' + pagerName + '.prev();" class="pg-normal"> « Anterior </span> | ';
	        }
	        for (var page = 1; page <= this.pages; page++)
	        {
	            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';            
	        }
	        
			if (this.currentPage < this.pages){
	        	pagerHtml += '<span onclick="' + pagerName + '.next();" class="pg-normal"> Siguiente »</span>';
	        }
	        
	        element.innerHTML = pagerHtml;
	    }
	}
</script>
Codigo HTML EJEMPLO
Código HTML:
<table id="results">
    <tbody><tr>
        <th>#</th>
        <th>field</th>
    </tr>
    <tr style="">
        <td>1</td>
        <td><input type="text" value="rec1" name="field-name"/></td>
    </tr>
    <tr style="">
        <td>2</td>
        <td><input type="text" value="rec2" name="field-name"/></td>
    </tr>
    <tr style="">
        <td>3</td>
        <td><input type="text" value="rec3" name="field-name"/></td>
    </tr>
    <tr>
        <td>4</td>
        <td><input type="text" value="rec4" name="field-name"/></td>
    </tr>
    <tr>
        <td>5</td>
        <td><input type="text" value="rec5" name="field-name"/></td>
    </tr>
    <tr>
        <td>6</td>
        <td><input type="text" value="rec6" name="field-name"/></td>
    </tr>
    <tr>
        <td>7</td>
        <td><input type="text" value="rec7" name="field-name"/></td>
    </tr>
    <tr>
        <td>8</td>
        <td><input type="text" value="rec8" name="field-name"/></td>
    </tr>
    <tr>
        <td>9</td>
        <td><input type="text" value="rec9" name="field-name"/></td>
    </tr>
    <tr>
        <td>10</td>
        <td><input type="text" value="rec10" name="field-name"/></td>
    </tr>
</tbody></table>
<div id="pageNavPosition"></div> 
Codigo Javascript Iniciador
Código:
<script type="text/javascript"><!--
    var paginador = new paginador('results', 3); 
    paginador.init(); 
    paginador.showPageNav('paginador', 'pageNavPosition'); 
    paginador.showPage(1);
//--></script>
Espero te sea de utilidad, saludos....
__________________
Mess With The Best and Die Like The Rest
  #3 (permalink)  
Antiguo 22/05/2012, 00:56
 
Fecha de Ingreso: febrero-2012
Mensajes: 6
Antigüedad: 12 años, 2 meses
Puntos: 0
Respuesta: modificar paginacion de tabla

Yo estoy usando el mismo codigo para paginar resultados traidos de una consulta mysql, pero este codigo me despliega los resultados ordenados por lineas, es necesario crear un <TR> Y <TD> CONSULTA1 </TD> </TR> <TR> Y <TD> CONSULTA2 </TD> </TR> si yo tengo una tabla donde puedo usar tres celdas en una sola linea, no toma en cuenta la paginacion, hay alguien que pueda ayudarme por favor... gracias!
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:57.