synergitech/fuel-datatables

v1.0.0 2020-04-29 08:55 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:16 UTC


README

Build status

使用FuelPHP的ORM实现DataTable

安装

$ composer require synergitech/fuel-datatables

基本用法

您需要调用setAllowedColumns()函数来定义哪些列可以被访问和返回。

class API extends Controller_Rest
{
  public function get_index()
  {
      $datatable = \SynergiTech\DataTables\DataTable::fromGet(\Model\YourModel::class);
      $datatable->setAllowedColumns([
         'id',
         'uuid',
         'name',
         'related_model.name',
         'created_at',
         'updated_at'
      ]);

      return $this->response($datatable->getResponse());
  }
}

自定义查询

如果您想执行更复杂的ORM查询,可以直接调用getQuery()函数,这将返回FuelPHP ORM的查询对象,您可以根据需要对其进行操作。

$datatable->getQuery()
   ->where('id', ">", 0)
   ->related("group")
   ->where("group.name", "!=", "guests")
   ->related("another_relation");

行格式化

您可以为要返回的每一行提供自定义回调,这将执行。您可以使用此功能来操作响应中的每一行。

$datatable->addRowFormatter(function ($model, $outputRow) {
    $outputRow['example'] = count($model->a_many_relation);
    return $outputRow;
});

XSS过滤

为了使您更容易管理输出过滤,您可以请求对所有或特定行进行编码输出。默认情况下,我们将XSS过滤留给您。

转义所有列

$datatable->setEscapedColumns();

有例外

$datatable->setEscapedColumns()
    ->setRawColumns(['html_body']);

转义某些列

$datatable->setEscapedColumns(['id', 'slug']);

关闭转义

$datatable->setEscapedColumns([]);

先决条件