项目有个需求是,根据栏目的id,获取到此栏目下的所有文章。本来觉得这个非常小意思嘛,然后发现如果不是自己写的页面,要用到fastadmin的默认的文章页面的话,要修改的还是挺多的。做个小记录分享一下
需求如下:
![image.png](https://cdn.fastadmin.net/uploads/20200521/0079e9b0fea65aba5583cf72352c0368.png "image.png")
这个是点击打开的弹窗,弹窗为一个fastamin的页面
![image.png](https://cdn.fastadmin.net/uploads/20200521/59f7fb9e44012d376e4a513a4f41dc6e.png "image.png")
现在开始修改:
1.先在列表添加对应的弹窗,修改请求路径
~~~
{
field: 'buttons1',
width: "120px",
title: __('签到列表'),
table: table,
events: Table.api.events.operate,
buttons: [{
name: 'detail',
text: __('签到列表'),
title: __('签到列表'),
classname: 'btn btn-xs btn-success btn-dialog',
url: 'meeting_sign/index',
callback: function (data) {
Layer.alert("接收到回传数据:" + JSON.stringify(data), {
title: "回传数据"
});
},
visible: function (row) {
//返回true时按钮显示,返回false隐藏
return true;
}
}],
formatter: Table.api.formatter.buttons
},
~~~
2.因为fastadmin是先渲染页面,然后在页面上发起ajax获取到数据,所以要在请求的控制器上,把id赋值到index页面,然后重新把id用路由的方式让ajax获取
现在这个控制器代码
~~~
/**
* 查看
*/
public function index($ids = null)
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
$this->view->assign('ids', $ids);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
$meeting_id = input("meeting_id");
//list($where, $sort, $order, $offset, $limit) = $this->buildparams();
list($subwhere, $sort, $order, $offset, $limit) = $this->buildparams();
$where = function ($query) use ($subwhere, $meeting_id) {
$query->where($subwhere)->where("meeting_id", $meeting_id);
};
$total = $this->model
->with(['user','meeting'])
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->with(['user','meeting'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['id','createtime']);
$row->visible(['user']);
$row->getRelation('user')->visible(['nickname']);
$row->visible(['meeting']);
$row->getRelation('meeting')->visible(['title']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
~~~
此处注意,需要给获取方法添加上where条件
~~~
$meeting_id = input("meeting_id");
//list($where, $sort, $order, $offset, $limit) = $this->buildparams();
list($subwhere, $sort, $order, $offset, $limit) = $this->buildparams();
$where = function ($query) use ($subwhere, $meeting_id) {
$query->where($subwhere)->where("meeting_id", $meeting_id);
};
~~~
3.在页面上添加隐藏的id
~~~
<input type="hidden" name="meeting_id" id="meeting_id" value="{$ids}">
~~~
4.在js上添加ajax的请求路由id
~~~
var table \=$("#table");
var meeting\_id \=$('#meeting\_id').val();
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url + '&meeting_id=' + meeting_id,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'user.nickname', title: __('用户昵称')},
{field: 'meeting.title', title: __('Meeting.title')},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
~~~