DataTables 服务器端处理 - 如何添加不是来自数据库的计算列值?
DataTables Server-side Processing - How to add calculated column values not from database?
我正在关注示例
https://datatables.net/examples/data_sources/server_side
在示例代码中,列数据 return 来自数据库,如下面的代码
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 )
);
我需要添加第 4 列评分,该值不是来自数据库,而是在 php 函数中计算得出的。我该怎么做?
array( 'db' => 'rating', 'dt' => 3 ) // 需要从 php 函数
获取值
下面是服务器端php例子中的代码,
$table = 'datatables_demo';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 ),
array( 'db' => 'office', 'dt' => 3 ),
array(
'db' => 'start_date',
'dt' => 4,
'formatter' => function( $d, $row ) {
return date( 'jS M y', strtotime($d));
}
),
array(
'db' => 'salary',
'dt' => 5,
'formatter' => function( $d, $row ) {
return '$'.number_format($d);
}
)
);
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
你可以试试
$columns[] = [ 'db' => 'rating', 'dt' => 3];
<?php
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 )
);
$columns[] = [ 'db' => 'rating', 'dt' => 3 ];
print_r($columns);
在
找到答案
https://datatables.net/forums/discussion/49799/datatable-ssp-class-php
需要更改 ssp.class.php
中的函数 data_output
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
$params = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
// Do we need to compute?
elseif (isset( $column['compute'] ) ) {
$params[ $column['dt'] ]['compute'] = $column['compute'];
$params[ $column['dt'] ]['params'][] = $data[$i][ $columns[$j]['db'] ];
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
if ( !empty( $params ) ) {
foreach ( $params as $dt => $parameters) {
$parameterStr = "";
foreach ( $parameters['params'] as $p) {
$parameterStr .= "$p,";
}
$parameterStr = rtrim($parameterStr,",");
$row[ $dt ] = self::$parameters['compute']($parameterStr);
}
}
ksort($row);
$out[] = $row;
}
return $out;
}
static function shippingMethod($parameterString) {
$output = "";
$parameters = explode(",",$parameterString);
foreach($parameters as $p)
$output .= "(".$p .")";
return $output;
}
static function konka($parameterString) {
$output = "";
$parameters = explode(",",$parameterString);
foreach($parameters as $p)
$output .= "***".$p ."***";
return $output;
}
我正在关注示例 https://datatables.net/examples/data_sources/server_side
在示例代码中,列数据 return 来自数据库,如下面的代码
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 )
);
我需要添加第 4 列评分,该值不是来自数据库,而是在 php 函数中计算得出的。我该怎么做?
array( 'db' => 'rating', 'dt' => 3 ) // 需要从 php 函数
获取值下面是服务器端php例子中的代码,
$table = 'datatables_demo';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 ),
array( 'db' => 'office', 'dt' => 3 ),
array(
'db' => 'start_date',
'dt' => 4,
'formatter' => function( $d, $row ) {
return date( 'jS M y', strtotime($d));
}
),
array(
'db' => 'salary',
'dt' => 5,
'formatter' => function( $d, $row ) {
return '$'.number_format($d);
}
)
);
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
你可以试试
$columns[] = [ 'db' => 'rating', 'dt' => 3];
<?php
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 )
);
$columns[] = [ 'db' => 'rating', 'dt' => 3 ];
print_r($columns);
在
找到答案https://datatables.net/forums/discussion/49799/datatable-ssp-class-php
需要更改 ssp.class.php
中的函数 data_outputstatic function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
$params = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
// Do we need to compute?
elseif (isset( $column['compute'] ) ) {
$params[ $column['dt'] ]['compute'] = $column['compute'];
$params[ $column['dt'] ]['params'][] = $data[$i][ $columns[$j]['db'] ];
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
if ( !empty( $params ) ) {
foreach ( $params as $dt => $parameters) {
$parameterStr = "";
foreach ( $parameters['params'] as $p) {
$parameterStr .= "$p,";
}
$parameterStr = rtrim($parameterStr,",");
$row[ $dt ] = self::$parameters['compute']($parameterStr);
}
}
ksort($row);
$out[] = $row;
}
return $out;
}
static function shippingMethod($parameterString) {
$output = "";
$parameters = explode(",",$parameterString);
foreach($parameters as $p)
$output .= "(".$p .")";
return $output;
}
static function konka($parameterString) {
$output = "";
$parameters = explode(",",$parameterString);
foreach($parameters as $p)
$output .= "***".$p ."***";
return $output;
}