dongzouya5792 2019-07-03 07:41
浏览 114
已采纳

我在使用datatables.net时遇到了我的DataTables问题

Im Having a problem with my DataTables im using datatables.net the problem im having right now is the "Showing # to # of # entries" is not working and not counting the data that it is showing in the tables and the pagination is not working and the search bar is not functioning the "Show 10/100 entries" is not working.

PS: I put quotation on them because i dont know what do you call them.

This is my html and ajax code

<div class="card card-dark">
    <h6 class="card-header">User Status Details</h6>
    <div class="card-body">
     <span id="message"></span>
     <div class="table-responsive">
     <table class="table table-striped" id="table-1">
    </div>
   </table>
     <script>
     $(document).ready(function(){

      load_user_data();

      function load_user_data()
      {
       var action = 'fetch';
       $.ajax({
        url:'action',
        method:'POST',
        data:{action:action},
        success:function(data)
        {
         $('#table-1').html(data);
        }
       });
      }

      $(document).on('click', '.action', function(){
       var id = $(this).data('id');
       var user_status = $(this).data('user_status');
       var action = 'change_status';
       $('#message').html('');
       if(confirm("Change User Status"))
       {
        $.ajax({
         url:'action',
         method:'POST',
         data:{id:id, user_status:user_status, action:action},
         success:function(data)
         {
          if(data != '')
          {
           load_user_data();
           $('#message').html(data);
          }
         }
        });
       }
       else
       {
        return false;
       }
      });

     });
     </script>
    </div>
   </div>

my action code

if(isset($_POST['action']))
{

    if($_POST["action"] == 'fetch')
    {
        $output = '';
        $query = "SELECT * FROM users WHERE user_type = 'user' ORDER BY username ASC";
        $statement = $connect->prepare($query);
        $statement->execute();
        $result = $statement->fetchAll();
        $output .= '
            <table class="table table-striped" id="table-1">
            <tr>
                <td>ID #</td>
                <td>Username</td>
                <td>Status</td>
                <td>Action</td>
            </tr>
        ';
        foreach($result as $row)
        {
            $status = '';
            if($row["user_status"] == 'Active')
            {
                $status = '<span class="badge badge-primary">Active</span>';
            }
            else
            {
                $status = '<span class="badge badge-primary">Inactive</span>';
            }
            $output .= '
            <tr>
                <td>'.$row['id'].'</td>
                <td>'.$row['username'].'</td>
                <td>'.$status.'</td>
                <td><button type="button" name="action" class="btn btn-info btn-xs action" data-id="'.$row["id"].'" data-user_status="'.$row["user_status"].'">Action</button></td>
            </tr>
            ';
        }
        $output .= '</table>';
        echo $output;
    }

    if($_POST["action"] == 'change_status')
    {
        $status = '';
        if($_POST['user_status'] == 'Active')
        {
            $status = 'Inactive';
        }
        else
        {
            $status = 'Active';
        }
        $query = '
        UPDATE users SET user_status = :user_status WHERE id = :id
        ';
        $statement = $connect->prepare($query);
        $statement->execute(
            array(
                ':user_status'          => $status,
                ':id'              => $_POST['id']
            )
        );
        $result = $statement->fetchAll();
        if(isset($result))
        {
            echo '<div class="alert alert-info">User status set to <strong>'.$status.'</strong><div>';
        }
    }
}

?>

I cant figure out whats missing on my code but the ajax code is working fine and the data is showing in my DataTable

  • 写回答

1条回答 默认 最新

  • douweibeng5219 2019-07-03 08:41
    关注

    HTML:

    <div class="card card-dark">
        <h6 class="card-header">User Status Details</h6>
    
        <div class="card-body">
            <span id="message"></span>
    
                <div id="tableDiv"></div>
    
        </div>
    
    </div>
    

    JS:

    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function(){
    
          load_user_data();
     // initialize DT 
     var table = $('#table-1').DataTable();
    
     $(document).on('click', '.action', function(){
           var id = $(this).data('id');
           var user_status = $(this).data('user_status');
           var action = 'change_status';
           $('#message').html('');
           if(confirm("Change User Status"))
           {
            $.ajax({
             url:'action',
             method:'POST',
             data:{id:id, user_status:user_status, action:action},
             success:function(data)
             {
              if(data != '')
              {
               load_user_data();
               $('#message').html(data);
               // re-load data and re-initialize DT
               $('#tableDiv').html(data); 
               table.rows().invalidate().draw(); // or table.rows().invalidate();
              }
             }
            });
           }
           else
           {
            return false;
           }
     });
    });
    
    function load_user_data()
     {
           var action = 'fetch';
     $.ajax({
            url:'action',
            method:'POST',
            data:{action:action},
            success:function(data)
            {
             $('#tableDiv').html(data);
            }
     });
     }
    </script>
    

    PHP:

    if(isset($_POST['action']))
    {
    
        if($_POST["action"] == 'fetch')
        {
            $output = '';
            $query = "SELECT * FROM users WHERE user_type = 'user' ORDER BY username ASC";
            $statement = $connect->prepare($query);
            $statement->execute();
            $result = $statement->fetchAll();
            $output .= '
                <table class="table table-striped table-responsive" id="table-1">
                <thead>
                <tr>
                    <th>ID #</th>
                    <th>Username</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
                </thead>
            ';
    $output .= '<tbody>';
            foreach($result as $row)
            {
                $status = '';
                if($row["user_status"] == 'Active')
                {
                    $status = '<span class="badge badge-primary">Active</span>';
                }
                else
                {
                    $status = '<span class="badge badge-primary">Inactive</span>';
                }
                $output .= '
                <tr>
                    <td>'.$row['id'].'</td>
                    <td>'.$row['username'].'</td>
                    <td>'.$status.'</td>
                    <td><button type="button" name="action" class="btn btn-info btn-xs action" data-id="'.$row["id"].'" data-user_status="'.$row["user_status"].'">Action</button></td>
                </tr>
                ';
            }
    $output .= '</tbody>';
            $output .= '</table>';
            echo $output;
        }
    
        if($_POST["action"] == 'change_status')
        {
            $status = '';
            if($_POST['user_status'] == 'Active')
            {
                $status = 'Inactive';
            }
            else
            {
                $status = 'Active';
            }
            $query = '
            UPDATE users SET user_status = :user_status WHERE id = :id
            ';
            $statement = $connect->prepare($query);
            $statement->execute(
                array(
                    ':user_status'          => $status,
                    ':id'              => $_POST['id']
                )
            );
            $result = $statement->fetchAll();
            if(isset($result))
            {
                echo '<div class="alert alert-info">User status set to <strong>'.$status.'</strong><div>';
            }
        }
    }
    
    ?>
    

    Try this all, I hope it will work, because I can't check it by myself. From this moment look at console for an error. Show them if they are.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题