使用带有codeigniter的ajax数据表显示数据

时间:2017-12-18 13:47:31

标签: php html ajax

我想使用ajax在我的视图中显示数据库中的数据。 但是我收到了这条消息:DataTables警告:table id = users - 请求的未知参数' 0'对于第0行,第0列。有关此错误的详细信息,请参阅http://datatables.net/tn/4

这是我的控制者:

public function listUser(){
     if ($this->session->userdata("success"))
        {

            // les Variables de datatable
           //
           $draw = intval($this->input->post("draw"));
           //
           $start = intval($this->input->post("start"));
           $length = intval($this->input->post("length"));
         $user = $this->UserModel->getUsers();
         $data = array();
         foreach( $user as $r) {        
                                        $tab[] = array();
                                        $tab[] =    $r->username;
                                        $tab[] =$r->usersurname;
                                        $tab[] =$r->useremail;
                                        $tab[] =$r->date_create;
                                        $tab[] =$r->Num_tel;
                                        $tab[] =$r->userright;                                                              
                                        $tab[] = '<button type="button" name="update" id="'.$r->user_id.'" class="btn btn-warning btn-xs update">Modifier</button>';  
                                        $tab[] = '<button type="button" name="delete" id="'.$r->user_id.'" class="btn btn-danger btn-xs delete">Supprimer</button>';  
                                       $data = $tab;
                            }

                            $output = array(
                                "draw" => $draw,
                                    "recordsTotal" => count($user),
                                    "recordsFiltered" => count($user),
                                    "data" => $data
                                );

                            echo json_encode($output);
                            exit();

                        }
//    $this->load->view('users/listUser',['user'=>$user]);
   //     }
     else
        {
            return redirect('Login');  
        }

}

2 个答案:

答案 0 :(得分:0)

您的问题可能在循环部分,每次循环在此行$tab[] = array();中传递您未重置变量但实际将新数组附加到$tab时,您应该使用{{1重置变量,最后$tab = array();$data[] = $tab;数据附加到$tab变量。

$data

答案 1 :(得分:0)

好的,谢谢,但我收到另一个警告:DataTables警告:table id = users - 第0行第6列请求的未知参数'6'。有关此错误的详细信息,请参阅http://datatables.net/tn/4

这是我的观点:

<table class="display responsive no-wrap table table-striped table-bordered table-hover table-checkable order-column dataTable no-footer" id="users" width="100%">
                                            <thead>
                                                <tr>
                                                    <th>Nom</th>
                                                    <th>Prenom</th>
                                                    <th>E-mail</th>              
                                                    <th>Date de creation</th>
                                                    <th>Téléphone</th>               
                                                    <th>Droit</th>
                                                     <th>Modifier</th>
                                                    <th>Supprimer</th>
                                                </tr>
                                            </thead>

                                        </table>
<script type="text/javascript">
        $('document').ready(function() {

            let mouvementTable =  $("#users").DataTable({
                                 buttons: [{
                        extend: "print",
                        className: "btn dark btn-outline"
                    },  {
                        extend: "pdf",
                        className: "btn green btn-outline"
                    }, {
                        extend: "excel",
                        className: "btn yellow btn-outline "
                    }, {
                        extend: "csv",
                        className: "btn purple btn-outline "
                    }],


                "ajax": {
                    url : "http://localhost/logistock/index.php/ControllerUser/listUser",
                    "type": "POST"
                },

                order: [[ 1, 'asc' ]],
                 "select": {
                     "style":    'multi',
                    "selector": 'td:first-child'
                 },
               "dom": "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",

                "lengthMenu": [[5, 10, 15, 20, -1], [5, 10, 15, 20, "All"]],
                "language": {
                    "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
                }



            });  

        });


    </script>