基本使用


表格前端组件是基于element-uiTable 表格实现的,具体可查看 官方文档

简单示例

生成模型与数据库迁移文件

  1. php artisan make:model Models\\Movie -m

编辑database\migrations\2020_xx_xx_xx_create_movies_table.php文件

  1. class CreateMoviesTable extends Migration
  2. {
  3. /**
  4. * Run the migrations.
  5. *
  6. * @return void
  7. */
  8. public function up()
  9. {
  10. Schema::create('movies', function (Blueprint $table) {
  11. $table->id();
  12. $table->string("title")->comment("标题");
  13. $table->string("cover")->comment("封面");
  14. $table->string("describe")->comment("描述");
  15. $table->tinyInteger("rate")->comment("评分");
  16. $table->boolean("released")->default(true)->comment("发布");
  17. $table->timestamps();
  18. });
  19. }
  20. /**
  21. * Reverse the migrations.
  22. *
  23. * @return void
  24. */
  25. public function down()
  26. {
  27. Schema::dropIfExists('movies');
  28. }
  29. }

执行迁移

  1. php artisan migrate

app\Admin\Controllers创建控制器MovieController

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Movie;
  4. use Illuminate\Support\Str;
  5. use SmallRuralDog\Admin\Components\Grid\Boole;
  6. use SmallRuralDog\Admin\Components\Grid\Image;
  7. use SmallRuralDog\Admin\Controllers\AdminController;
  8. use SmallRuralDog\Admin\Grid;
  9. class MovieController extends AdminController
  10. {
  11. public function grid()
  12. {
  13. $grid = new Grid(new Movie());
  14. $grid->column('id', 'ID')->sortable();
  15. $grid->column('title', '标题');
  16. $grid->column('cover', '封面')->component(Image::make()->size(70, 100));
  17. $grid->column('describe', '描述')->customValue(function ($row, $value) {
  18. return Str::limit($value, 50);
  19. });
  20. $grid->column('rate', '评分');
  21. $grid->column('released', '发布')->component(Boole::make());
  22. return $grid;
  23. }
  24. public function form($isEdit = false)
  25. {
  26. }
  27. }

app\Admin\routes.php注册路由

<?php

use Illuminate\Routing\Router;

Route::group([
    'domain'=>config('admin.route.domain'),
    'prefix' => config('admin.route.api_prefix'),
    'namespace'=>config('admin.route.namespace'),
    'middleware' => config('admin.route.middleware'),
], function (Router $router) {
    .....
    $router->resource('movies','MovieController');
});

浏览器访问http://你的域名/admin#/movies

表格属性

表格对象属性

高度

Table 的高度,默认为自动高度。如果 height 为 number 类型,单位 px;如果 height 为 string 类型,则这个高度会设置为 Table 的 style.height 的值,Table 的高度会受控于外部样式。

$grid->height('500px');
$grid->height(500);

自动高度

js计算页面高度,使表格高度撑满窗口

$grid->autoHeight();

最大高度

Table 的最大高度。合法的值为数字或者单位为 px 的高度。

$grid->maxHeight('500px');
$grid->maxHeight(500);

斑马纹

是否为斑马纹 table

$grid->stripe();
$grid->stripe(true);
$grid->stripe(false);

纵向边框

是否带有纵向边框

 $grid->border();
 $grid->border(true);
 $grid->border(false);

尺寸

Table 的尺寸

可选择 medium small mini

$grid->size('medium');
$grid->size('small');
$grid

宽度是否自撑开

列的宽度是否自撑开

$grid->fit();
$grid->fit(true);
$grid->fit(false);

显示表头

是否显示表头

$grid->showHeader();
$grid->showHeader(true);
$grid->showHeader(false);

高亮当前行

是否要高亮当前行

$grid->highlightCurrentRow();
$grid->highlightCurrentRow(false);

空数据

空数据时显示的文本内容,只支持纯文本

$grid->emptyText("暂无数据");

多选

Table 多选

$grid->selection();

默认排序

当前模型的默认排序,不设置为模型key desc

$grid->defaultSort('id', 'asc');

数据状态保存

把数据存入vuex,默认为不保存

$grid->dataVuex();

分页区间

设置 Table 的分页属性,默认[10, 20, 30, 40, 50, 100]

$grid->pageSizes([10, 20, 30, 40, 50, 100]);

每页大小

默认 20,如果需要不分页,可把perPage设置一个很大的数

$grid->perPage(20);

分页背景色

是否为分页按钮添加背景色,默认false

$grid->pageBackground();

隐藏分页

不启用分页

$grid->hidePage();

数据源附加字段

字段没定义也想在数据源中返回

$grid->appendFields([]);

数据源请求方式

$grid->method("post");

自定义数据源

 $grid->customData($data, $current_page = 0, $per_page = 0, $last_page = 0, $total = 0);

可用方法

方法 描述 类型 可选值/示例值 默认
actionAlign($actionAlign) 操作栏对齐 string left right center left
actionFixed($actionFixed) 操作栏固定位置 string left right center
actionLabel($actionLabel) 操作栏名称 string 操作
actions($closure) 自定义行操作 function(Actions $a)
actionWidth($actionWidth) 操作栏宽度 string 80px
appendFields($appendFields) 数据源返回附加字段 array [“uid”,”user.id”]
autoHeight() js计算页面高度,使表格高度撑满窗口
batchActions(\Closure $closure) 自定义批量操作 function(BatchActions $ba)
border() 是否带有纵向边框
bottom($closure) 表格底部自定义组件 function(Content $c)
column($name, $label = ‘’, $columnKey = null) 添加字段
customData($data, $current_page = 0,$per_page = 0, $last_page = 0, $total = 0) 自定义数据
dataUrl($dataUrl) 自定义数据源路径
dataVuex() 表格数据是否存入vuex
defaultExpandAll() 是否默认展开所有行,当 Table 包含展开行存在或者为树形表格时有效
defaultSort($prop, $order, $field = null) 默认排序
dialogForm(Form $dialogForm, $width = ‘500px’, $title = [‘添加’, ‘修改’]) 弹窗模式表单
emptyText($emptyText) 空数据时显示的文本内容 string
filter(Closure $callback) 表单搜索 function(Filter $f)
fit() 列的宽度是否自撑开 bool
height() Table 的高度 500px
hideActions() 隐藏操作栏 bool
hidePage() 隐藏分页 bool
highlightCurrentRow() 是否要高亮当前行 bool
maxHeight() Table 的最大高度 number/string
method() 数据源URL请求方式 string get / post get
pageBackground() 是否为分页按钮添加背景色 bool
pageLayout() 设置分页布局,子组件名用逗号分隔 string prev, pager, next, jumper, ->, total
pageSizes() 每页显示个数选择器的选项设置 array [10,20,30,40,50]
perPage() 每页显示条目个数 number
quickSearch() 字段快捷搜索 string/array [“name”,”content”]
quickSearchPlaceholder() 设置快捷搜索占位符,开启快捷搜索后生效 string
ref() 注册ref名称 string
refData(string $ref, $refData, string $event = “click”) ref动态代码注入
selection() 是否开启多选 bool
showHeader() 是否显示表头 bool
size() Table 的尺寸 string medium / small / mini
stripe() 是否为斑马纹 table bool
toolbars() 自定义toolbars function(Toolbars $t)
top() 表格头部自定义组件 function(Content $c)
tree() 设置树形表格 bool