CI3.x 使用json,配合easyui,javascript
其實很簡單,走了不少的彎路,php
首先在ci的控制器重,創建2個方法,一個用於顯示加載view,一個用於echo json,就能夠了。css
須要注意的是,在ci的配置中config文件中,要配置base_url ,並且,在引用時localhost和127.0.0.1不能互用,html
這一段,是ci的php控制器中的方法java
public function demojson() { $this->load->database(); $query = $this->db->query('select id,nav_name,nav_desc from nav'); $row = $query->result_array(); $json = json_encode($row); echo $json; } public function demo2() { $this->load->view("demo2.html"); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>demo2</title> <link rel="stylesheet" href="http://localhost/easyui/themes/default/easyui.css"> <link rel="stylesheet" href="http://localhost/easyui/themes/icon.css"> <script type="text/javascript" src="http://localhost/easyui/jquery.min.js"></script> <script type="text/javascript" src="http://localhost/easyui/jquery.easyui.min.js"></script> <script type="text/javascript" src="http://localhost/public/demo2.js"></script> </head> <body> <?php echo site_url("demo/demojson"); ?> <table id="dg"></table> </body> </html>
$(function(){ $('#dg').datagrid({ url:'demojson', columns:[[ {field:'id',title:'id',width:100}, {field:'nav_name',title:'name',width:200}, {field:'nav_desc',title:'描述',width:300} ]] }); });
最終運行結果:jquery