stevebauman / eloquenttable
为 Laravel 集合生成 HTML 表格的工具
该包的官方仓库似乎已丢失,因此该包已被冻结。
Requires
- php: >=5.4.0
- illuminate/support: 4.* | 5.*
README
🚨 被遗弃 🚨
强烈推荐使用 Orchestra/HTML 而不是这个包,因为他们的解决方案经过了进一步测试,更加完整和健壮。此包将不会收到 任何更新。
如果您想成为这个包的维护者,请通过 steven_bauman@outlook.com 联系我,或创建一个问题。
谢谢!
描述
Eloquent table 是一个为 Laravel 集合生成 HTML 表格的工具。
安装
在 composer.json
中包含该包
"stevebauman/eloquenttable": "1.1.*"
现在执行 composer update
。
Laravel 4
在 app/config/app.php
配置文件的最底部包含服务提供者
'Stevebauman\EloquentTable\PaginationServiceProvider',
'Stevebauman\EloquentTable\EloquentTableServiceProvider',
发布配置文件(可选)
php artisan config:publish stevebauman/eloquenttable
Laravel 5
在 config/app.php
配置文件的最底部包含服务提供者
'Stevebauman\EloquentTable\EloquentTableServiceProvider',
发布配置文件(在 Laravel 5 中是强制性的)
php artisan vendor:publish
您已经准备好了!
注意:由于分页更改,以下 Laravel 5 中的
showPages()
方法不可用。您需要使用以下示例中的render()
方法手动显示页面:https://laravel.net.cn/docs/5.0/pagination#usage
用法
在您的模型上插入特性
class Book extends Eloquent {
use \Stevebauman\EloquentTable\TableTrait;
protected $table = 'books';
}
像平常一样从模型中获取记录
$books = Books::get();
return view('books.index', compact('books'));
在您的 blade 视图中,我们只需指定我们想要显示的列,然后调用 render 方法
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By'
))
->render()
!!}
使用 means($column, $relationship)
处理关系值
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
))
->means('owned_by', 'user.first_name')
->render()
!!}
在这种情况下,模型 books 需要有一个 user 方法来定义它与此关系的关系。
您还必须使用 '点' 表示法来表示关系。
使用 modify($column, $closure)
自定义列值的显示
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
))
->means('owned_by', 'user')
->modify('owned_by', function($user, $book) {
return $user->first_name . ' ' . $user->last_name;
})
->render()
!!}
使用 modify,我们可以指定我们想要修改的列,该函数将返回当前关系记录(如果该列是关系),以及当前基础记录,在这种情况下是 book。
使用 modifyCell($column, $closure)
自定义列中每个单元格的属性
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
))
->means('owned_by', 'user')
->modifyCell('owned_by', function($user) {
return array('class' => $user->role);
})
->render()
!!}
使用 modifyCell,我们可以指定我们想要修改的单元格的列,该函数应该返回要添加到单元格中的属性数组。
使用 modifyRow($name, $closure)
自定义表中每行的属性
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
))
->means('owned_by', 'user')
->modifyRow('mod1', function($user) {
return array('id' => 'user-'.$user->id);
})
->render()
!!}
使用 modifyRow,我们可以添加命名修改(在我们的上一个例子中是 'mod1'),该函数应该返回要添加到每行中的属性数组。
使用 eloquent-table,我们还可以轻松地为列生成可排序的链接
在您的控制器中
$books = Book::sort(Input::get('field'), Input::get('sort'))->get();
在您的视图中
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
))
->sortable(array('id', 'title'))
->render()
!!}
列标题中将生成一个可点击的链接。生成的 HTML 将看起来像
<a class="link-sort" href="http://www.example.com/books?field=id&sort=desc">
ID <i class="fa fa-sort"></i>
</a>
如果我们想将所有这些结合在一起,与分页和排序一起呢?很简单
在您的控制器中
$books = Book::sort(Input::get('field'), Input::get('sort'))->paginate(25);
return view('books.index', compact('books'));
在您的视图中
{!!
$books->columns(array(
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By',
'owned_by' => 'Owned By',
'publisher' => 'Publisher',
))
->means('owned_by', 'user')
->modify('owned_by', function($user, $book) {
return $user->first_name . ' ' . $user->last_name;
})
->means('publisher', 'publisher')
->modify('publisher', function($publisher, $book) {
return 'The publisher of this book: '. $publisher->name;
})
->sortable(array('id', 'title'))
->showPages()
->render()
!!}
如果我想要为一个关系生成一个表格怎么办?
在您的控制器中
$book = Book::with('authors')->find(1);
return view('book.show', compact('book'));
在这种情况下,book 将有多个作者(hasMany
关系)
在您的视图中
{!!
$book->authors->columns(
'id' => 'ID',
'name' => 'Name',
'books' => 'Total # of Books'
)
->means('books', 'num_of_books')
->render()
!!}
请注意,我们无法对关系表进行分页,或提供可排序的列。如果您需要这些,请单独获取它们
在您的控制器中
$book = Book::find(1);
$authors = Authors::where('book_id', $book->id)->paginate(25);
return view('books.show', array(
'book' => $book,
'authors' => $authors,
));
在您的视图中
{!!
$authors->columns(array(
'name' => 'Name',
))->render()
!!}
使用 attributes($attributes = array())
自定义表属性
{!!
$authors->columns(array(
'name' => 'Name',
))
->attributes(array(
'id' => 'table-1',
'class' => 'table table-striped table-bordered',
))
->render()
!!}
在其他地方显示您的页面
只需不要在集合上调用showPages()
方法,并将您的页面放置在您的页面上,就像您通常做的那样。
{!!
$authors->columns(array(
'name' => 'Name',
))
->attributes(array(
'id' => 'table-1',
'class' => 'table table-striped table-bordered',
))
->render()
!!}
<div class="text-center">{!! $authors->appends(Input::except('page'))->links() !!}</div>
为什么需要分页服务提供商?
当在您的模型和/或集合上调用paginate()
时,会返回一个不同的集合实例。遗憾的是,唯一的解决方案是覆盖默认的分页器实例。然而,这个分页器扩展了Laravel内置的分页器,所以没有任何功能被移除或丢失。