2017-07-13 24 views
2

這是我的index.php觀點是我的表被放置如何顯示我的表格?

<table class="striped"> 
       <thead> 
       <tr> 
        <th>Id</th> 
        <th>Name</th> 
        <th>Address</th> 
        <th>Created at</th> 
       </tr> 
       </thead> 

       <tbody id="showdata"> 
       <!--<tr> 
        <td>1</td> 
        <td>Alvin</td> 
        <td>Eclair</td> 
        <td>$0.87</td> 
        <td> 
        <a href="javascript:;" class="waves-effect waves-light btn">Edit</a> 
        <a href="javascript:;" class="waves-effect waves-light btn">Delete</a> 
        </td> 
       </tr>--> 
       </tbody> 
</table> 

這是我的AJAX腳本放在index.php文件

function showAllEmployee() { 
     $.ajax({ 
     type: 'ajax', 
     url: '<?php echo base_url(); ?>index.php/Employee/showAllEmployee', 
     async: false, 
     dataType: 'json', 
     success: function(data) { 
      //console.log(data); 
      var html = ''; 
      var i; 
      for (i=0; i<data.length; i++) { 
      html += '<tr>'+ 
        '<td>'+data[i].emp_id+'</td>'+ 
        '<td>'+data[i].name+'</td>'+ 
        '<td>'+data[i].address+'</td>'+ 
        '<td>'+data[i].created_at+'</td>'+ 
        '<td>'+ 
        '<a href="javascript:;" class="waves-effect waves-light btn" style="margin-right: 10px;" data="'+data[i].emp_id+'">Edit</a>'+ 
        '<a href="javascript:;" class="waves-effect waves-light btn" data="'+data[i].emp_id+'">Delete</a>'+ 
        '</td>'+ 
       '</tr>'; 
      } 
      $('#showdata').html(html); 
     }, 
     error: function() { 
      alert('Could not get data from database'); 
     } 

     }); 
    } 

這是我有我的員工控制器上

public function showAllEmployee() 
    { 
     $result = $this->em->showAllEmployee(); 
     echo json_encode($result); 
    } 

這是我的模型

public function showAllEmployee() 
    { 
     $this->db->order_by('created_at', 'desc'); 
     $query = $this->db->get('tbl_employees'); 
     if($query->num_rows() > 0) { 
      return $query->result(); 
     } else { 
      return false; 
     } 
    } 

每當我刷新頁面中的數據不會顯示,而不是我碰到一個錯誤無法從它是我在我的AJAX腳本設置可能是錯誤請幫助

+0

如果AJAX腳本打那麼控制器腳本就可以了。確保它擊中控制器 –

+0

@masum billah我確信這個URL我可以查看我的JSON數據 – Lestah

+0

如何調用JS函數'showAllEmployee()'?並使用'type:'get','而不是使用'ajax'。 –

回答

0

組信頭作爲JSON狀況數據庫獲取數據在控制器類型 控制器更改爲

public function showAllEmployee(){ 
    $result = $this->em->showAllEmployee(); 
    $this->output 
     ->set_content_type('application/json') 
     ->set_output(json_encode($result)); 
}