Article Directory
question
We often encounter in development that when accessing the index method, we need to pass a parameter to display the data we have specified with a unified logo.
1. Ideas
- Pass a parameter to index via the URL!
1) Since the accessed index method is often initiated by ajax, the parameters will not take effect! - Add a search condition when ajax is initiated,
such as $group_id=1
2. Method 1
Use the assignconfig() method in the controller to transparently pass parameters to js
public function index($ids=NULL) //需要从URL接收参数
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax())
{
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['taxState']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$this->assignconfig("group_id",$ids); //在这里透传给js
return $this->view->fetch();
}
Corresponding to receiving data in js and splicing it into the search conditions
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'test/index',
add_url: 'test/add',
edit_url: 'test/edit',
del_url: 'test/del',
multi_url: 'test/multi',
table: 'test',
},
queryParams: function (params) {
//自定义搜索条件
var filter = params.filter ? JSON.parse(params.filter) : {
}; //判断当前是否还有其他高级搜索栏的条件
var op = params.op ? JSON.parse(params.op) : {
}; //并将搜索过滤器 转为对象方便我们追加条件
filter.group_id= Config.group_id; //将透传的参数 Config.group_id,追加到搜索条件中
op.group_id= "="; //group_id的操作方法的为 找到相等的
params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
params.op = JSON.stringify(op);
> //如果希望忽略搜索栏搜索条件,可使用
//params.filter = JSON.stringify({url: 'login'});
//params.op = JSON.stringify({url: 'like'});
return params;
},
});
3. Method 2
Without modifying the controller, you only need to modify the parameters when initiating ajax in js
queryParams: function (params) {
//自定义搜索条件
var filter = params.filter ? JSON.parse(params.filter) : {
}; //判断当前是否还有其他高级搜索栏的条件
var op = params.op ? JSON.parse(params.op) : {
}; //并将搜索过滤器 转为对象方便我们追加条件
//将需要的参数group_id 加入到搜索条件中去 要求url传递的参数必须为group_id=XX;可以用 Fast.api.query("group_id")获取到!
filter.group_id= Fast.api.query("group_id");
op.group_id= "="; //group_id的操作方法的为 找到相等的
params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
params.op = JSON.stringify(op);
//如果希望忽略搜索栏搜索条件,可使用
//params.filter = JSON.stringify({url: 'login'});
//params.op = JSON.stringify({url: 'like'});
return params;
},
think
- Arrays should be used when there are many parameters to pass.
- There should be feedback if the case is accessed without parameters.