簡體   English   中英

數據表默認排序(asc / desc)不起作用

[英]datatables default sort (asc/desc) not working

我試圖使用此將數據獲取到mysql表

$query=$conn->query("SELECT * FROM users ORDER BY id_user ASC");

這是我的表結構

使用者

id_user  | INT auto-increment
username | varchar
password | varchar
nama     | varchar
role     | varchar
status   | SET

但是顯示的數據順序按以下順序排列:1、10、11、12、13、2、3、4、5、6、7、8、9(數據庫中有13個數據)

編輯:顯示完整代碼

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
        <thead>
            <tr>
                <th align="center">ID</th>
                <th align="center">Username</th>
                <th align="center">Nama</th>
                <th align="center">Role</th>
                <th width="13%" align="center">Status</th>      
                <th width="7%" align="center">Edit</th> 
                <th width="8%" align="center">Hapus</th>
            </tr>
        </thead>
        <?php
        $query=$conn->query("SELECT * FROM users ORDER BY id_user ASC");
        while($row=$query->fetch()){
            $id=$row['id_user'];
            $name=$row['username'];
            $nama=$row['nama'];
            $role=$row['role'];
            $status=$row['status'];
        ?>  
        <tr>
            <td>
                &nbsp;<?php echo $id ;?>
            </td>
            <td>
                &nbsp;<?php echo $name ;?>
            </td>
            <td>
                &nbsp;<?php echo $nama ;?>
            </td>
            <td>
                &nbsp;<?php echo $role ;?>
            </td>
            <td>
                &nbsp;<?php echo $status ;?>
            </td>
            <td>
                <a href="edituser.php?id=<?php echo $id;?>"><button class="alert-success">Edit</button></a>
            </td>
            <td>
              <a href="deleteuser.php?id=<?php echo $id;?>&role=<?php echo $role;?>" onclick="return confirm('Apa anda yakin akan menghapus user ini?');"><button class="alert-success">Delete</button></a></td>
            </td>
        </tr>
        <?php }?>
    </table>

為數據表添加了腳本,但是它返回錯誤大炮重新初始化數據表

    $(document).ready(function (){
    var table = $('#example').dataTable({
   "order": [[ 0, 'asc' ]]
   });    
  });

確保DB表中的“ id_user”字段為“ int”數據類型。

然后,嘗試使用“ aaSorting”。 參考: http : //legacy.datatables.net/release-datatables/examples/basic_init/table_sorting.html

$(document).ready(function() {
    $('#example').dataTable( {
        "aaSorting": [[ 0, "asc" ]]
    } );
} );

嘗試這個 :

 <td>
   <?php echo $id ;?>
  </td>

只需從顯示記錄中刪除

對於居中數據,您可以使用text-center bootstrap類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM