繁体   English   中英

无法使用 php 格式化来自 mysql 数据库的 DataTables 数据

[英]Not able to format DataTables data that is coming from a mysql database using php

我正在使用 dataTables 插件来处理一些数据。 我正在使用 mysql 数据库和 php。 我能够在数据库中显示数据,但我无法对其进行格式化。 我有一些字段是美元金额,我想添加美元符号和千位逗号。 谁能告诉我该怎么做? dataTables 中的列属性对我不起作用,因为这些列是在 php 文件中定义的。 下面的代码有效,但我无法格式化我的货币列。

HTML:

<table id="example" class="display desktop" style="">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Title</th>
                <th>Company</th>
                <th>2019 Compensation</th>
                <th>Median Employee Pay</th>
                <th>Type of Position</th>
                <th>Stock Price Change (2018-19)</th>
                <th>2018 Compensation</th>
                <th>Compensation Increase (2018-19)</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
               <th></th>
                <th>Name</th>
                <th>Title</th>
                <th>Company</th>
                <th>2019 Compensation</th>
                <th>Median Employee Pay</th>
                <th>Type of Position</th>
                <th>Stock Price Change (2018-19)</th>
                <th>2018 Compensation</th>
                <th>Compensation Increase (2018-19)</th>
            </tr>
        </tfoot>
    </table>

jQuery:

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "php/getTables.php"
} );

PHP 来自 getTables.php 文件。
这是从教程中复制的。 我刚刚插入了我的数据库详细信息。 代码有效; 我只是无法格式化我的货币列:

// DB table to use
$table = 'fortunate';
 
// Table's primary key
$primaryKey = 'fortunateID';
 
// 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' => 'fortunateID',  'dt' => 0 ),
    array( 'db' => 'name',  'dt' => 1 ),
    array( 'db' => 'title',     'dt' => 2 ),
   array( 'db' => 'company',     'dt' => 3 ),
   array( 'db' => 'compensation2019',     'dt' => 4 ),
   array( 'db' => 'median-employee-pay',     'dt' => 5 ),
   array( 'db' => 'type-of-position',     'dt' => 6 ),
   array( 'db' => 'stock-price-change-2018-19',     'dt' => 7 ),
   array( 'db' => 'compensation2018',     'dt' => 8 ),
   array( 'db' => 'compensation-increase',     'dt' => 9 )
   
);
 
// SQL server connection information
$sql_details = array(
    'user' => 'myUserName',
    'pass' => 'myPassword',
    'db'   => 'myDatabase',
    'host' => 'myHost'
);
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );
 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

您可以在 php 或 javascript 中添加回调以进行格式化。

后端解决方案:

$columns = array(
  array( 'db' => 'fortunateID',  'dt' => 0 ),
  array( 'db' => 'name',  'dt' => 1 ),
  array( 'db' => 'title',     'dt' => 2 ),
  array( 'db' => 'company',     'dt' => 3 ),
  array( 'db' => 'compensation2019',     'dt' => 4 ),
  array( 'db' => 'median-employee-pay',
         'dt' => 5,
         'formatter' => function( $d, $row ) {   
            return '$ '. number_format($number, 2);
        } ),
  array( 'db' => 'type-of-position',     'dt' => 6 ),
  array( 'db' => 'stock-price-change-2018-19',     'dt' => 7 ),
  array( 'db' => 'compensation2018',     'dt' => 8 ),
  array( 'db' => 'compensation-increase',     'dt' => 9 )   
);

或者您可以在前端进行格式化:

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "php/getTables.php"
    "columnDefs" : [
      {
        "targets": [3,4,6,7,8] //currency columns (0 indexed)
        "render": function ( data, type, row, meta ) {
                     let num = parseInt(data); //cast to number
                     let formatted = Number(num.toFixed(1)).toLocaleString();
                     return '$ ' + formatted; 
                 }
      }
 ]
} );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM