【问题标题】:datatables default sort (asc/desc) not working数据表默认排序(asc/desc)不起作用
【发布时间】:2018-05-02 20:37:42
【问题描述】:

我尝试使用此方法将数据提取到 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>

为数据表添加脚本,但返回错误 cannon reinitialize data table

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

【问题讨论】:

  • id_user 数据类型?
  • id_user 是字符串吗?
  • 如果它是 int 它应该以正确的顺序打印用户。向我们展示您的整个代码。 @比利安东尼
  • @BillyAnthony,如果你在phpmyadmin中执行同样的查询SELECT * FROM users ORDER BY id_user ASC,你得到的记录顺序是一样的吗?
  • 用 print_r($row['id_user'] 它给出正确的订单输出..也许 jquery.datatable.js 弄乱了订单

标签: javascript jquery datatables


【解决方案1】:

男性确定“id_user”字段在数据库表中是“int”数据类型。

然后尝试使用“aaSorting”。参考:http://legacy.datatables.net/release-datatables/examples/basic_init/table_sorting.html

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    只需从显示记录中删除

    对于居中的数据,您可以使用text-center 引导类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-25
      • 2018-05-26
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2014-07-11
      相关资源
      最近更新 更多