Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/05/2007, 10:29
Avatar de derkenuke
derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: Problema al seleccionar checkboxes y pintar celdas.

Deberías colocar aquí código (x)HTML puro, sin PHP. Además no estaría mal simplificarlo: el CSS sólo hace que tener más texto que leer.

Un código que acabo de hacer te servirá para tu propósito supongo:

Código PHP:
<script type="text/javascript">


/******************
    FUNCION PARA COLOREAR CELDAS, FILAS O TABLAS (según tagPadre)
******************/
function colorea(celda,tagPadre,col) {
    
// subiremos un nivel en el DOM mientras el elemento actual no tenga el tag tagPadre
    
while( celda.nodeType==&& celda.tagName.toUpperCase()!=tagPadre.toUpperCase() )
        
celda=celda.parentNode;
    
celda.style.backgroundColor=col;
}

/******************
    FUNCION PARA MARCAR TODOS LOS CHECKBOXES CONTENIDOS EN UNA TABLA
******************/
function todosChkDeTabla(obj,check) {
    while( 
obj.nodeType==&& obj.tagName.toUpperCase()!="TABLE" )
        
obj=obj.parentNode;
    
//obj ahra es la TABLE sobre la que trabajamos
    
function recorrerHijos(elem) {
        if(
elem.nodeType==&& elem.tagName.toUpperCase()=="INPUT" && elem.type=="checkbox") {    // tipo ELEMENT_NODE y encima es checkbox
            
elem.checked=check;
            
colorea(elem,'TR'check?'#aaf':'transparent' );
        }
        else if (
elem.nodeType==&& elem.hasChildNodes() ) {        //ELEMENT_NODE y encima con hijos
            
for(var hijo=0hijo<elem.childNodes.lengthhijo++)        //recorremos cada hijo con la función, recursivamente
                
recorrerHijoselem.childNodes[hijo] );
        }
    }
    
recorrerHijos(obj);        //recorremos sus hijos y encontramos checkboxes para marcarlos
}

</script>



<table id="tabla" border="1">
    <tr>
        <td colspan="3"><input type="checkbox" onclick="todosChkDeTabla(this,this.checked);" /></td>
    </tr>
    <tr>
        <td><input type="checkbox" onclick="colorea(this,'TR', this.checked?'#aaf':'transparent' );" /></td>
        <td>loren</td>
        <td>ipsum</td>
    </tr>
    <tr>
        <td><input type="checkbox" onclick="colorea(this,'TR', this.checked?'#aaf':'transparent' );" /></td>
        <td>dolor</td>
        <td>sit</td>
    </tr>
    <tr>
        <td><input type="checkbox" onclick="colorea(this,'TR', this.checked?'#aaf':'transparent' );" /></td>
        <td>amet</td>
        <td>loren</td>
    </tr>
    <tr>
        <td><input type="checkbox" onclick="colorea(this,'TR', this.checked?'#aaf':'transparent' );" /></td>
        <td>ipsum</td>
        <td>dolor</td>
    </tr>
</table> 
Lo he intentado hacer lo más claro posible.


Saludos.
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.