queryParams是bootstrap-table的一個屬性功能。
前端代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>第二個</title> ....... </head> <body> <input type="text" id="test" placeholder="輸入用戶id" > <input type="text" id="test1" placeholder="輸入用戶名" > <input type="button" value="找它" id="name"> <table id="table" data-toggle="table" data-url="<?php echo base_url('work2/page');?>" data-pagination="true" data-side-pagination="server" data-query-params="queryParams" > <thead> <tr> <th data-field="uid" data-filter-control="input">ID</th> <th data-field="username">用戶名</th> <th data-field="password">用戶密碼</th> </tr> </thead> </table> <script> function queryParams(params) { var uid = $("#test").val(); //獲取文本框的值 var name = $("#test1").val(); //獲取文本框的值 params.search1 = uid; params.search2 = name; return params; } $('#name').click(function () { $('#table').bootstrapTable('refresh') }) </script> </body> </html>
后端代碼如下php:
前端傳入后台的是一個對象,前后台一般都以JSON字符串傳遞,后台只要根據關鍵字名稱取值。
$search = $this->input->get('search2'); //這個是查用戶名 $search1 = $this->input->get('search1'); //這個是查uid if(empty($search) && empty($search1)){ //兩個空則輸出全部數據 $rows = $this->db->limit($limit,$offset)->get('user1')->result(); $count = $this->db->count_all('user1'); }else if(empty($search) && $search1 != " " ){ //用戶名空,查uid $rows = $this->db->like('uid',$search1)->limit($limit,$offset)->get('user1')->result(); $count = $this->db->like('uid',$search1)->count_all_results('user1'); } else if(empty($search1) && $search != " " ){ //uid空,查用戶名 $rows = $this->db->like('username',$search)->limit($limit,$offset)->get('user1')->result(); $count = $this->db->like('username',$search)->count_all_results('user1'); }else{ //一起查 $rows = $this->db->like('uid',$search1)->like('username',$search)->get('user1')->result(); $count = $this->db->like('uid',$search1)->like('username',$search)->count_all_results('user1'); } $data = array( 'total'=>$count, 'rows'=>$rows ); echo json_encode($data);