列表展示多数据
展示类似结构且可搜索
第一步查看表结构存储
因存表是附表id 展示效果是附表的name值
第二步处理查询
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
$wheres = [];
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//自定义需要参数
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
if(isset($filter['book_id'])){
$whereLike = " name like '%" .$filter['book_id'] . "%'";
$book_id = implode(',', db('book')->where($whereLike)->column('id'));
$rex = explode(',', $book_id);
foreach ($rex as $v){
$wheres[] = ['exp',Db::raw("FIND_IN_SET('$v', book_id)")];
}
}
//end
$list = $this->model
->with(['address','member', 'arealist'])
->where($where)
->whereOr($wheres)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->visible(['id','user_id', 'book_id','address_id','address_info','contact','phone','start_end_time','order_sn','create_time','status','delivery_time','return_book_time','return_book_finsh_time','des']);
$row->visible(['address']);
$row->visible(['member']);
}
$total = $list->total();
$lists = $list->toArray();
foreach ($lists['data'] as &$value) {
//自定义需要参数
$value['bookids'] = $value['book_id'];
if($value['book_id']){
$bookId = explode(',', $value['book_id']);
$value['book_id'] = implode(',', db('book')
->whereIn('id', $bookId)
->column('name'));
}
//end
}
$result = array("total" => $total, "rows" => $lists['data']);
return json($result);
}
return $this->view->fetch();
}
处理数据之前是需要吧total和toArray取出 否则foreach需要读取item下的数据(使用的是分页查询)
- book_id 存储及时book表的id使用in查询name返回即可
第二步处理搜索
修改application/common/controller/Backend.php大约270行左右
book_id是返回去的id且不能修改 只需要安装此方法查询即可