attributes = new Attributes(); $this->dataUrl = request()->path(); $this->model = new Model($model, $this); if ($model) { $this->table = $model->getTable(); $this->keyName = $model->getKeyName(); $this->defaultSort($model->getKeyName(), "asc"); } $this->isGetData = request()->header('getData') == "true"; $this->toolbars = new Toolbars($this); $this->batchActions = new BatchActions(); $this->filter = new Filter($this->model); } /** * 获取自定义数据模型 * @return Model|Builder */ public function model() { return $this->model; } /** * 自定义数据源路径 * @param string $dataUrl * @return $this */ public function dataUrl(string $dataUrl) { $this->dataUrl = $dataUrl; return $this; } /** * 设置请求方式 * @param string $method * @return $this */ public function method(string $method) { $this->method = $method; return $this; } /** * @return array */ public function getAppendFields(): array { return $this->appendFields; } /** * 数据返回附加字段 * @param array $appendFields * @return $this */ public function appendFields(array $appendFields) { $this->appendFields = $appendFields; return $this; } /** * @return bool */ public function isGetData(): bool { return $this->isGetData; } /** * 设置树形表格 * @param bool $tree * @return $this */ public function tree($tree = true) { $this->tree = $tree; return $this; } /** * Grid添加字段 * @param string $name 对应列内容的字段名 * @param string $label 显示的标题 * @param string $type 表单类型 * @return Column */ public function column($name, $label = '', $type ='') { if (Str::contains($name, '.')) { $this->addRelationColumn($name, $label); } return $this->addColumn($name, $label, $type); } /** * 添加多列数据 * @param array $column * @return $this */ public function addColumns(array $column = []) { if (!empty($columns)) { foreach ($columns as $column) { call_user_func_array([$this, 'addColumn'], $column); } } return $this; } /*** * 添加列 * @param string $name * @param string $title 标题 * @param null $type 字段类型 * @param string $default * @param array $param * @return Column */ protected function addColumn($name = '', $title = '', $type ='',$default='',$param=[]) { $column = new Column($name, $title, $type,$default,$param); $column->setGrid($this); $this->columns[] = $column; return $column; } /** * Add a relation column to grid. * @param string $name * @param string $label * @return $this|bool|Column */ protected function addRelationColumn($name, $label = '') { if ($this->model) { list($relation, $column) = explode('.', $name); $model = $this->model()->eloquent(); if (!method_exists($model, $relation) || !$model->{$relation}() instanceof Relations\Relation) { } else { $this->model()->with($relation); } } } /** * 获取列表属性 * @param Column[] $columns */ protected function columns($columns) { $this->columnAttributes = collect($columns)->map(function (Column $column) { return $column->getAttributes(); })->toArray(); } public function getColumns() { return $this->columns; } public function applyWhere() { //快捷搜索 $this->applyQuickSearch(); } public function applyQuery() { //快捷搜索 $this->applyQuickSearch(); $this->applyFilter(false); } /** * 自定义toolbars * @param $closure * @return $this */ public function toolbars($closure) { call_user_func($closure, $this->toolbars); return $this; } /** * 自定义行操作 * @param $closure * @return $this */ public function actions($closure) { $this->actions = $closure; return $this; } /** * 自定义批量操作 * @param \Closure $closure * @return $this */ public function batchActions(\Closure $closure) { call_user_func($closure, $this->batchActions); return $this; } /** * 获取行操作 * @param $row * @param $key * @return mixed */ public function getActions($row, $key) { $actions = new Actions($this); $actions->row($row)->key($key); if ($this->actions) call_user_func($this->actions, $actions); return $actions->builderActions(); } /** * @param $closure * @return $this */ public function top($closure) { $this->top = new Content(); call_user_func($closure, $this->top); return $this; } /** * @param $closure * @return $this */ public function bottom($closure) { $this->bottom = new Content(); call_user_func($closure, $this->bottom); return $this; } /** * 自定义数据 * @param $data * @param $current_page * @param $per_page * @param $last_page * @param $total * @return $this */ public function customData($data, $current_page = 0, $per_page = 0, $last_page = 0, $total = 0) { $this->customData = [ 'current_page' => (int)$current_page, 'per_page' => (int)$per_page, 'last_page' => (int)$last_page, 'total' => (int)$total, 'items' => $data, ]; return $this; } /*** * 设置自定义数据 * @param $data * @param int $current_page * @param int $per_page * @param int $last_page * @param int $total * @return $this */ public function setRowList($data, $current_page = 0, $per_page = 0, $last_page = 0, $total = 0) { $this->customData = [ 'current_page' => (int)$current_page, 'per_page' => (int)$per_page, 'last_page' => (int)$last_page, 'total' => (int)$total, 'items' => $data, ]; return $this; } /** * data * @return array */ protected function data() { if ($this->customData) { var_dump(9999); // $this->customData['data'] = $this->model()->displayData($this->customData['data']); return [ 'code' => 200, 'data' => $this->isHidePage() ? $this->customData['data'] : $this->customData ]; } $this->applyQuery(); $data = $this->model->buildData(); return [ 'code' => 200, 'data' => $data ]; } /** * 输出JSON * @inheritDoc */ public function jsonSerialize(): array { if (count($this->columnAttributes) <= 0) { $this->columns($this->columns); } if ($this->isGetData) { return $this->data(); } else { $viewData['componentName'] = $this->componentName; //模板名 $viewData['crudOptions'] =$this->attributes; //表单参数 $viewData['dataUrl'] = $this->dataUrl; //数据请求地址 $viewData['method'] = $this->method; //请求方式 $viewData['topButtons'] = $this->top_buttons; //系统默认按钮 $viewData['rowColumn'] = $this->columnAttributes;//列表 // $viewData['attributes'] = $this->attributes;//列表 // var_dump($this->attributes); // $viewData['ref'] = $this->getRef(); //自定义事件 // $viewData['quickSearch'] = $this->quickSearch; //快速搜索 // $viewData['toolbars'] = $this->toolbars->builderData(); //操作工具 // $viewData['batchActions'] = $this->batchActions->builderActions(); //自定义TOP操作工具 // $viewData['selection'] = $this->attributes->selection;//搜索 // 'topBottom' =>$this->top_buttons, //数据请求地址 // $viewData['keyName'] = // $viewData['filter'] = $this->filter->buildFilter();//过滤 // $viewData['tree'] = $this->tree;//信息树 // $viewData['hidePage'] = $this->isHidePage(); //分页状态 // $viewData['pageSizes'] = $this->pageSizes; // $viewData['perPage'] = $this->perPage; //分页默认数量 // $viewData['dialogForm'] =[ // 'dialogTitle'=> $this->dialogTitle, // 'dialogFormWidth'=>$this->dialogFormWidth, // 'Form'=>$this->dialogForm, // ] ; //弹窗表单 // $viewData['pageBackground'] = $this->pageBackground; //页面背景 // $viewData['pageLayout'] = $this->pageLayout; //页面布局 // $viewData['export'] = $this->enableExport; //数据导出 // $viewData['defaultSort'] = $this->defaultSort;//默认排序 // $viewData['attributes'] = (array)$this->attributes;//属性 // $viewData['top'] = $this->top; //顶部 // $viewData['bottom'] = $this->bottom; //按钮 return $viewData; } } /** * @return string */ public function getComponentName(): string { return $this->componentName; } /** * @param string $componentName */ public function setComponentName(string $componentName) { $this->componentName = $componentName; return $this; } /** * @param bool $isGetData */ public function setGetData(bool $isGetData): void { $this->isGetData = $isGetData; } public function getTable() { return $this->table; } }