datatables
2.3.16
使用我们闪电般快速的 PHP 库轻松简化您的datatables服务器端处理,无缝简化您的工作流程。现场演示
注意: 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 ();
如果您使用的是codeigniter或laravel等php框架,则可以使用相关的数据库适配器。
// Codeigniter 4 Example
<?php
namespace App Controllers ;
use Config Database ;
use Ozdemir datatables datatables ;
use Ozdemir datatables DB Codeigniter4Adapter ;
class Home extends BaseController
{
public function index ()
{
return view ( ' index ' );
}
public function ajax ()
{
// CI 4 builder class
$ db = Database:: connect ();
$ builder = $ db -> table ( ' Track ' );
$ builder -> select ( ' TrackId, Name, UnitPrice ' );
// datatables Php Library
$ datatables = new datatables ( new Codeigniter4Adapter );
// using CI4 Builder
$ datatables -> query ( $ builder );
// alternatively plain sql
// $ datatables ->query('Select TrackId, Name, UnitPrice from Track');
return $ this -> response -> setJSON ( $ datatables -> generate ()-> toJson ());
}
}
// Laravel Example
<?php
// routes/web.php
use Ozdemir datatables datatables ;
use Ozdemir datatables DB LaravelAdapter ;
Route:: get ( ' /ajax/laravel ' , function () {
$ dt = new datatables ( new LaravelAdapter );
$ dt -> query (
Track:: query ()
-> select ([
' TrackId ' ,
' Track.Name ' ,
' Title as Album ' ,
' MediaType.Name as MediaType ' ,
' UnitPrice ' ,
' Milliseconds ' ,
' Bytes ' ,
])
-> join ( ' Album ' , ' Album.AlbumId ' , ' Track.AlbumId ' )
-> join ( ' MediaType ' , ' MediaType.MediaTypeId ' , ' Track.MediaTypeId ' )
); // same as the previous example, sql statement can be used.
return $ dt -> generate ();
});
这是可用公共方法的列表。
需要查询($query)
需要生成()
toJson()
toArray()
添加($列,函数($行){})
编辑($列,函数($行){})
过滤器($列, 函数(){})
隐藏($列)
setDistinctResponseFrom($列)
setDistinctResponse($输出)
获取列()
获取查询()
<?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 ){
// masks email : [email protected] => m***@mail.com
return preg_replace ( ' /(?<=.).(?=.*@)/u ' , ' * ' , $ data [ ' email ' ]);
});
$ dt -> edit ( ' address ' , function ( $ data ){
// checks user access.
$ 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 ' ); // hides 'plevel' column from the output
$ dt -> add ( ' action ' , function ( $ data ){
// returns 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()';
作曲家
datatables > 1.10
PHP > 7.1.3
版权所有 (c) 2015 Yusuf ÖZDEMIIR,根据 MIT 许可发布