aginev/datagrid

Laravel v5+ 数据网格包

3.0.0 2022-03-19 17:39 UTC

This package is auto-updated.

Last update: 2024-09-19 23:07:46 UTC


README

该包可以将模型集合轻松转换为数据网格表。包的主要目标是为您构建一个具有列排序和过滤功能的表格。您在控制器中定义网格结构,将数据网格传递到视图中并显示。这将为您提供一个非常干净的视图,只需一行即可显示表格 + 过滤 + 排序 + 分页。请注意,过滤和排序数据由您自行负责!

特性

  • Composer 可安装
  • PSR4 自动加载
  • 具有过滤行
  • 具有列排序顺序
  • 可以轻松添加操作列,包含编辑/删除等链接
  • 可以通过闭包函数修改单元格数据
  • 兼容 Bootstrap
  • 每个列都具有基于列数据键的数据属性

需求

仅构建用于 Laravel 的包!

安装

在您的 composer.json 文件中要求包,如下所示

{
    "require": {
        "aginev/datagrid": "3"
    }
}

告诉 composer 更新您的依赖项

composer update

或在终端中

composer require aginev/datagrid

如何使用

让我们假设我们系统中有一个用户表和用户角色(角色)表。

用户表

id: 主键

role_id: 到角色表主键的外键

email: 用户电子邮件,用作用户名

first_name: 用户名

last_name: 用户姓

password: 哈希密码

created_at: 创建时间

updated_at: 最后更新时间

角色表

id: 主键

title: 角色标题,例如“管理员访问”

created_at: 创建时间

updated_at: 最后更新时间

我们需要一个包含所有用户数据、他们的角色、编辑和删除链接(在表格最后一列)、过滤和排序链接(在顶部)、分页(在底部)的表格。

<?php

// Grab all the users with their roles
// NB!!! At the next line you are responsible for data filtration and sorting!
$users = User::with('role')->paginate(25);

// Create Datagrid instance
// You need to pass the users and the URL query params that the package is using
$grid = new \Datagrid($users, Request::get('f', []));

// Or if you do not want to use the alias
//$grid = new \Aginev\Datagrid\Datagrid($users, Request::get('f', []));

// Then we are starting to define columns
$grid
	->setColumn('first_name', 'First Name', [
		// Will be sortable column
		'sortable'    => true,
		// Will have filtered
		'has_filters' => true
	])
	->setColumn('email', 'Email', [
		'sortable'    => true,
		'has_filters' => true,
		// Wrapper closure will accept two params
		// $value is the actual cell value
		// $row are the all values for this row
		'wrapper'     => function ($value, $row) {
			return '<a href="mailto:' . $value . '">' . $value . '</a>';
		}
	])
	->setColumn('role_id', 'Role', [
		// If you want to have role_id in the URL query string but you need to show role.name as value (dot notation for the user/role relation)
		'refers_to'   => 'role.name',
		'sortable'    => true,
		'has_filters' => true,
		// Pass array of data to the filter. It will generate select field.
		'filters'     => Role::all()->lists('title', 'id'),
		// Define HTML attributes for this column
		'attributes'  => [
            'class'         => 'custom-class-here',
            'data-custom'   => 'custom-data-attribute-value',
        ],
	])
	->setColumn('created_at', 'Created', [
		'sortable'    => true,
		'has_filters' => true,
		'wrapper'     => function ($value, $row) {
			// The value here is still Carbon instance, so you can format it using the Carbon methods
			return $value;
		}
	])
	->setColumn('updated_at', 'Updated', [
		'sortable'    => true,
		'has_filters' => true
	])
	// Setup action column
	->setActionColumn([
		'wrapper' => function ($value, $row) {
			return '<a href="' . action('HomeController@index', $row->id) . '" title="Edit" class="btn btn-xs"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
					<a href="' . action('HomeController@index', $row->id) . '" title="Delete" data-method="DELETE" class="btn btn-xs text-danger" data-confirm="Are you sure?"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>';
		}
	]);

// Finally, pass the grid object to the view
return view('grid', ['grid' => $grid]);

在视图中显示网格。grid-table 参数不是必需的,它是表格的 ID。

...
{!! $grid->show('grid-table') !!}
...

修改默认视图

php artisan vendor:publish --provider="Aginev\Datagrid\DatagridServiceProvider" --tag="views"

这将把视图复制到 resources/views/vendor/datagrid/datagrid.blade.php。编辑此文件,您将能够修改网格视图,而不会丢失您的更改。

修改配置

php artisan vendor:publish --provider="Aginev\Datagrid\DatagridServiceProvider" --tag="config"

这将把配置复制到 config/datagrid.php

许可

MIT - https://open-source.org.cn/licenses/MIT