click sobre fila

click sobre fila

silenssilens Posts: 101Questions: 40Answers: 0

Hola, me gustaría saber como puedo usar esta funcion sobre mi funcion.

$(document).ready(function() {
var table = $('#example').DataTable();

$('#example tbody').on('click', 'tr', function () {
    var data = table.row( this ).data();
    alert( 'You clicked on '+data[0]+'\'s row' );
} );

} );

Esta es mi funcion:

$(document).ready(function() {
   var table= $('#tbl_entidad').DataTable( {
        "ajax": {
            "url": "php/identidades.php",
            "dataSrc": ""
        
        },
                
    
        "columns": [
            { "data": "id" },
            { "data": "cif" },
            { "data": "name" },
            { "data": "fch" }
           ]
           
    } );
} );

Gracias

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,844Questions: 85Answers: 408
    $(document).ready(function() {
       var table= $('#tbl_entidad').DataTable( {
            "ajax": {
                "url": "php/identidades.php",
                "dataSrc": ""
             
            },
                     
         
            "columns": [
                { "data": "id" },
                { "data": "cif" },
                { "data": "name" },
                { "data": "fch" }
               ]
                
        } );
        $('#tbl_entidad tbody').on('click', 'tr', function () {
            var data = table.row( this ).data();
            alert( 'You clicked on '+data[0]+'\'s row' );
        } );
    } );
    
  • silenssilens Posts: 101Questions: 40Answers: 0

    Hola otra vez, va usted a aprender a hablar Español. :)
    He puesto el codigo pero no funciona, pongo mi funcion y mi tabla.

    Mi tabla en archivo index.html:

        <table id="tbl_entidad" class="mdl-data-table" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Cif</th>
                            <th>Nombre</th>
                            <th>Fecha</th>
                        </tr>
                    </thead>
                    <tbody>
                    <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                        </tr>
                    </tbody>
                   </table>
    

    Este es mi código en js.

    function saldos(dni,saldos){
    
    
      tabla = $('#tbl_entidad').DataTable( {
           "destroy": "true", 
      
            "ajax": {
                "url": "php/identidades.php",
                 "type": "POST", 
                "dataSrc": "",
                "data": {cif:dni}, //En el data meto la variable cif con el contenido de dni, es formato json
            
            },
                    
            "columns": [
                { "data": "id" }, //Estas son las colunas del json
                { "data": "cif" },
                { "data": "name" },
                { "data": "fch" }
               ]
               
        } );
        
        
        
         $('#tbl_entidad tbody').on('click', 'tr', function () {
            var data = table.row( this ).data();
            alert( 'You clicked on '+data[0]+'\'s row' );
        } );
    
        
    }
    

    Pero no funciona, no se a que se debe, cuando pulso sobre la fila, no se selecciona ni cambia el color ni aparece el alert.

    Muchas gracias otra vez.

  • silenssilens Posts: 101Questions: 40Answers: 0
    edited August 2017

    Acabo de darme cuenta del error.
    No es table, mi variable es tabla.

    Tampoco es data[0],  es data[id] or data[name]
    

    Muchas gracias.

  • rf1234rf1234 Posts: 2,844Questions: 85Answers: 408
    Answer ✓

    Muy bien. Estoy encantado de poder ayudar un poquito. Good luck to you! It is a bit of try and error in the beginning but it gets better after a short while ...

This discussion has been closed.