项目作者: n1crack

项目描述 :
PHP Library to handle server-side processing for Datatables, in a fast and simple way.
高级语言: PHP
项目地址: git://github.com/n1crack/datatables.git
创建时间: 2015-01-17T21:09:10Z
项目社区:https://github.com/n1crack/datatables

开源协议:MIT License

下载


Datatables library for PHP

Latest Stable Version PHP Composer license

Simplify your Datatables server-side processing effortlessly using our lightning-fast PHP library, streamlining your workflow seamlessly. Live Demo

Features

Installation

NOTE: version 2.0+ requires php 7.1.3+ (php supported versions)

The recommended way to install the library is with Composer

If you haven’t started using composer, I highly recommend you to use it.

Put a file named composer.json at the root of your project, containing this information:

  1. {
  2. "require": {
  3. "ozdemir/datatables": "2.*"
  4. }
  5. }

And then run:

  1. composer install

Or just run :

  1. composer require ozdemir/datatables

Add the autoloader to your project:

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

You’re now ready to begin using the Datatables php library.

  1. <?php
  2. require_once 'vendor/autoload.php';
  3. use Ozdemir\Datatables\Datatables;
  4. use Ozdemir\Datatables\DB\MySQL;
  5. $config = [ 'host' => 'localhost',
  6. 'port' => '3306',
  7. 'username' => 'homestead',
  8. 'password' => 'secret',
  9. 'database' => 'sakila' ];
  10. $dt = new Datatables( new MySQL($config) );
  11. $dt->query('Select film_id, title, description from film');
  12. echo $dt->generate();

If you are using a php framework such as codeigniter or laravel, you can use the relevant database adapter.

  1. // Codeigniter 4 Example
  2. <?php
  3. namespace App\Controllers;
  4. use Config\Database;
  5. use Ozdemir\Datatables\Datatables;
  6. use Ozdemir\Datatables\DB\Codeigniter4Adapter;
  7. class Home extends BaseController
  8. {
  9. public function index()
  10. {
  11. return view('index');
  12. }
  13. public function ajax()
  14. {
  15. // CI 4 builder class
  16. $db = Database::connect();
  17. $builder = $db->table('Track');
  18. $builder->select('TrackId, Name, UnitPrice');
  19. // Datatables Php Library
  20. $datatables = new Datatables(new Codeigniter4Adapter);
  21. // using CI4 Builder
  22. $datatables->query($builder);
  23. // alternatively plain sql
  24. // $datatables->query('Select TrackId, Name, UnitPrice from Track');
  25. return $this->response->setJSON($datatables->generate()->toJson());
  26. }
  27. }
  1. // Laravel Example
  2. <?php
  3. // routes/web.php
  4. use Ozdemir\Datatables\Datatables;
  5. use Ozdemir\Datatables\DB\LaravelAdapter;
  6. Route::get('/ajax/laravel', function () {
  7. $dt = new Datatables(new LaravelAdapter);
  8. $dt->query(
  9. Track::query()
  10. ->select([
  11. 'TrackId',
  12. 'Track.Name',
  13. 'Title as Album',
  14. 'MediaType.Name as MediaType',
  15. 'UnitPrice',
  16. 'Milliseconds',
  17. 'Bytes',
  18. ])
  19. ->join('Album', 'Album.AlbumId', 'Track.AlbumId')
  20. ->join('MediaType', 'MediaType.MediaTypeId', 'Track.MediaTypeId')
  21. ); // same as the previous example, sql statement can be used.
  22. return $dt->generate();
  23. });

Methods

This is the list of available public methods.

query($query) required

  • sets the sql query

generate() required

  • runs the queries and build outputs
  • returns the output as json
  • same as generate()->toJson()

toJson()

  • returns the output as json
  • should be called after generate()

toArray()

  • returns the output as array
  • should be called after generate()

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

  • adds extra columns for custom usage

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

  • allows column editing

filter($column, function(){})

  • allows custom filtering
  • it has the methods below
    • escape($value)
    • searchValue()
    • defaultFilter()
    • between($low, $high)
    • whereIn($array)
    • greaterThan($value)
    • lessThan($value)

hide($columns)

  • removes the column from output
  • It is useful when you only need to use the data in add() or edit() methods.

setDistinctResponseFrom($column)

  • executes the query with the given column name and adds the returned data to the output with the distinctData key.

setDistinctResponse($output)

  • adds the given data to the output with the distinctData key.

getColumns()

  • returns column names (for dev purpose)

getQuery()

  • returns the sql query string that is created by the library (for dev purpose)

Example

  1. <?php
  2. require_once 'vendor/autoload.php';
  3. use Ozdemir\Datatables\Datatables;
  4. use Ozdemir\Datatables\DB\SQLite;
  5. $path = __DIR__ . '/../path/to/database.db';
  6. $dt = new Datatables( new SQLite($path) );
  7. $dt->query('Select id, name, email, age, address, plevel from users');
  8. $dt->edit('id', function($data){
  9. // return a link.
  10. return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
  11. });
  12. $dt->edit('email', function($data){
  13. // masks email : mail@mail.com => m***@mail.com
  14. return preg_replace('/(?<=.).(?=.*@)/u','*', $data['email']);
  15. });
  16. $dt->edit('address', function($data){
  17. // checks user access.
  18. $current_user_plevel = 4;
  19. if ($current_user_plevel > 2 && $current_user_plevel > $data['plevel']) {
  20. return $data['address'];
  21. }
  22. return 'you are not authorized to view this column';
  23. });
  24. $dt->hide('plevel'); // hides 'plevel' column from the output
  25. $dt->add('action', function($data){
  26. // returns a link in a new column
  27. return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
  28. });
  29. $dt->filter('age', function (){
  30. // applies custom filtering.
  31. return $this->between(15, 30);
  32. });
  33. echo $dt->generate()->toJson(); // same as 'echo $dt->generate()';

Road Map

  • better test suites for each class
  • improve integrations for php frameworks

Requirements

Composer
DataTables > 1.10
PHP > 7.1.3

License

Copyright (c) 2015 Yusuf ÖZDEMİR, released under the MIT license

If you like the library please consider giving a star.