提问人:Eddi 提问时间:12/19/2021 最后编辑:Eddi 更新时间:12/20/2021 访问量:985
DataTables服务器端处理 - 如何添加不是来自数据库的计算列值?
DataTables Server-side Processing - How to add calculated column values not from database?
问:
我在 https://datatables.net/examples/data_sources/server_side 上以身作则
在示例代码中,列数据从数据库返回,如下代码所示
$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 )
);
答:
0赞
zimorok
12/19/2021
#1
你可以试试$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);
评论
0赞
Eddi
12/19/2021
嗨,这仅添加到列定义表示从数据库字段获取数据。我需要一个定义或一种方法来让这个特定的列从 php 函数获取数据。
0赞
zimorok
12/19/2021
是一样的,你只需计算所需的数据,并将计算出的数据附加到原始数组中,作为我提供的代码
0赞
Eddi
12/20/2021
#2
找到答案
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;
}
评论