【问题标题】:Datatables and JEditable数据表和 JEditable
【发布时间】:2016-12-02 04:19:01
【问题描述】:

刚从 php、datatables 和 jeditable 开始,我可以让我的表加载并且看起来不错,当我单击表中的记录时,它可以让我编辑记录,当我查看 dataOut 时我正在获取数据。 chrome开发者工具中的php文件,但它没有传递记录标识符。

<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>

当我将姓氏从 Lewis 更改为 Lane 时,这是 chrome developer 中的结果

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

【问题讨论】:

    标签: php jquery datatables jeditable


    【解决方案1】:

    我不确定我是否理解您的问题,但我注意到一个错误,我怀疑这可能是您的问题。

    在您的foreach 循环中,您正在循环通过$conn-&gt;query() 这是一个mysqli_result 对象。您需要调用fetch_assoc 方法。此外,这需要通过while 循环而不是foreach 循环来完成。这是因为fetch_assoc 是获取与您的查询匹配的下一行的函数,而不是返回一个包含所有匹配行的大数组。当没有更多行时,它返回 null 并因此结束 while 循环。试试这个:

    <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

    【讨论】:

    • 谢谢,这给了我更多的数据,我想我的问题是返回的数据不包括与数据库记录相关的主键。返回数据:array(4) { ["value"]=> string(10) "Peter.Lane" ["id"]=> string(0) "" ["row_id"]=> string(0) "" ["column"]=> 字符串(1) "3" }
    • 知道了,我的错误:更改了这一行:“row_id”:this.parentNode.getAttribute('id_user'),改为“row_id”:this.parentNode.getAttribute('id'),和将标识符放在表目标 id 的 标记中。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多