简体   繁体   English

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

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

I'm using the dataTables plugin to deal with some data.我正在使用 dataTables 插件来处理一些数据。 I'm using a mysql database and php.我正在使用 mysql 数据库和 php。 I'm able to display the data in the database, but I'm not able to format it.我能够在数据库中显示数据,但我无法对其进行格式化。 I have some fields that are dollar amounts, and I'd like to add dollar signs and thousands commas.我有一些字段是美元金额,我想添加美元符号和千位逗号。 Can anyone tell me how to do this please?谁能告诉我该怎么做? The columns properties in dataTables don't work for me because the columns are being defined in the php file. dataTables 中的列属性对我不起作用,因为这些列是在 php 文件中定义的。 The code below works, but I can't format my currency columns.下面的代码有效,但我无法格式化我的货币列。

HTML: 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: jQuery:

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

PHP from getTables.php file. PHP 来自 getTables.php 文件。
This is copied from a tutorial.这是从教程中复制的。 I just plugged in my database details.我刚刚插入了我的数据库详细信息。 The code works;代码有效; I just can't format my currency columns:我只是无法格式化我的货币列:

// 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 )
);

You can add a callback to do your formatting in either the php or javascript.您可以在 php 或 javascript 中添加回调以进行格式化。

Backend solution:后端解决方案:

$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 )   
);

Or you could do your formatting on the front end:或者您可以在前端进行格式化:

$('#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