![](/img/trans.png)
[英]Fetch data in the format [1,2,3] from mysql database using php and jquery
[英]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.