作者 开飞机的舒克

后台优化

<?php
namespace app\admin\controller\data;
use app\common\controller\Backend;
/**
* 数据大屏管理
*
* @icon fa fa-circle-o
*/
class Screen extends Backend
{
/**
* Screen模型对象
* @var \app\admin\model\data\Screen
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\data\Screen;
$this->view->assign("isViewList", $this->model->getIsViewList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->visible(['id','title','campus_id','images','starttime','endtime','is_view']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
}
<?php
namespace app\admin\model\data;
use think\Model;
class Screen extends Model
{
// 表名
protected $name = 'data_screen';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'starttime_text',
'endtime_text',
'is_view_text'
];
public function getIsViewList()
{
return ['0' => __('Is_view 0'), '1' => __('Is_view 1')];
}
public function getStarttimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getEndtimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getIsViewTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_view']) ? $data['is_view'] : '');
$list = $this->getIsViewList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setStarttimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setEndtimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
}
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'data/screen/index' + location.search,
add_url: 'data/screen/add',
edit_url: 'data/screen/edit',
del_url: 'data/screen/del',
multi_url: 'data/screen/multi',
import_url: 'data/screen/import',
table: 'data_screen',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'title', title: __('Title'), operate: 'LIKE'},
{field: 'campus_id', title: __('Campus_id')},
{field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
{field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
{field: 'is_view', title: __('Is_view'), searchList: {"0":__('Is_view 0'),"1":__('Is_view 1')}, formatter: Table.api.formatter.normal},
{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;
});