ropendev/datatablesphp

PHP DataTables包装类,用于DataTables.js(Html和/或JavaScript生成,服务器端SQL)。

0.0.95 2015-01-07 11:53 UTC

This package is auto-updated.

Last update: 2024-08-29 04:12:31 UTC


README

Build Status Total Downloads

##目录

##描述

DataTablesPHP可以轻松生成带有服务器端或非服务器端的DataTable Html或JavaScript...

兼容DataTables最新版本(1.10.x)。

###特性

  • 生成html表格(复杂表头)
  • 生成相关JavaScript(数据可以设置在初始参数中或通过Ajax或服务器端加载)
  • 按列自定义搜索,进行复杂搜索以优化SQL查询
  • 分析服务器端请求并生成SQL查询
    • 可以处理复杂查询(连接)
    • 可以处理优化查询以搜索值(不仅限于Like %,您可以使用参数使用=、<、>、<=、>=、BETWEEN...)
  • 同时使用所有这些特性,可以轻松使用PHP处理dataTable

服务器端部分灵感来源于Allan Jardine的Class SSP。为了不信任用户输入,增加了连接可能性等等...

##安装

您可以克隆此git,下载类或使用Composer。

composer require ropendev/datatablesphp

##示例

请参阅examples文件夹。

##文档

DataTable::instance('id')
    ->setJsInitParam($key, $value)           // https://datatables.net.cn/reference/option/
    ->setJsInitParams($params)               // To set all params in one time
    ->setDom($dom)                           // Alias for setJsInitParameter('dom', $dom)
    ->setColumn($params, $show = true)       // Add a column and there options to the table:
                                             // - Initialization Javascript Options (see the doc : DataTables.net > Refererences > Column)
                                             //  - PHP Options (parent for complex header, sFilter, sql_table, sqlFilter... see l.169)
                                             // if($show) will be printed in the table else will only be load via ajax
    ->setColumns($columns)                   // Add columns
    ->setServerSide($ajax)                   // https://datatables.net.cn/reference/option/ajax
    ->setAjax($ajax)                         // Alias for setJsInitParameter('ajax', $ajax)
    ->setFilters($ajax)                      // Set permanent filters for sql queries (where)
    ->setData($data)                         // Permit to set the data in the DataTables Javascript Initialization.
    ->setHeader($bool)                       // To generate thead when you will call getHtml
    ->setFooter($bool)                       // To generate tfoot with th empty when you will call getHtml.
                                             // ... automatically called if you have set individual column filters


DataTable::instance('id')->getJavascript();  // Return javascript string. It is not embeding JS Files from DataTables.js... only it activation
                                            // and individual column filtering stuff

DataTable::instance('id')->getHtml([array('class'=>'my_table_class', 'data-nuclear'=>'bomb')]);        // Return html table in a string

/*** Server-Side Functions ***/
# You can't use server side options if you didn't set Columns

DataTable::instance('id')
    ->setFrom($table)                                                                                                 // Name of the table to query
    ->setJoin('table2', array('table'=>'column', 'table2'=>'column2') [, $join = 'LEFT JOIN', $duplicate = false])    // Table to join
    ->setPdoLink($pdoLink)                                                                                            // Add PHP PDO class link
    ->setCounterActive(false)  // Disabled counters (permits to gain in performanche, think to change your infoFiltered)

DataTable::instance('id')->exec($_REQUEST[, $csv = false]);  // Output the json results
                                                             //or export to csv format (use setInitFilter before if you use Individual column Filters)
DataTable::instance('id')->sendFatal($error);                // Output an error

对于列的php数组可以包含

  • 初始化JavaScript的属性(请参阅self::$columnParamshttps://datatables.net.cn/reference/option/
  • parent (=>$title) : 为了具有具有colspan的复杂表头... 将相同的$title设置在同一个th下多个列
  • sFilter (=>array) : 用于列过滤选项(请参阅self::$columFilteringParams
  • SQL参数(sql_namesql_table): 如果与数据或默认表不同,请使用setFrom设置
  • alias : SQL别名(不要求)
  • formatter (=>function($columnValue,$rowValues,$columnParams)) : 如果您想以特殊格式打印数据,设置一个函数。在服务器端请求中,如果您不想选择SQL列,只需不要设置sql_namedata属性(但设置一个formatter函数来打印一些内容!)。

##许可证

MIT(请参阅LICENSE文件以获取详细信息)

##待办事项 将很快到来

  • 自动生成选择选项,如果有数据(l.469)
  • 具有导出功能的示例