- 添加左侧菜单
-
生成规则
-
表结构
-
目录结构
控制器
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
/**
* demo
*
* @icon fa fa-dashboard
* @remark 这是一个手动生成CRUD的demo
*/
class Demo extends Backend
{
/**
* 模型对象
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
//设置对应的model
$this->model = model('Demo');
//设置模板的布局
$this->layout = 'demoLayout';
$this->view->assign("weekList", $this->model->getWeekList());
$this->view->assign("flagList", $this->model->getFlagList());
$this->view->assign("genderdataList", $this->model->getGenderdataList());
$this->view->assign("hobbydataList", $this->model->getHobbydataList());
$this->view->assign("statusList", $this->model->getStatusList());
$this->view->assign("stateList", $this->model->getStateList());
}
/**
* 查看
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('pkey_name'))
{
return $this->selectpage();
}
//生成查询条件的方法
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();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
}
模型
<?php
namespace app\admin\model;
use think\model;
class Demo extends model
{
// 表名
protected $name = 'test';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'week_text',
'flag_text',
'genderdata_text',
'hobbydata_text',
'refreshtime_text',
'status_text',
'state_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getWeekList()
{
return ['monday' => __('Week monday'),'tuesday' => __('Week tuesday'),'wednesday' => __('Week wednesday')];
}
public function getFlagList()
{
return ['hot' => __('Flag hot'),'index' => __('Flag index'),'recommend' => __('Flag recommend')];
}
public function getGenderdataList()
{
return ['male' => __('Genderdata male'),'female' => __('Genderdata female')];
}
public function getHobbydataList()
{
return ['music' => __('Hobbydata music'),'reading' => __('Hobbydata reading'),'swimming' => __('Hobbydata swimming')];
}
public function getStatusList()
{
return ['normal' => __('Normal'),'hidden' => __('Hidden')];
}
public function getStateList()
{
return ['0' => __('State 0'),'1' => __('State 1'),'2' => __('State 2')];
}
public function getWeekTextAttr($value, $data)
{
$value = $value ? $value : $data['week'];
$list = $this->getWeekList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getFlagTextAttr($value, $data)
{
$value = $value ? $value : $data['flag'];
$valueArr = explode(',', $value);
$list = $this->getFlagList();
return implode(',', array_intersect_key($list, array_flip($valueArr)));
}
public function getGenderdataTextAttr($value, $data)
{
$value = $value ? $value : $data['genderdata'];
$list = $this->getGenderdataList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getHobbydataTextAttr($value, $data)
{
$value = $value ? $value : $data['hobbydata'];
$valueArr = explode(',', $value);
$list = $this->getHobbydataList();
return implode(',', array_intersect_key($list, array_flip($valueArr)));
}
public function getRefreshtimeTextAttr($value, $data)
{
$value = $value ? $value : $data['refreshtime'];
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : $data['status'];
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStateTextAttr($value, $data)
{
$value = $value ? $value : $data['state'];
$list = $this->getStateList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setFlagAttr($value)
{
return is_array($value) ? implode(',', $value) : $value;
}
protected function setHobbydataAttr($value)
{
return is_array($value) ? implode(',', $value) : $value;
}
protected function setRefreshtimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
}
视图 列表
<div class="panel panel-default panel-intro">
<!--动态生成父节点的title和remark信息 build_heading-->
<!--{:build_heading()}-->
<div class="panel-heading"><div class="panel-lead"><em>demo</em>父节点的remark</div></div>
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<!--根据权限动态的生成表格操作按钮栏 build_toolbar-->
<!--{:build_toolbar()}-->
<a href="javascript:;" class="btn btn-primary btn-refresh" title="刷新" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success btn-add" title="添加" ><i class="fa fa-plus"></i> 添加</a>
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled" title="编辑" ><i class="fa fa-pencil"></i> 编辑</a>
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled" title="删除" ><i class="fa fa-trash"></i> 删除</a>
<a href="javascript:;" class="btn btn-danger btn-import" title="导入" id="btn-import-NfljRw" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> 导入</a>
<!--更多下拉框-->
<div class="dropdown btn-group {:$auth->check('demo/multi')?'':'hide'}">
<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
<ul class="dropdown-menu text-left" role="menu">
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
</ul>
</div>
</div>
<!--列表-->
<table id="table" class="table table-striped table-bordered table-hover"
data-operate-edit="{:$auth->check('demo/edit')}"
data-operate-del="{:$auth->check('demo/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
前端列表 js文件
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'demo/index',
add_url: 'demo/add',
edit_url: 'demo/edit',
del_url: 'demo/del',
multi_url: 'demo/multi',
table: 'demo',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'weigh',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'admin_id', title: __('Admin_id')},
{field: 'category_id', title: __('Category_id')},
{field: 'category_ids', title: __('Category_ids')},
{field: 'week_text', title: __('Week'), operate:false},
{field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
{field: 'genderdata_text', title: __('Genderdata'), operate:false},
{field: 'hobbydata_text', title: __('Hobbydata'), operate:false, formatter: Table.api.formatter.label},
{field: 'title', title: __('Title')},
{field: 'image', title: __('Image'), formatter: Table.api.formatter.image},
{field: 'images', title: __('Images'), formatter: Table.api.formatter.images},
{field: 'attachfile', title: __('Attachfile')},
{field: 'keywords', title: __('Keywords')},
{field: 'description', title: __('Description')},
{field: 'city', title: __('City')},
{field: 'price', title: __('Price')},
{field: 'views', title: __('Views')},
{field: 'startdate', title: __('Startdate')},
{field: 'activitytime', title: __('Activitytime')},
{field: 'year', title: __('Year')},
{field: 'times', title: __('Times')},
{field: 'refreshtime', title: __('Refreshtime'), formatter: Table.api.formatter.datetime},
{field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
{field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime},
{field: 'weigh', title: __('Weigh')},
{field: 'switch', title: __('Switch')},
{field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
{field: 'state_text', title: __('State'), operate:false},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
框架的布局文件
关于模板布局
控制器默认全部采用模板布局,因此我们的页面都会自动加上头部和尾部,如果我们有特殊的页面不需要采用模板布局,
我们可以使用$this->view->engine->layout(false);来关闭当前方法的模板布局
如果我们需要使用自己的模板布局,在当前控制器定义protected $layout = '布局模板';即可。
请注意如果采用了自己的模板布局或禁用了模板布局,将无法使用FastAdmin的JS按需加载和Config变量访问。