wisnubaldas/datatables

PHP库,以快速简单的方式处理Datatables的服务端处理。

2.2.0 2019-10-05 16:58 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:25 UTC


README

Latest Stable Version Build Status license

PHP库,以快速简单的方式处理Datatables的服务端处理。 实时演示

功能

  • 易于使用。仅使用几行代码即可生成json。
  • 支持使用闭包函数编辑列。
  • 支持自定义过滤器。
  • 可以处理大多数复杂的查询。
  • 支持mysql和sqlite原生PHP。
  • 兼容

安装

注意:版本2.0+需要php 7.1.3+ (php支持的版本)

推荐使用Composer安装库。

如果您还没有开始使用Composer,我强烈建议您使用它。

在项目的根目录下创建一个名为composer.json的文件,包含以下信息

{
    "require": {
       "ozdemir/datatables": "2.*"
    }
}

然后运行

composer install

或者直接运行

composer require ozdemir/datatables

将自动加载器添加到您的项目中

    <?php

    require_once 'vendor/autoload.php';

现在您可以使用Datatables PHP库了。

    <?php
    require_once 'vendor/autoload.php';

    use Ozdemir\Datatables\Datatables;
    use Ozdemir\Datatables\DB\MySQL;

    $config = [ 'host'     => 'localhost',
                'port'     => '3306',
                'username' => 'homestead',
                'password' => 'secret',
                'database' => 'sakila' ];

    $dt = new Datatables( new MySQL($config) );

    $dt->query('Select film_id, title, description from film');

    echo $dt->generate();

方法

以下是可用的公共方法列表。

query($query) 必需

  • 设置SQL查询

generate() 必需

  • 运行查询并构建输出
  • 以json格式返回输出
  • 等同于 generate()->toJson()

toJson()

  • 以json格式返回输出
  • 应在 generate() 之后调用

toArray()

  • 以数组格式返回输出
  • 应在 generate() 之后调用

add($column, function( $row ){})

  • 添加额外的列以供自定义使用

edit($column, function($row){})

  • 允许列编辑

filter($column, function(){})

  • 允许自定义过滤
  • 具有以下方法
    • escape($value)
    • searchValue()
    • defaultFilter()
    • between($low, $high)
    • whereIn($array)
    • greaterThan($value)
    • lessThan($value)

hide($columns)

  • 从输出中删除列
  • 当您只需要在add()或edit()方法中使用数据时很有用。

setDistinctResponseFrom($column)

  • 使用给定的列名执行查询,并将返回的数据添加到输出中,键为distinctData。

setDistinctResponse($output)

  • 将给定的数据添加到输出中,键为distinctData。

getColumns()

  • 返回列名(用于开发目的)

getQuery()

  • 返回库创建的SQL查询字符串(用于开发目的)

示例

    <?php
    require_once 'vendor/autoload.php';

    use Ozdemir\Datatables\Datatables;
    use Ozdemir\Datatables\DB\SQLite;

    $path = __DIR__ . '/../path/to/database.db';
    $dt = new Datatables( new SQLite($path) );

    $dt->query('Select id, name, email, age, address, plevel from users');

    $dt->edit('id', function($data){
        // return a link.
        return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
    });

    $dt->edit('email', function($data){
        // mask email : mail@mail.com => m***@mail.com
        return preg_replace('/(?<=.).(?=.*@)/u','*', $data['email']);
    });

    $dt->edit('address', function($data){
        // check if a user has authorized to see the column value.
        $current_user_plevel = 4;
        if ($current_user_plevel > 2 && $current_user_plevel > $data['plevel']) {
            return $data['address'];
        }

        return 'you are not authorized to view this column';
    });
    
    $dt->hide('plevel'); // hide 'plevel' column from the output

    $dt->add('action', function($data){
        // return a link in a new column
        return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
    });

    $dt->filter('age', function (){
        // applies custom filtering.
        return $this->between(15, 30);
    });

    echo $dt->generate()->toJson(); // same as 'echo $dt->generate()';

要求

Composer
DataTables > 1.10
PHP > 7.1.3

许可

版权(c)2015 Yusuf ÖZDEMİR,在MIT许可下发布