简体   繁体   English

数据表和JEditable

[英]Datatables and JEditable

Just starting with php, datatables and jeditable, I can get my table to load and looks ok, when I click on a record in the table it lets me edit the record and I am getting data when I look at the dataOut.php file in chrome developer tools, but it is not passing the record identifier. 只是从php,datatables和jeditable入手,我可以加载我的表并看起来不错,当我单击表中的记录时,它可以编辑记录,而当我查看表中的dataOut.php文件时,我正在获取数据。 chrome开发人员工具,但未传递记录标识符。

 <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%"> <thead> <tr> <th>id</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th>Name</th> <th>Role Code</th> </tr> </thead> <tbody> <?php $sql = "SELECT * FROM `users`"; foreach ($conn->query($sql)as $row){ echo '<tr>'; echo '<td>' . $row['id_user'] . '</td>'; echo '<td>' . $row['firstname'] . '</td>'; echo '<td>' . $row['lastname'] . '</td>'; echo '<td>' . $row['username'] . '</td>'; echo '<td>' . $row['realname'] . '</td>'; echo '<td>' . $row['role'] . '</td>'; echo '</tr>'; } ?> </tbody> <tfoot> <tr> <td>id</td> <td>iFirstname</td> <td>Lastname</td> <td>Username</td> <td>Name</td> <td>Role Code</td> </tr> </tfoot> </table> 

And when I change a surname from Lewis to Lane, here is the result in chrome developer 当我将姓氏从Lewis更改为Lane时,这是chrome开发人员的结果

array(4) { ["value"]=> string(4) "Lane" ["id"]=> string(0) "" ["row_id"]=> string(0) "" ["column"]=> string(1) "2" } array(4){[“” value“] => string(4)” Lane“ [” id“] => string(0)”“ [” row_id“] => string(0)”“ [” column“] =>字符串(1)“ 2”}

I'm not sure I understand your question but I noticed an error, and I suspect this could be your problem. 我不确定我是否理解您的问题,但是我注意到一个错误,我怀疑这可能是您的问题。

In your foreach loop you're looping through $conn->query() which is a mysqli_result object. 在您的foreach循环中,您正在循环通过mysqli_result对象的$conn->query() You need to call the fetch_assoc method. 您需要调用fetch_assoc方法。 Additionally, this needs to be done through a while loop instead of a foreach loop. 另外,这需要通过while循环而不是foreach循环来完成。 This is because fetch_assoc is function that gets the next row that matches your query, rather than returning one big array with all of the matching rows. 这是因为fetch_assoc是获取与查询匹配的下一行的函数,而不是返回包含所有匹配行的大数组。 When there are no more rows, it returns null and therefore ends the while loop. 当没有更多的行时,它返回null并因此结束while循环。 Try this: 尝试这个:

 <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%"> <thead> <tr> <th>id</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th>Name</th> <th>Role Code</th> </tr> </thead> <tbody> <?php $sql = "SELECT * FROM `users`"; while($row = ($conn->query($sql))->fetch_assoc()) { echo '<tr>'; echo '<td>' . $row['id_user'] . '</td>'; echo '<td>' . $row['firstname'] . '</td>'; echo '<td>' . $row['lastname'] . '</td>'; echo '<td>' . $row['username'] . '</td>'; echo '<td>' . $row['realname'] . '</td>'; echo '<td>' . $row['role'] . '</td>'; echo '</tr>'; } ?> </tbody> <tfoot> <tr> <td>id</td> <td>iFirstname</td> <td>Lastname</td> <td>Username</td> <td>Name</td> <td>Role Code</td> </tr> </tfoot> </table> 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM