tjbp/tablelegs

轻松创建具有排序、过滤和分页功能的HTML表格。

dev-master 2015-12-28 16:52 UTC

This package is auto-updated.

Last update: 2024-09-15 21:41:44 UTC


README

StyleCI Build Status Total Downloads Latest Stable Version Latest Unstable Version License

Tablelegs 允许您轻松地从数据库中构建HTML表格,包括对过滤器、可排序列和分页的支持。Tablelegs 不输出HTML,它只提供根据专门构建的类输出表格的辅助函数,并可以生成用于启用过滤和排序的URL。

安装

Tablelegs 可以通过 Packagist 使用 Composer 进行安装

用法

扩展 Tablelegs\Table

每个表格都应该有自己的类,尽管如果您有许多相似的表格,您可以轻松地创建一个具有公共属性/方法的基表格,并为每个表格扩展它。考虑将您的所有表格类放置在应用程序中的 Tables 命名空间内。

创建表格所需的最少内容是 $columnHeaders 属性。在实例化您的表格类时,只需传递您的数据库。支持的数据库包括简单的关联数组、关联数组的数字数组、Laravel Collection 或 Laravel Eloquent builder。支持的数据库将自动检测。

您可以通过扩展 Tablelegs\Databases\Database 并实现 Tablelegs\Databases\DatabaseInterface 来支持更多数据库,然后设置您的表格的 $dbClass 属性为其名称。

以下是一个简单的表格类示例

namespace App\Tables;

use Tablelegs\Table;

class ManageUsers extends Table
{
    public $columnHeaders = [
        'user_id' => 'User ID',
        'username' => 'Username',
        'email' => 'Email',
        'joined' => 'Joined',
        'last_login' => 'Last login',
    ];

    public $filters = [
        'Type' => [
            'Administrator',
            'Moderator',
        ],
    ];

    public $presenter = FoundationFivePresenter::class;

    public function filterTypeAdministrator()
    {
        return $this->db->where('administrator', true);
    }

    public function filterTypeSeller()
    {
        return $this->db->where('moderator', true);
    }
}

列标题

列标题在表格类的 $columnHeaders 属性中定义,使用友好的URL名称作为键,自然语言等效值作为值。

排序

Tablelegs 将允许按任何列排序,并且将尝试自行执行此操作。但是,可以使用格式为 sortColumn 的方法覆盖逻辑。此方法将按顺序("asc""desc")作为其唯一参数传递。

过滤器

过滤器在表格类的 $filters 属性中定义,为多维数组。数组的第一个级别的键代表过滤器(过滤键)的自然语言名称。第二级别的值代表过滤器的选项,再次使用自然语言。

每个过滤器都应该定义一个格式为 filterNameOption 的对应方法。该方法应包含逻辑,以适当地过滤类的 $db 属性。上面显示的示例使用 Laravel 的 Eloquent 查询系统;如果您使用数组数据库,则可能利用 array_filter()

请注意,在URL中,过滤器的名称和选项将自动小写,并将空格替换为下划线,这在很大程度上是为了美观。

输出表格(纯PHP)

<?php

// This is the same table definition as in the example above
$table = new ManageUsers(User::query());

$users = $table->paginate();

?>
<!-- Loop through the filters, outputting the name followed by a link to enable each option -->
<?php foreach ($table->getFilters() as $filter): ?>
    <?php echo $filter->getName() ?>:
    <?php foreach ($filter->getOptions() as $filter_option_key => $filter_option_name): ?>
        <a href="<?php echo $filter->getUrl($filter_option_key) ?>"><?php echo $filter_option_name ?></a>
    <?php endforeach; ?>
<?php endforeach; ?>
<table>
    <thead>
        <tr>
            <!-- Loop through the column headers, outputting the names as links for sorting, and an icon indicating the sort order -->
            <?php foreach ($table->getColumnHeaders() as $column_header): ?>
                <th>
                    <a href="<?php echo $column_header->getUrl() ?>"><?php echo $column_header->getName() ?></a>
                    <?php if ($column_header->isSortKey()): ?>
                        <?php echo $table->getSortOrder() == 'asc' ? '' : '' ?>
                    <?php endif; ?>
                </th>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <!-- Loop through the query results -->
        <?php foreach ($users as $user): ?>
            <tr>
                <td><?php echo $user->getKey() ?></td>
                <td><?php echo $user->username ?></td>
                <td><?php echo $user->email ?></td>
                <td><?php echo date('Y/m/d', $user->joined) ?></td>
                <td><?php echo date('Y/m/d', $user->last_login) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<div>
    <?php echo $table->paginator() ?>
</div>

输出表格(Laravel 5)

在控制器中放置一个方法

public function getUsers()
{
    // This is the same table definition as in the example above
    $table = new ManageUsers(User::query());

    return view('manage.users', [
        'table' => $table,
        'users' => $table->paginate()
    ]);
}

您可以使用 paginate() 方法对结果进行分页,或使用 get() 获取整个数据集。

用于与 Laravel 的 Blade 模板系统和 ZURB Foundation 一起使用的视图也已包含在内,如下例所示

@include('tablelegs::filter')
<table class="expand">
    @include('tablelegs::header')
    <tbody>
        @foreach ($users as $user)
            <tr>
                <td>{{ $user->userId }}</td>
                <td>{!! u($user) !!}</td>
                <td>{{ $user->email }}</td>
                <td>{{ date('Y/m/d', $user->joined) }}</td>
                <td>{{ date('Y/m/d', $user->last_login) }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
<div class="row">
    <div class="medium-12 columns pagination-centered">
        {!! $table->paginator() !!}
    </div>
</div>

许可

Tablelegs 是免费软件,根据 GPL3 许可证 授权。