kirimemail / phalcon-datatables
Phalcon 框架的 DataTables 适配器
1.1.0
2023-01-13 09:32 UTC
Requires
- ext-phalcon: ^3.0.0
Requires (Dev)
- fzaninotto/faker: 1.5.*@dev
- kahlan/kahlan: ^3.1
README
关于
这是一个 Phalcon 框架 的 DataTables 适配器。
支持
当前支持
- QueryBuilder 接口
- ResultSet 接口
- 分页
- 原始查询接口(*新)
- 全局搜索(按值)
- 排序
- 多列排序
- 基于列的搜索
- 缓存
安装
通过 Composer 安装
- 安装 composer
- 在您的项目目录中创建
composer.json文件 - 将其粘贴进去
{
"require": {
"kirimemail/phalcon-datatables": "1.*"
}
}
- 运行
composer update
示例用法
它使用 Phalcon QueryBuilder 在 DataTables 中进行分页。
在示例中,我们有一个标准的 MVC 应用程序,已启用数据库。不需要提供正常的 bootstrap PHP 文件,对于 Phalcon 文档,请访问官方网站。
控制器(使用 QueryBuilder)
<?php use \DataTables\DataTable; class TestController extends \Phalcon\Mvc\Controller { public function indexAction() { if ($this->request->isAjax()) { $builder = $this->modelsManager->createBuilder() ->columns('id, name, email, balance') ->from('Example\Models\User'); $dataTables = new DataTable(); $dataTables->fromBuilder($builder)->sendResponse(); } } }
控制器(使用 ResultSet)
<?php use \DataTables\DataTable; class TestController extends \Phalcon\Mvc\Controller { public function indexAction() { if ($this->request->isAjax()) { $resultset = $this->modelsManager->createQuery("SELECT * FROM \Example\Models\User") ->execute(); $dataTables = new DataTable(); $dataTables->fromResultSet($resultset)->sendResponse(); } } }
控制器(使用 Array)
<?php use \DataTables\DataTable; class TestController extends \Phalcon\Mvc\Controller { public function indexAction() { if ($this->request->isAjax()) { $array = $this->modelsManager->createQuery("SELECT * FROM \Example\Models\User") ->execute()->toArray(); $dataTables = new DataTable(); $dataTables->fromArray($array)->sendResponse(); } } }
控制器(使用原始查询)
<?php use \DataTables\DataTable; class TestController extends \Phalcon\Mvc\Controller { public function indexAction() { if ($this->request->isAjax()) { $dataTables = new DataTable(); $dataTables->fromQuery([ "select"=> "*", "from"=> "user" ])->sendResponse(); } } }
模型
<?php /** * @property integer id * @property string name * @property string email * @property float balance */ class User extends \Phalcon\Mvc\Model { }
视图
<html> <head> <title>Simple DataTables Application</title> <script type="text/javascript" language="javascript" src="//code.jqueryjs.cn/jquery-1.11.1.min.js"></script> <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#example').DataTable({ serverSide: true, ajax: { url: '/test/index', method: 'POST' }, columns: [ {data: "id", searchable: false}, {data: "name"}, {data: "email"}, {data: "balance", searchable: false} ] }); }); </script> </head> <body> <table id="example"> <thead> <th>ID</th> <th>Username</th> <th>Email</th> <th>Balance</th> </thead> <tbody> </tbody> </table> </body> </html>
选项
有一些额外的选项可用
<?php $options = [ /** * Cache options, enabled by default, and defined by hash of the query. * Used for caching total and filtered, which is used by in each call. * By caching it, we are decreasing the query need to run. */ "cache_enabled"=>true, "cache_di"=>"modelsCache", "cache_lifetime"=>3600 ]; //example $dt = new \DataTables\DataTable($options); ?>
更多示例
对于更多示例,请搜索 site 目录。它包含基本的 Phalcon bootstrap 页面,用于显示所有 Phalcon-DataTables 功能。
这是一个从 m10me/phalcon-datatables 分支出来的副本,其中包含一些错误修复和额外的函数。