2016-12-02 4 views
0

Je commence tout juste avec php, datatables et jeditable, je peux charger ma table et avoir l'air ok, quand je clique sur un enregistrement dans le tableau, il me permet d'éditer l'enregistrement et j'obtiens des données quand je regarde le fichier dataOut.php dans les outils de développement chrome, mais il ne transmet pas l'identificateur d'enregistrement.Datasables et JEditable

<script type="text/javascript"> 
 
    
 
$(document).ready(function() { 
 
    
 
$('#users').dataTable({ 
 
    }); 
 
    
 
    /* Init DataTables */ 
 
    var oTable = $('#users').dataTable(); 
 
     
 
    /* Apply the jEditable handlers to the table */ 
 
    oTable.$('td').editable('dataOut.php', { 
 
     "callback": function(sValue, y) { 
 
      var aPos = oTable.fnGetPosition(this); 
 
      oTable.fnUpdate(sValue, aPos[0], aPos[1]); 
 
      window.location.reload(); 
 
     }, 
 
     "submitdata": function (value, settings) { 
 
      return { 
 
       "row_id": this.parentNode.getAttribute('id_user'), 
 
       "column": oTable.fnGetPosition(this)[2] 
 
      } 
 
     }, 
 
     "height": "14px", 
 
     "width": "100%" 
 
    }); 
 
}); 
 
    
 
    
 
</script>
<table id="users" class="display" cellspacing="0" width="100%"> 
 
     \t \t \t <thead> 
 
      \t \t \t \t <tr> 
 
       \t \t \t \t <th>id</th> 
 
       \t \t \t \t <th>First Name</th> 
 
       \t \t \t <th>Last Name</th> 
 
       \t \t \t <th>Username</th> 
 
       \t \t \t <th>Name</th> 
 
       \t \t \t <th>Role Code</th> 
 
      \t \t \t </tr> 
 
     \t \t \t </thead> 
 
     \t <tbody> 
 
     \t <?php 
 
\t \t \t \t $sql = "SELECT * FROM `users`"; 
 
\t \t \t \t \t foreach ($conn->query($sql)as $row){ 
 
\t \t \t \t \t \t echo '<tr>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['id_user'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['firstname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['lastname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['username'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['realname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['role'] . '</td>'; 
 
\t \t \t \t \t \t echo '</tr>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t ?> 
 
\t \t \t \t \t </tbody> 
 
     \t \t \t <tfoot> 
 
      \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t <td>id</td> 
 
      \t \t \t \t \t <td>iFirstname</td> 
 
      \t \t \t \t \t <td>Lastname</td> 
 
      \t \t \t \t \t <td>Username</td> 
 
      \t \t \t \t \t <td>Name</td> 
 
      \t \t \t \t \t <td>Role Code</td> 
 
      \t \t \t </tr> 
 
     \t \t \t </tfoot> 
 
     \t \t \t 
 
\t \t \t </table>

Et quand je change un nom de famille de Lewis Lane, voici le résultat dans le révélateur chrome

tableau

(4) {[ "valeur"] chaîne => (4) "Lane" ["id"] => chaîne (0) "" ["row_id"] => chaîne (0) "" ["column"] => string (1) "2"}

Répondre

0

I Je ne suis pas sûr de comprendre votre question, mais j'ai remarqué une erreur et je suppose que cela pourrait être votre problème. Dans votre boucle foreach, vous parcourez $conn->query() qui est un objet mysqli_result. Vous devez appeler la méthode fetch_assoc. De plus, ceci doit être fait par une boucle while au lieu d'une boucle foreach. C'est parce que fetch_assoc est la fonction qui obtient la ligne suivante qui correspond à votre requête, plutôt que de retourner un grand tableau avec toutes les lignes correspondantes. Lorsqu'il n'y a plus de lignes, il renvoie null et termine donc la boucle while. Essayez ceci:

<script type="text/javascript"> 
 
    
 
$(document).ready(function() { 
 
    
 
$('#users').dataTable({ 
 
    }); 
 
    
 
    /* Init DataTables */ 
 
    var oTable = $('#users').dataTable(); 
 
     
 
    /* Apply the jEditable handlers to the table */ 
 
    oTable.$('td').editable('dataOut.php', { 
 
     "callback": function(sValue, y) { 
 
      var aPos = oTable.fnGetPosition(this); 
 
      oTable.fnUpdate(sValue, aPos[0], aPos[1]); 
 
      window.location.reload(); 
 
     }, 
 
     "submitdata": function (value, settings) { 
 
      return { 
 
       "row_id": this.parentNode.getAttribute('id_user'), 
 
       "column": oTable.fnGetPosition(this)[2] 
 
      } 
 
     }, 
 
     "height": "14px", 
 
     "width": "100%" 
 
    }); 
 
}); 
 
    
 
    
 
</script>
<table id="users" class="display" cellspacing="0" width="100%"> 
 
     \t \t \t <thead> 
 
      \t \t \t \t <tr> 
 
       \t \t \t \t <th>id</th> 
 
       \t \t \t \t <th>First Name</th> 
 
       \t \t \t <th>Last Name</th> 
 
       \t \t \t <th>Username</th> 
 
       \t \t \t <th>Name</th> 
 
       \t \t \t <th>Role Code</th> 
 
      \t \t \t </tr> 
 
     \t \t \t </thead> 
 
     \t <tbody> 
 
     \t <?php 
 
\t \t \t \t $sql = "SELECT * FROM `users`"; 
 
\t \t \t \t \t while($row = ($conn->query($sql))->fetch_assoc()) { 
 
\t \t \t \t \t \t echo '<tr>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['id_user'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['firstname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['lastname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['username'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['realname'] . '</td>'; 
 
\t \t \t \t \t \t \t echo '<td>' . $row['role'] . '</td>'; 
 
\t \t \t \t \t \t echo '</tr>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t ?> 
 
\t \t \t \t \t </tbody> 
 
     \t \t \t <tfoot> 
 
      \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t <td>id</td> 
 
      \t \t \t \t \t <td>iFirstname</td> 
 
      \t \t \t \t \t <td>Lastname</td> 
 
      \t \t \t \t \t <td>Username</td> 
 
      \t \t \t \t \t <td>Name</td> 
 
      \t \t \t \t \t <td>Role Code</td> 
 
      \t \t \t </tr> 
 
     \t \t \t </tfoot> 
 
     \t \t \t 
 
\t \t \t </table>

http://php.net/manual/en/mysqli-result.fetch-assoc.php

+0

Merci, qui me donne plus de données, je pense que mon problème est que les données renvoyées ne comprend pas la clé primaire relative à l'enregistrement de base de données. Données renvoyées: array (4) {["value"] => chaîne (10) "Peter.Lane" ["id"] => chaîne (0) "" ["row_id"] => chaîne (0) "" ["column"] => string (1) "3"} – DaveS

+0

Got it, mon erreur: Changé cette ligne: "row_id": this.parentNode.getAttribute ('id_user'), à "row_id": this.parentNode. getAttribute ('id') et placez un identificateur dans la balise dans l'ID de ciblage de table. Merci – DaveS