0

I have a php file with a table getted from dataTable.net

I load my table and content with this code:

<div class="table-responsive">
     <div class="panel-responsive">
        <div class="panel-heading" align="center"><h3>Usuarios</h3></div>
    <table id="example" class="display" cellspacing="0" width="100%">

        <?php 
        //require_once('funciones.php');
        //conectar('localhost', 'root', '', 'agroapp');
        $result = mysql_query("SELECT * FROM usuario WHERE username <> 'admin'"); 

  if ($row = mysql_fetch_array($result)){ 
  ?>
  <thead>
  <tr> 
  <th>Username</th>
  </tr>
  </thead>
<tfoot>
        <tr>
            <th>Username</th>
        </tr>
         </tfoot>

 <tbody>

 <?php
  do { 
    $titulo=$row['nombre'];
    $post = $row['username'];
    $rol = $row['rol'];
    $idUser = $row['idUsuario'];

        echo "<tr > \n"; 
          //if($row["username"]!="admin"){
        //echo "<td >  </td> \n";
        echo "<td ><img src= ".$row["ruta"]." height='30' width='30 class=profile-photo img-circle'>  ".$row["username"]." </td> \n";


        echo "</tr> \n"; 
        //}
     } while ($row = mysql_fetch_array($result)); 
       //echo "</tbody></table> \n"; 
} else { 
    echo '<h5 align="center">¡ No tiene empleados añadidos! </h5>'; 
} 
 ?>

</tbody>
</table>
</div>
</div>

I'm add a selected when I click into a row with the next code, but I can't get the value username, because I get in the php file called from jquery a [object object] or undefined. This is getted when use :

 myFunction(table.row('.selected').value); 

And I try also use table.row('.selected').eq(0).data('username'), and value('username')... But I can't get the value.

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

$('#example tbody').on( 'click', 'tr', function () {
    if ( $(this).hasClass('selected') ) {
        $(this).removeClass('selected');
    }
    else {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
    }
} );

$('#button').click( function () {

    myFunction(table.row('.selected').eq(0).data('username'));  //Here I'm trying get the value
    table.row('.selected').remove().draw( false );
 } );
} );
function myFunction(val) {

var getted = val;

        $.ajax({
data: 'empleado=' + getted,
url: 'updateEmpleado.php',
method: 'POST', // or GET
success: function(msg) {
    //alert(msg);
    location.reload();
}
});
}

 </script>

How can I get the value of row selected? Thanks!

user3745888
  • 6,143
  • 15
  • 48
  • 97
  • When trying to get the value of a `` element, you can use: `document.getElementById ( "id-of-td" ).textContent` – ngray Dec 07 '15 at 17:22
  • 1
    Rows don't have any specific value. They've cells, i.e. `td` elements, which have `textContent` and `innerHTML` for example. – Teemu Dec 07 '15 at 17:23
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 07 '15 at 17:33

2 Answers2

0

Can u try with this:

var table = $('#example').DataTable();
var myData;
$('#example tbody').on( 'click', 'tr', function () {
    if ( $(this).hasClass('selected') ) {
        $(this).removeClass('selected');
    }
    else {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
    }
    myData = table.row( this ).data();
} );

$('#button').on('click', myFunction);

function myFunction(){
        var getted = myData.username;

        $.ajax({
           data: 'empleado=' + getted,
           url: 'updateEmpleado.php',
           method: 'POST', // or GET
           success: function(msg) {
              //alert(msg);
             location.reload();
           }
       });
       table.row('.selected').remove().draw( false );
}

Reference: https://datatables.net/reference/api/row().data()

Kiramishima
  • 192
  • 1
  • 7
0

You should listen to corresponding event (set event handler) and use API for data retrieval or get it directly from DOM.

https://datatables.net/reference/event/select

BI2O2T
  • 91
  • 5