2016-01-10 20 views
0

我的模型中獲取數據EasyUI DataGrid的網址不是從表

<?php 

/* * 要改變這種許可證頭,選擇在項目屬性許可頭。 *要更改此模板文件,請選擇工具|模板 *並在編輯器中打開模板。 */ 類User_model延伸CI_Model {

public function get_user($offset,$limit,$q=''){ 

    $sql = "SELECT * FROM users WHERE 1=1 "; 
    if($q!=''){ 

     $sql .=" AND name LIKE '%{$q}%' "; 
    } 
    $result['count'] = $this->db->query($sql)->num_rows(); 
    $sql .=" LIMIT {$offset},{$limit} "; 
    $result['data'] = $this->db->query($sql)->result(); 

    return $result ; 
} 

}

<?php 

類用戶延伸是CI_Controller {

public function __construct(){ 

    parent::__construct(); 

    //load the model 
    // $this->load->model('user_model'); 
} 

//load user view 
public function index(){ 


    $this->load->view('user_view'); 
} 

//get data for datagrid 
public function get_user(){ 

    /*Default request pager params dari jeasyUI*/ 
    $offset = isset($_POST['page']) ? intval($_POST['page']) : 1; 
    $limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
    $search = isset($_POST['search']) ? $_POST['search'] : ''; 
    $offset = ($offset-1)*$limit; 
    $data = $this->user_model->get_user($offset,$limit,$search); 
    $i = 0; 
    $rows = array(); 
    foreach ($data ['data'] as $r) { 

     //array keys ini = attribute 'field' di view nya 
     $rows[$i]['first_name'] = $r->first_name; 
     $rows[$i]['last_name'] = $r->last_name; 
     $rows[$i]['phone'] = $r->phone; 
     $rows[$i]['email'] = $r->email; 

    $i++; 
    } 

    //keys total & rows wajib bagi jEasyUI 
    $result = array('total'=>$data['count'],'rows'=>$rows); 
    echo json_encode($result); //return nya json 
} 

}

<?php $url_data=base_url().'user/get_user';?> <?php echo $url_data;?>

<table id="dg" title="Product" class="easyui-datagrid" 
 
style ="width:auto;height:400px" 
 
url ='<?php echo $url_data;?>' toolbar="#toolbar" 
 
pagination="true" 
 
rownumbers="false" 
 
fitColumns="true" singleSelect="true" 
 
checkBox ="true" striped="true" 
 
remoteSort="false" 
 
nowrap ="false"> 
 
    <thead> 
 
     <tr> 
 
\t <th field="ck" checkbox="true"></th> 
 
\t <th field="first_name" width="200" sortable="true"> 
 
      <b>First Name</b> 
 
      </th> 
 
\t <th field="last_name" width="700" sortable="true"> 
 
      <b>Last Name</b> 
 
      </th> 
 
     <th field="phone" width="100" sortable="true"> 
 
      <b>Phone</b> 
 
     </th> 
 
     <th field="email" width="100" sortable="true"> 
 
      <b>email</b> 
 
     </th> \t \t 
 
\t </tr> 
 
    </thead> 
 
</table>

回答

0

我沒有那麼多到OOP,所以我可能是錯的。 我想你忘記了返回之前在JSON您的SQL查詢的結果編碼:

echo json_encode($result); 

希望這會有所幫助。