llama-laravel/table-view

Laravel 5 包,用于轻松显示带有内置搜索和排序功能的 Eloquent 集合的表格视图。

dev-master 2016-12-25 23:02 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:21:33 UTC


README

Laravel 5 包,用于轻松显示带有内置搜索和排序功能的 Eloquent 集合的表格视图。

安装

更新您的 composer.json 文件,将其作为依赖项包含此包。

"llama-laravel/table-view": "dev-master"

通过将其添加到 config/app.php 文件中的 providers 数组中,注册 TableView 服务提供者。

'providers' => array(
    Llama\TableView\TableViewServiceProvider::class
)

如果您想,您可以通过将其添加到 config/app.php 文件中的 aliases 数组中来别称 TableView 门面。

'aliases' => array(
        'TableView' => Llama\TableView\Facades\TableViewFacade::class,
)

配置

通过运行以下命令将供应商文件 views 和 assets 复制到您的项目中:

php artisan vendor:publish

这将向 public/vendor/table-view 添加多个样式和一个脚本。插件依赖于 jQuery,v1.9.1 将包含在 public/vendor/table-view 中 - Bootstrap CSS v3.3.2 - Font Awesome v4.3.0 - jQuery v1.9.1

用法

通过传递 \Illuminate\Eloquent\Builder 实例或简单地将表视图的模型类名传递给初始化表视图。

	$users = User::select('id', 'name', 'email', 'created_at');

	$usersTableView = TableView::collection( $users )
	// or $usersTableView = TableView::collection( \App\User::class )

向 tableview 添加列

	addColumn($usersTableView
		// you can pass in the title for the column, and the Eloquent\Model property name
		->addColumn('Email', 'email')

		// Add a colon after the Eloquent\Model property name along with sort and/or search to enable these options
		->addColumn('Name', 'name:sort,search')

		// Set the default sorting property with 
		->addColumn('Name', 'name:sort*,search')	// Sorted Ascending by default or specify
		->addColumn('Name', 'name:sort*:asc')
		->addColumn('Name', 'name:sort*:desc')

		// Custom column values are created by passing an array with the Eloquent\Model property name as the key
		//  and a closure function
		->addColumn('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])



		// OR
		->addColumn(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		})
		->addColumn('Email', 'email:sort,search')
		->addColumn(function ($user) 
		{
			return '<a class="btn btn-success" href="/users/' . $user->id . '">View</a>';
		});

从 tableview 移除列

	$usersTableView->removeColumn('Email', 'Name');

自定义列值

	addColumn($usersTableView
		// You can pass in an array for the column's row value with the Eloquent\Model property name as the key
		//  and a closure function
		->addColumn('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])
		// OR if sorting and searching is unnecessary, simply pass in the Closure instead of the array
		->addColumn('Image', function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		})
		// Using modify, we can specify the column of the cell we want to modify, and the function should return an array of attributes to be added to the cell.
		->modifyColumn('Image', ['created_at:sort*' => function ($user) 
		{
			return 'something';
		}]);
}]);

无标题的列

	addColumn($usersTableView
		// Just leave the column title out if you don't want to use it
		->addColumn(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		});

最后,构建 TableView 并将其传递到视图中

	$usersTableView = $usersTableView->build();

	return view('test', [
		'usersTableView' => $usersTableView
	]);

全部通过链式调用

Route::get('/', function(\Illuminate\Http\Request $request) 
{
	$users = User::select('id', 'name', 'email', 'created_at');

	$usersTableView = TableView::collection( $users, 'Administrator' )
		->addColumn(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		})
		->addColumn('Name', 'name:sort,search')
		->addColumn('Email', 'email:sort,search')
		->addColumn('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])
		->addColumn(function ($user) 
		{
			return '<a class="btn btn-success" href="/users/' . $user->id . '">View</a>';
		})
		->build();

	return view('test', [
		'usersTableView' => $usersTableView
	]);
});

前端

包含 Bootstrap 和 Font Awesome 的样式表 - Bootstrap CSS v3.3.2 和 Font Awesome v4.3.0 已包含在供应商中

<link href="{{ asset('vendor/table-view/bootstrap.min.css') }}" rel="stylesheet" />
<link href="{{ asset('vendor/table-view/font-awesome/css/font-awesome.min.css') }}" rel="stylesheet" />
<link href="{{ asset('vendor/table-view/css/themes/tableview-a.css') }}" rel="stylesheet" />

在视图中包含 tablview,并引用其给定的变量名

@include('table-view::container', ['tableView' => $usersTableView])

中间件 Cookie 存储

使用内置中间件轻松将 tableview 的选项添加到 Cookie 存储。

排序选项和每页限制分别添加到永久存储。在任何时候,返回 tableview 的用户将看到这些选项填充了他/她在最近会话中选择的相同值。

搜索查询和页码将在用户的当前会话中临时存储。使用此功能,用户可以访问 http://tableview.com/blog-articles 并显示具有文章列表的 tableview。当用户查看特定文章,如 http://tableview.com/blog-articles/laravel-blog/article 时,任何返回到 http://tableview.com/blog-articles 的链接都将显示具有最新页码和搜索查询的 tableview。

您只需做以下几步

编辑 app/Http/Kernel.php,添加对中间件的引用

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

        // Laravel TableView Middleware
        'table-view.storage' => \Llama\TableView\Middleware\TableViewCookieStorage::class,
    ];

然后将其添加到包含 tableview 的路由中

    Route::get('/', ['middleware' => 'table-view.storage', function () {

就这样!

它很特别,但只需几行代码,您就有一个具有强大功能的动态表格视图。请随意自定义 tableview 和元素部分视图。即将推出更多主题和样式。