[fastadmin]第十篇 fastadmin 自定义按钮增加表单(又是找不到的教程)
自定义按钮增加form
前面我们讲了,很多自定义按钮的东西
但是没讲form
我们来看form 的如何做
1. 首先在对应模块js中 增加一个 button
{
field: 'operate', title: __('Operate'),
table: table,
events: Table.api.events.operate,
formatter: Table.api.formatter.operate,
buttons: [
{
name: 'add_comment',
text: __(''),
title: __('备注'),
classname: 'btn btn-xs btn-primary btn-dialog',
// classname: 'btn btn-xs btn-primary btn-log',
icon: 'fa fa-comment',
url: 'ar/mangement/comment',
method: 'get',
// callback: function (data) {
// Layer.alert("接收到回传数据:" + JSON.stringify(data), {title: "回传数据"});
// },
},
],
}
这里url 就是你的后台代码处.一会儿要写后台php方法
2. js 与add edit 同级 ,注册一个 comment的form
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
comment: function () {
Form.api.bindevent($("form[role=form]"));
},
3. 后台代码,展示界面,处理逻辑
public function comment($ids = null)
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isPost()) {
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
// row 是 一个数组的名字, row/a 是取 数组 row 的所有的(all)数据。
$comment = $this->request->post();
if (isset($params["comment"]) && empty($params["comment"])) {
$this->error(__('备注必填'));
}
$oldData = $this->model->find($ids); // 需要去掉这个 追加属性
$oldData->comment = $comment["comment"];
$res = $oldData->save();
if (false === $res) {
$this->error(__('备注失败!'));
}
$this->success();
}
return $this->view->fetch();
}
4. 新增commment.html
<form id="comment-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Comment')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-comment" class="form-control" name="comment" type="text" value="">
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-primary btn-embossed">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
5.提交后自动关闭窗口
在 comment.html
增加
<script>
// 这里加了,才会触发自动关闭该窗口
$(function () {
Form.api.bindevent($("form[role=form]"));
});
</script>
本作品采用《CC 协议》,转载必须注明作者和本文链接