1、视图层定义一个input
<input id="c-shop_id" data-rule="required" data-source="company/list" class="form-control selectpage" name="row[shop_id]" data-field="name" data-primary-key="id" data-pagination="true" data-page-size="10" type="text" value="">
其中,data-source 为数据源 URL
2、编写控制器
public function list()
{
$this->model = (new ManystoreShop());
$condition = [];
$this->request->filter(['strip_tags']); //分页查询
$word = $this->request->param()['q_word'][0];
$page = $this->request->param('pageNumber');
$limit = $this->request->param('pageSize');
$limit = $limit ? $limit : 1;
// 模糊搜索
if (strlen($word) > 0) {
$condition['name'] = ['like', '%'.$word.'%'];
}
if ($this->request->param("keyValue")) {
$condition['id'] = $this->request->param("keyValue");
}
$total = $this->model->where($condition)
->order("id", "ASC")
->count();
$list = $this->model
->where($condition)
->field('id,name')
->order("id", "ASC")
->limit($limit)
->page($page)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
3、完成
4、问题
测试时,发现打开编辑 edit.html 并没有返回原有的数据值,所以需要对控制器数据源 URL 进行一下修改:
原因是,当我们打开编辑页面时,数据源URL会进行一次请求,请求时会携带一个 keyvalue 的参数,这个参数就是我们对应的源ID,此时我们进行一次查询并返回一条查询的数据就可以了
if($this->request->request("keyValue")){
$list = $this->model->field('id,name')->find([
'id' => $this->request->request("keyValue")
]);
return json(["total" => 1, "rows" => $list]);
}
此时可以看到,edit 初次加载就会获取到数据源信息
发表评论 取消回复