xsoft/datatables

数据表格插件

2.1.2 2019-10-10 08:07 UTC

This package is auto-updated.

Last update: 2024-09-10 18:10:21 UTC


README

用于轻松创建数据表格的包

安装

php artisan vendor:publish --tag=datatables-public

基本用法

在控制器中使用

Xsoft\DataTables\DataTable as DataTable

创建您表格的 create 方法

private function getTable()
{
 $table = new DataTable('tableOrModelName','routeToIndexAjax');
 return $table;
}

并使用它

public function index()
{
    return response()->view('data.index',['dataTable' => $this->getTable()]);
}

public function indexAjax(Request $request)
{
    $output = $this->getTable()->output($request);
    return response()->json($output);
}

最后,在视图中使用

@dataTable($dataTable)

如果需要包含 datatables JS 和 CSS 文件,请使用视图中 'datatables-js''datatables-css' 部分。

警告!如果您不想使用上述部分,则需要将您的表格的 noSection 参数设置为 true

示例

private function getTable()
{
 $table = new DataTable('tableOrModelName','routeToIndexAjax');
 $table->noSection = true;
 return $table;
}

高级用法

__construct()

`$table = new DataTable('tableOrModelName','routeName','columns','options','params')

构造函数接受四个参数

  • 模型(模型或表名)
  • 路由(返回表格输出的路由名)
  • 列(可以为空,格式为 ['columnName','columnDatabaseField','columnsOptions'] 的列数组)
  • 选项(可以为空,格式为 ['option1' => '1', 'option2' => '2'] 的数组)
  • params(格式为 ['paramName' => ['value','paramType']] 的路由或ajax参数,paramType 可设置为 'ajax'(默认)或 'route')

所有这些表格参数都可以通过setter函数 setModel(), setRoute(), setColumns()setOptions()addParams() 设置

join(...string|array)

字符串示例

$table->join('products')

在这个示例中,products 表将通过 OUR_TABLE.product_idproducts.id 与我们的表连接

数组示例

$table->join(['products','OUR_TABLE.prod_id','products.uid'])

在这个示例中,products 表将通过 OUR_TABLE.prod_idproducts.uid 与我们的表连接

searchBy(..string)

传递您想要搜索的列名

orderBy(array)

传递包含两个元素的数组:列名和排序方向 ascdesc

setQuery()

设置输出数据

示例

$query = User::where('active',1)->get();
$table->setQuery($query);