562 lines
9.4 KiB
PHP
Raw Normal View History

2021-03-29 19:52:01 +08:00
<?php
namespace catcher\library\table;
class Table
{
use Events;
/**
* 头信息
*
* @var array
*/
2021-03-31 20:21:16 +08:00
protected $headers = [];
2021-03-29 19:52:01 +08:00
/**
* table 操作
*
* @var array
*/
protected $actions = [];
/**
* 搜索参数
*
* @var array
*/
protected $search = [];
/**
* 表格引用
*
* @var string
*/
protected $ref;
/**
* @var array
*/
protected $defaultQueryParams = [];
/**
* 表单事件
*
* @var array
*/
protected $events = [];
/**
* 是否隐藏分页
*
* @var bool
*/
2021-04-27 08:47:56 +08:00
protected $hidePagination = false;
2021-03-29 19:52:01 +08:00
/**
* tree table
*
* @var array
*/
protected $tree = [];
/**
* @var string
*/
protected $apiRoute;
/**
* @var bool
*/
protected $loading = false;
/**
* @var array
*/
protected $dialog;
2021-03-31 20:21:16 +08:00
/**
* @var array
*/
protected $filterParams;
2021-04-03 10:35:21 +08:00
/**
* @var string
*/
protected $importRoute;
/**
* @var string
*/
protected $exportRoute;
2021-04-05 22:03:25 +08:00
/**
* @var bool
*/
protected $forceUpdate = false;
2021-04-24 20:30:28 +08:00
/**
* @var array
*/
protected $excel = [];
/**
* 导出 excel 所使用 model
*
* @var string
*/
protected $usedModel;
2021-04-27 08:47:56 +08:00
/**
* 树状展开
*
* @var bool
*/
protected $defaultExpandAll;
/**
* 默认
*
* @var bool
*/
protected $border = true;
2021-04-27 18:33:34 +08:00
/**
* bind table
*
* @var bool
*/
protected $bind = false;
2021-03-29 19:52:01 +08:00
/**
* Table constructor.
* @param string $ref
*/
public function __construct(string $ref)
{
$this->ref = $ref;
}
/**
* 设置头信息
*
* @time 2021年03月21日
* @param array $header
* @return $this
*/
public function header(array $header): Table
{
foreach ($header as $h) {
2021-03-31 20:21:16 +08:00
$this->headers[] = $h->attributes;
2021-03-29 19:52:01 +08:00
}
return $this;
}
/**
* 设置 actions
*
* @time 2021年03月21日
* @param array $actions
* @return $this
*/
public function withActions(array $actions): Table
{
foreach ($actions as $action) {
$this->actions[] = $action->render();
}
return $this;
}
/**
* 设置搜索参数
*
* @time 2021年03月21日
* @param array $search
* @return $this
*/
public function withSearch(array $search): Table
{
$this->search = $search;
return $this;
}
/**
* 表单事件
*
* @time 2021年03月21日
* @param array $events
* @return $this
*/
public function withEvents(array $events): Table
{
$this->events = $events;
return $this;
}
2021-04-24 20:30:28 +08:00
/**
* excel 信息
*
* @time 2021年04月21日
* @param array $excel
* @param string $usedModel
* @return $this
*/
public function withUsedModelAndExcel(string $usedModel, array $excel = []): Table
2021-04-24 20:30:28 +08:00
{
foreach ($excel as $e) {
$this->excel[] = $e->render();
}
$this->usedModel = $usedModel;
return $this;
}
2021-03-29 19:52:01 +08:00
/**
* set
*
* @time 2021年03月29日
* @param array $params
* @return $this
*/
public function withDefaultQueryParams(array $params): Table
{
$this->defaultQueryParams = $params;
return $this;
}
2021-03-31 20:21:16 +08:00
/**
* filter params
*
* @time 2021年03月30日
* @param array $filterParams
* @return $this
*/
public function withFilterParams(array $filterParams): Table
{
$this->filterParams = $filterParams;
return $this;
}
2021-03-29 19:52:01 +08:00
/**
* 隐藏分页
*
* @time 2021年03月29日
* @return $this
*/
public function withHiddenPaginate(): Table
{
2021-04-27 08:47:56 +08:00
$this->hidePagination = true;
return $this;
}
/**
* 展开
*
* @time 2021年04月26日
* @param bool $expand
* @return $this
*/
public function expandAll($expand = true): Table
{
$this->defaultExpandAll = $expand;
2021-03-29 19:52:01 +08:00
return $this;
}
/**
* 设置 api route
*
* @time 2021年03月29日
* @param string $apiRoute
* @return $this
*/
public function withApiRoute(string $apiRoute): Table
{
$this->apiRoute = $apiRoute;
return $this;
}
/**
* loading
*
* @time 2021年03月29日
* @return $this
*/
public function withLoading(): Table
{
$this->loading = true;
return $this;
}
/**
* 设置弹出层的宽度
*
* @time 2021年03月29日
* @param string $width
* @return $this
*/
public function withDialogWidth(string $width): Table
{
$this->dialog['width'] = $width;
return $this;
}
2021-04-03 10:35:21 +08:00
/**
* 导出路由
*
* @time 2021年04月02日
* @param string $route
* @return $this
*/
public function withImportRoute(string $route): Table
{
$this->importRoute = $route;
return $this;
}
/**
* 导出路由
*
* @time 2021年04月02日
* @param string $route
* @return $this
*/
public function withExportRoute(string $route): Table
{
$this->exportRoute = $route;
return $this;
}
2021-04-27 18:33:34 +08:00
/**
* table 使用 v-bind
*
* @time 2021年04月27日
* @return $this
*/
public function withBind(): Table
{
$this->bind = true;
return $this;
}
2021-04-03 10:35:21 +08:00
2021-03-29 19:52:01 +08:00
/**
* 变成 tree table
*
* @time 2021年03月29日
* @param string $rowKey
* @param array $props ['children' => '', 'hasChildren' => '']
* @return $this
*/
public function toTreeTable(string $rowKey = 'id', array $props = []): Table
{
$this->tree['row_key'] = $rowKey;
$this->tree['props'] = count($props) ? $props : [
'children' => 'children',
'hasChildren' => 'hasChildren'
];
return $this;
}
2021-04-05 22:03:25 +08:00
/**
* 强制更新组件
*
* @time 2021年04月05日
* @return $this
*/
public function forceUpdate(): Table
{
$this->forceUpdate = true;
return $this;
}
2021-03-29 19:52:01 +08:00
/**
* 渲染
*
* @time 2021年03月21日
* @return array
*/
public function render(): array
{
$render = [];
foreach (get_class_vars(self::class) as $property => $v) {
if (!empty($this->{$property})) {
$render[$property] = $this->{$property};
}
}
return $render;
}
/**
* 追加 headers
*
* @time 2021年03月28日
* @param $header
* @return $this
*/
public function appendHeaders($header): Table
{
if ($header instanceof HeaderItem) {
2021-03-31 20:21:16 +08:00
$this->headers[] = $header;
2021-03-29 19:52:01 +08:00
}
if (is_array($header)) {
2021-03-31 20:21:16 +08:00
$this->headers = array_merge($this->headers, $header);
2021-03-29 19:52:01 +08:00
}
return $this;
}
/**
* 追加
*
* @time 2021年03月29日
* @param string $param
* @return $this
*/
public function appendDefaultQueryParams(string $param): Table
{
$this->defaultQueryParams[] = $param;
return $this;
}
/**
* 追加 events
*
* @time 2021年03月28日
* @param array $events
* @return $this
*/
public function appendEvents(array $events): Table
{
$this->events = array_merge($this->events, $events);
return $this;
}
/**
* 追加 events
*
* @time 2021年03月28日
* @param array $actions
* @return $this
*/
public function appendActions(array $actions): Table
{
$this->actions = array_merge($this->actions, $actions);
return $this;
}
/**
* 追加 header
*
* @time 2021年03月28日
* @param array $header
* @return $this
*/
public function appendHeader(array $header): Table
{
2021-03-31 20:21:16 +08:00
$this->headers = array_merge($this->headers, $header);
return $this;
}
/**
* 新增 filter params
*
* @time 2021年03月30日
* @param array $params
* @return $this
*/
public function appendFilterParams(array $params): Table
{
$this->filterParams = array_merge($this->filterParams, $params);
2021-03-29 19:52:01 +08:00
return $this;
}
/**
* 获取头部
*
* @time 2021年03月29日
* @return array
*/
public function getHeader(): array
{
2021-03-31 20:21:16 +08:00
return $this->headers;
2021-03-29 19:52:01 +08:00
}
/**
* 获取事件
*
* @time 2021年03月29日
* @return array
*/
public function getEvents(): array
{
return $this->events;
}
/**
* 获取表格操作
*
* @time 2021年03月29日
* @return array
*/
public function getActions(): array
{
return $this->actions;
}
/**
* 获取 默认 query params
*
* @time 2021年03月29日
* @return array
*/
public function getDefaultQueryParams(): array
{
return $this->defaultQueryParams;
}
2021-03-31 20:21:16 +08:00
/**
* get filter params
*
* @time 2021年03月30日
* @return array
*/
public function getFilterParams(): array
{
return $this->filterParams;
}
2021-03-29 19:52:01 +08:00
}