Error Can not set property '_DT_CellIndex' of undefined jquerydatatable

0

I am using PHP and when I put data into the jquery datatable the datatable style is lost, I get the following error in the console.

<?php  
include 'inc/conexion.php';

 $query ="SELECT * FROM user ";  
 $result = mysqli_query($conn, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>DataTable</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
            <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>  
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>            
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />  
      </head>  
      <body>  
           <br /><br />  
           <div class="container">  
                <h3 align="center">Datatables Jquery</h3>  
                <br />  
                <div class="table-responsive">  
                     <table id="employee_data" class="table">  
                          <thead>  
                               <tr>  
                            <th>Usuario Nombre</th>  
                            <th>Tipo</th> 
                               </tr>  
                          </thead>  
                          <tr>
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                            ?> 
                            <tr>
                              <td><?php echo $row["usuario_id"]; ?></td>
                              <td><?php echo $row["tipo"]; ?></td>
                              
                            </tr>
                            <?php 
                          }  
                          ?>  
                          <tr>
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#employee_data').DataTable();  
 });  
 </script>  
    
asked by MoteCL 07.09.2017 в 18:49
source

2 answers

1

You have two tr more, before <?php and after closing ?> :

Fixed :

<?php  
include 'inc/conexion.php';

 $query ="SELECT * FROM user ";  
 $result = mysqli_query($conn, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>DataTable</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
            <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>  
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>            
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />  
      </head>  
      <body>  
           <br /><br />  
           <div class="container">  
                <h3 align="center">Datatables Jquery</h3>  
                <br />  
                <div class="table-responsive">  
                     <table id="employee_data" class="table">  
                          <thead>  
                               <tr>  
                            <th>Usuario Nombre</th>  
                            <th>Tipo</th> 
                               </tr>  
                          </thead>  
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                            ?> 
                            <tr>
                              <td><?php echo $row["usuario_id"]; ?></td>
                              <td><?php echo $row["tipo"]; ?></td>
                              
                            </tr>
                            <?php 
                          }  
                          ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#employee_data').DataTable();  
 });  
 </script>  
    
answered by 08.09.2017 / 00:38
source
0

It seems that the error is here

                     <tr>
                      <?php  
                      while($row = mysqli_fetch_array($result))  
                      {  
                        ?> 
                        <tr>
                          <td><?php echo $row["usuario_id"]; ?></td>
                          <td><?php echo $row["tipo"]; ?></td>

                        </tr>
                        <?php 
                      }  
                      ?>  
                      <tr>

There should be a tbody like that:

                        <tbody>
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                            ?> 
                            <tr>
                              <td><?php echo $row["usuario_id"]; ?></td>
                              <td><?php echo $row["tipo"]; ?></td>

                            </tr>
                            <?php 
                          }  
                          ?>  
                          </tbody>
    
answered by 07.09.2017 в 22:14