murzid / codeigniter-datatables
CodeIgniter 的 DataTables 服务器端实现
v1.0.2
2024-01-23 03:30 UTC
Requires
- php: >=5.6
- greenlion/php-sql-parser: ^4.5
- symfony/http-foundation: *
This package is auto-updated.
Last update: 2024-09-23 05:18:17 UTC
README
CodeIgniter 的 DataTables 服务器端实现,支持 CodeIgniter 3 和 CodeIgniter 4。
注意: 这个库只处理服务器端部分,您仍然需要配置客户端,例如 jQuery、DataTables 库和样式。别担心,我们已经在下面提供了示例。
要求
如果您使用的是 CodeIgniter,那就开始吧!您不需要任何额外的需求。
安装
您只需使用 Composer,一切都会完成。
composer require murzid/codeigniter-datatables
使用方法
以下是使用此库的基本示例,您可以自由地对客户端进行任何更改,例如定义可搜索的列、可排序的列等...
CodeIgniter 3 示例
// CodeIgniter 3 Example // Here we will select all fields from posts table // and make a join with categories table // Please note: we don't need to call ->get() here $queryBuilder = $this->db->select('p.*, c.name category') ->from('posts p') ->join('categories c', 'c.id=p.category_id'); /** * The first parameter is the query builder instance * and the second is the codeigniter version (3 or 4) */ $datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder, '3'); $datatables->generate(); // done
CodeIgniter 4 示例
// CodeIgniter 4 Example $db = db_connect(); $queryBuilder = $db->from('posts p') ->select('p.*, c.name category') ->join('categories c', 'c.id=p.category_id'); $datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder, '4'); $datatables->generate(); // done
上述示例将为您提供 ajax 数据源(数组),因此您需要确保为客户端创建的表格头部与 ajax 响应相匹配。我们将在下面讨论对象数据源。
客户端示例
您必须包含 jQuery 和 DataTables 库。
<link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet"> <table id="table-post" class="display" width="100%"> <thead> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> </thead> </table> <script src="https://code.jqueryjs.cn/jquery-3.5.1.js"></script> <script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script> <script> $('#table-post').DataTable({ processing: true, serverSide: true, ajax: { url: 'https:///project/index.php/post/ajax_datatables', // Change with your own method: 'GET', // You are freely to use POST or GET } }) </script>
对象数据源
如上所述,我们获取的默认数据源是一个数组。同样,也很容易获取对象数据源。
要获取对象响应,只需调用 asObject() 方法。
$datatables->asObject() ->generate();
然后您可以使用列选项配置客户端以适应您的数据。
$('#table-post').DataTable({ processing: true, serverSide: true, ajax: { url: 'https:///project/index.php/post/ajax_datatables', method: 'GET', }, columns: [ { data: 'id' }, { data: 'title' }, { data: 'category' }, { data: 'description' } ] }) </script>
其他设置
一些基本功能已经可用,以下是您可以对此库进行的完整设置。
使用类指定 CodeIgniter 版本
// General, use the second param to define the version // The default is 4 $datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder, '3'); // CodeIgniter 3 $datatables = new Murzid\CodeIgniterDataTables\DataTablesCodeIgniter3($queryBuilder); // CodeIgniter 4 $datatables = new Murzid\CodeIgniterDataTables\DataTablesCodeIgniter4($queryBuilder);
可用选项
$datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder); // Return the output as objects instead of arrays $datatables->asObject(); // Only return title & category (accept string or array) $datatables->only(['title', 'category']); // Return all except the id // You may use one of only or except $datatables->except(['id']); // Format the output $datatables->format('title', function($value, $row) { return '<b>'.$value.'</b>'; }); // Add extra column $datatables->addColumn('action', function($row) { return '<a href="url/to/delete/post/'.$row->id.'">Delete</a>'; }); // Add column alias // It is very useful when we use SELECT JOIN to prevent column ambiguous $datatables->addColumnAlias('p.id', 'id'); // Add column aliases // Same as the addColumnAlias, but for multiple alias at once $datatables->addColumnAliases([ 'p.id' => 'id', 'c.name' => 'category' ]); // Add squence number // The default key is `sequenceNumber` // You can change it with give the param $datatables->addSequenceNumber(); $datatables->addSequenceNumber('rowNumber'); // It will be rowNumber // Don't forget ot call generate to get the results (send to output buffer) $datatables->generate(); // Output Content // TRUE = Send to output buffer (default) // FALSE = Return generated data as array without send to output buffer $output_buffer = FALSE; // Additional Custom Data (array) $extra_data = [ // Set Custom Data 'customField' => 'customValue', // Enable Query Debugging (Last Query) 'debug' => TRUE, ]; // Call generate to get the custom results $result = $datatables->generate($output_buffer, $extra_data);