glazilla / datatable
生成JSON输出的类,可用于从mysql/pdo pgsql/pdo生成,适用于JavaScript库DataTable
1.1.7.1
2024-02-08 15:29 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: 6.*
README
完整的JSON接口,允许通过DataTable UI查询数据库(支持mySql、MariaDB或PostgreSQL)。成功测试与Symfony 6+、SlimFramework、DataTable 1.13.1兼容。可以过滤所有或特定行的搜索。过滤器可以组合(或)值,使用管道符号(|)分隔,例如:"john|lisa"。有关DataTable的使用说明,请参阅https://datatables.net.cn/
安装
composer require glazilla/datatable:dev-master
使用
$myDataTable = new \Glazilla\DataTable\DataTable( \Pdo $pdoInstance);
与MariaDB/MySQL一起使用
echo $myDataTable->query( [ Array $columns, String $columnKey, String $tableOrview, String $where ]); // Returns JSON String as expected by datatables
与PostgreSQL一起使用
echo $myDataTable->PGquery( [ Array $columns, String $columnKey, String $tableOrview, String $where ]); // Returns JSON String as expected by datatables
如果您想使用UNACCENT PostgreSQL扩展进行搜索,请激活扩展并在代码中插入此常量
define("UNACCENT_QUERY_PG", "1");
MariaDB的完整示例
public function DataJSON(RequestInterface $request, ResponseInterface $response)
{
$columns = ["category", "title", "id", "when"] ;
$contenu = $this->datatable->Query($colonnes, "id", "view_articles", "archive=1");
$response = $response->withHeader('Content-Type', 'application/json')->withStatus(200);
$response = $response->write($contenu);
return $response;
}
在DataTable/jquery中使用
sAjaxSource: '/userdata', // route to function DataJSON
fnServerData:
(sSource, aoData, fnCallback) => {
$.getJSON(sSource, aoData, (json) => {
fnCallback(json);
})
.fail(() => {
myAlert({
text: 'An error happened !',
icon: 'alert',
button: 'Sorry...',
});
});
},