fastadmin学习笔记
一 快速开始
cd fastadmin
php think crud -t test -u 1
命令解释:
一,添加了以下文件
# new file: application/admin/controller/Test.php # new file: application/admin/lang/zh-cn/test.php # new file: application/admin/model/Test.php # new file: application/admin/validate/Test.php # new file: application/admin/view/test/add.html # new file: application/admin/view/test/edit.html # new file: application/admin/view/test/index.html # new file: application/admin/view/test/recyclebin.html # new file: public/assets/js/backend/test.js
二,添加了菜单权限
id |
type | pid | name | title | icon | url | condition | remark | ismenu | menutype | extend | py | pinyin | createtime | updatetime | weigh | status |
85 | file | 0 | test | 测试管理 | fa fa-circle-o | 1 | csgl | ceshiguanli | 1733211196 | 1733211196 | 0 | normal | |||||
86 | file | 85 | test/index | 查看 | fa fa-circle-o | 0 | zk | zhakan | 1733211196 | 1733211196 | 0 | normal | |||||
87 | file | 85 | test/recyclebin | 回收站 | fa fa-circle-o | 0 | hsz | huishouzhan | 1733211196 | 1733211196 | 0 | normal | |||||
88 | file | 85 | test/add | 添加 | fa fa-circle-o | 0 | tj | tianjia | 1733211196 | 1733211196 | 0 | normal | |||||
89 | file | 85 | test/edit | 编辑 | fa fa-circle-o | 0 | bj | bianji | 1733211196 | 1733211196 | 0 | normal | |||||
90 | file | 85 | test/del | 删除 | fa fa-circle-o | 0 | sc | shanchu | 1733211196 | 1733211196 | 0 | normal | |||||
91 | file | 85 | test/destroy | 真实删除 | fa fa-circle-o | 0 | zssc | zhenshishanchu | 1733211196 | 1733211196 | 0 | normal | |||||
92 | file | 85 | test/restore | 还原 | fa fa-circle-o | 0 | hy | huanyuan | 1733211196 | 1733211196 | 0 | normal | |||||
93 | file | 85 | test/multi | 批量更新 | fa fa-circle-o | 0 | plgx | pilianggengxin | 1733211196 | 1733211196 | 0 | normal |
生成文档
php think api --force=true
# new file: public/api.html
fastadmin表单中添加下拉树表单
表结构:
CREATE TABLE IF NOT EXISTS `fa_tree_data` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `pid` int(10) DEFAULT NULL COMMENT '父节点', `name` varchar(200) DEFAULT NULL COMMENT '名称', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COMMENT='树表数据';
controller 添加
use fast\Tree;
在初始化方法_initialize()中添加:
$tree = Tree::instance(); $tree->init(collection($this->model->order('id desc')->select())->toArray(), 'pid'); $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name'); $categorydata = [0 => ['type' => 'all', 'name' => __('None')]]; foreach ($this->categorylist as $k => $v) { $categorydata[$v['id']] = $v; } $this->view->assign("parentList", $categorydata);
在add.html中添加
<select id="c-pid" data-rule="required" class="form-control selectpicker" name="row[pid]"> {foreach name="parentList" item="vo"} <option value="{$key}" {in name="key" value="" }selected{/in}>{$vo.name}</option> {/foreach} </select>
在edit.html中添加
<select id="c-pid" data-rule="required" class="form-control selectpicker" name="row[pid]"> {foreach name="parentList" item="vo"} <option value="{$key}" {in name="key" value="$row.pid" }selected{/in}>{$vo.name} </option> {/foreach} </select>
表单打开高德地图选择定位和地址
<div class="form-group"> <label class="control-label col-xs-12 col-sm-2">{:__('Address')}:</label> <div class="col-xs-12 col-sm-8"> <div class="input-group"> <input id="c-address" class="form-control" name="row[address]" type="text"> <div class="input-group-addon no-border no-padding"> <span></span> <span><button type="button" class="btn btn-primary" data-toggle="addresspicker" data-input-id="c-address" data-lng-id="c-lng" data-lat-id="c-lat">打开地图选择地址/经纬度</button></span> </div> <span class="msg-box n-right" for="c-address"></span> </div> </div> </div> <div class="form-group"> <label class="control-label col-xs-12 col-sm-2">经纬度:</label> <div class="col-xs-12 col-sm-8"> <div class="row no-padding"> <div class="col-md-6"> <input id="c-lng" class="form-control" name="row[lng]" type="text"> </div> <div class="col-md-6"> <input id="c-lat" class="form-control" name="row[lat]" type="text"> </div> </div> </div> </div>
如果需要整个控制器不使用布局
定义$layout
属性为false
protected $layout = false;
如果需要在控制器的某个方法不使用布局,可以
$this->view->engine->layout(false);
一对多关联表快速开发
界面效果:
1.在模块.js中 表格的columns中添加按钮
{ field: 'buttons', width: "120px", title: __('广告位'), table: table, events: Table.api.events.operate, buttons: [ { name: 'detail', text: __('管理广告位'), title: __('管理广告位'), classname: 'btn btn-xs btn-primary btn-dialog', icon: 'fa fa-list', url: 'uran/adslot/index?upoi_id={id}', callback: function (data) { Layer.alert("接收到回传数据:" + JSON.stringify(data), { title: "回传数据" }); }, visible: function (row) { //返回true时按钮显示,返回false隐藏 return true; } } ], formatter: Table.api.formatter.buttons },
注意加粗的部分,向子表的index方法传递参数,参数需要严格匹配表字段,则子表自动条件过滤.
2.修改子表的添加按钮,将参数传递给add.html ,在子表的js中add_url后台添加
add_url: 'corp/adslot/add' + location.search,
3.在添加页面中自动选中主表选框值,使用thinkphp5的页面传参语法
<input id="c-corp_upoi_id" data-rule="required" data-source="corp/upoi/index" class="form-control selectpage" name="row[corp_upoi_id]" type="text" value="{$Think.get.corp_upoi_id|default=''}">
完成
表格中的列显示为关联表中的名称字符
第一步:在表的js文件中添加或修改多个关联列为下面的格式
{ field: 'placetype.name', title: __('placetype_id'), operate: 'FIND_IN_SET' }, { field: 'position.name', title: __('Position'), operate: 'FIND_IN_SET' }, ....
第二步,在PHP文件中添加index方法,使用with关联表语法,如果是多个关联列,在数组中添加
/** * 查看 */ public function index() { $this->relationSearch = true; $this->searchFields = "admin.username,id"; if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model // ->with("admin") ->with(['placetype','position','media','admin']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); }
第三步,在model中添加with引用的方法
public function admin() { return $this->belongsTo('app\admin\model\Admin', 'admin_id')->setEagerlyType(0); } public function placetype() { return $this->belongsTo('app\common\model\Category', 'corp_placetype_id')->setEagerlyType(0); } public function position() { return $this->belongsTo('app\common\model\Category', 'position')->setEagerlyType(0); } public function media() { return $this->belongsTo('app\admin\model\corp\Media', 'corp_media_id')->setEagerlyType(0); }