acfbentveld/laravel-datatables

此包已被弃用,不再维护。作者建议使用singlequote/laravel-datatables包。

支持分页和关系递归搜索的Datatables

2.0.28 2019-06-03 08:51 UTC

README

Latest Version on Packagist Total Downloads

ACFBentveld到SingleQuote

acfbentveld组已迁移到singlequote。

此仓库包含一个可以渲染可过滤和可排序表格的Datatable。它旨在非常轻量级且易于使用。它支持异步检索数据、分页和关系递归搜索。

版本3已发布

此包的版本3已发布。查看新版本DataTables v3 为什么会有版本3?让我为您解释一下。在新版本中,您不必编写自己的脚本或HTML表格。新版本使用TableModels,比之前版本快得多。

重要

如果您正在使用版本>2.0.20且<2.0.24,请尽快将包更新到版本2.0.24。这是因为使用AVG内容时的安全风险!

安装

该包已在laravel 5.8.*上进行了测试

您可以通过composer安装此包

composer require acfbentveld/laravel-datatables

新增内容

自2.0.11以来,添加了新方法。对于大量结果,搜索方法非常慢。新的searchable方法解决了这个问题。通过定义您想要搜索的键,datatables不必自己创建可搜索键,这使得它变得更快。

    \DataTables::model(new User)
        ->searchable('name', 'description', 'roles.name'); //use the relation name and key
        ->get();

示例

以下是一个简单的示例,显示用户模型内部的用户名。首先,我们从PHP控制器开始

namespace App\Http\Controllers\Users;
use App\Http\Controllers\Controller;
use App\User;

class UsersController extends Controller
{
    public function index()
    {
        //when the datatables makes a request to the same route/method the package will catch this.
        \DataTables::model(new User)->get();
        return view("users.index");
    }
}

然后是带有表格的HTML

<table id="datatable" class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>name</th>
        </tr>
    </thead>
</table>

然后是JavaScript

 $(document).ready(function() {
    //thats all
    $('#datatable').DataTable({
        "processing": true, //process it
        "serverSide": true, //make it server side
        "ajax": location.href, //this will call the the index function in the user controller
        "columns": [ //define the keys
            { "data": "id" },
            { "data": "name" },
        ],
    });
} );

用法

PHP

此包支持构建JSON数据的两种方法,第一种(不推荐!)是collect方法。对于此方法,您将检索到的数据作为集合传递给该方法。这对于少量记录效果很好。

DataTables::collect(User::all())->get();

第二种方法(推荐)是model方法。这对于所有事情都非常出色。datatables类创建一个新的模型实例,对其运行查询,然后完成。它还具有许多其他选项,并且只对数据库发出一个请求。这对于大量和少量记录都很快。只需使用此方法即可!

DataTables::model(new User)->get();

JavaScript | jQuery

您不需要指定不同的URL。该包将检测datatables是否建立连接

 $(document).ready(function() {
    //thats all
    $('#datatable').DataTable({
        "processing": true, //process it
        "serverSide": true, //make it server side
        "ajax": location.href //Just get the data from the same url. The package will handle it all
    });

} );

HTML

最后,创建一个HTML表格。无需解释如何操作。

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/dt-1.10.16/datatables.min.css"/>

<table id="datatable" class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>name</th>
        </tr>
    </thead>
</table>

<script type="text/javascript" src="https://cdn.datatables.net/v/bs/dt-1.10.16/datatables.min.js"></script>

选项

单页多表格

在网页上使用多个表格时,您将需要多个路由来调用datatables包。或者您可以使用table方法。

    \DataTables::model(new User)->table('users')->get(); //will be initialized when the table parameter is users
    
    \DataTables::model(new User)->table('roles')->get(); //will be initialized when the table parameter is roles

在您的javascript中可以这样调用

$('#userstable').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": location.href + '?table=users' //add the table parameter to make it unique
});

可搜索的

创建搜索键并查找结果需要一些时间。如果您在执行之前定义搜索键,datatables将渲染得更快

    \DataTables::model(new User)
        ->searchable('name', 'description', 'roles.name'); //use the relation name and key
        ->get();
where

仅仅是常规的where方法。用它来过滤模型

 DataTables::model(new User)->where('name', 'John Snow')->where('email', 'knows@nothing.com')->get();
whereHas

仅仅是常规的whereHas方法。用它来过滤模型

 DataTables::model(new User)->whereHas('roles')->get();
 DataTables::model(new User)->whereHas('roles', function($query){
   $query->whereName('admin');
 })->get();
orWhereHas

仅仅是常规的orWhereHas方法。用它来过滤模型

 DataTables::model(new User)->whereHas('roles')->orWhereHas('permissions')->get();
whereYear

仅仅是常规的whereYear方法。用它来过滤模型

 DataTables::model(new User)->whereYear('created_at', '2018')->get();
with

仅仅是常规的with方法。用它选择与之相关的关联

 DataTables::model(new User)->with('roles', 'permissions')->get();
encrypt

有时您可能想加密一个特定的值。例如模型的ID。

DataTables::model(new User)->encrypt('id')->get(); // will return all items with an encrypted value
exclude

exclude方法从响应数据中排除键

 DataTables::model(new User)->exclude('id', 'email')->get(); //removes the id column from the collection
作用域

当尝试从模型访问作用域时,您可以使用addScope方法将作用域添加到您的集合中。

    DataTables::model(new User)->addScope('active')->get(); //Access the scopeActive on the users model

向作用域中添加数据

    DataTables::model(new User)->addScope('formatDate', 'd-m-Y')->get(); //Access the scopeFormatDate with data
with trashed

要从数据库中检索软删除项,您可以使用默认的withTrashed方法。这将仅在包含软删除特质的模型上起作用。

    DataTables::model(new User)->withTrashed()->get(); //Retrieve the soft deleted items.
datatable选项
 $(document).ready(function() {
    //thats all
    $('#datatable').DataTable({
        "processing": true, //process it
        "serverSide": true, //make it server side
        "ajax": location.href, //Just get the data from the same url. The package will handle it all
        "columns": [ //define the keys
                { "data": "id" },
                { "data": "name" },
            ],
        //if you want to use relations or chage the behavior of a cell
        "columnDefs": [
                {
                    "render": function ( data, type, row ) {
                        //for relations just return the relation key
                        return data.name;
                    },
                    "targets": [0] //the targets, starts at 0
                },
            ],
    });

} );

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件wim@acfbentveld.nl联系,而不是使用问题跟踪器。

明信片软件

您可以使用这个包,但如果它进入了您的生产环境,我们非常感激您从家乡给我们寄一张明信片,并说明您正在使用我们的哪个包。

我们的地址是:ACF Bentveld,Ecu 2 8305 BA,Emmeloord,荷兰。

致谢

许可

麻省理工学院许可证(MIT)。有关更多信息,请参阅许可文件