Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/10/2009, 10:36
Avatar de netzky
netzky
 
Fecha de Ingreso: mayo-2007
Mensajes: 56
Antigüedad: 17 años, 8 meses
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