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

我正在使用 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"

} );

来自 getTables.php 文件的 PHP。

这是从教程中复制的。我刚刚插入了我的数据库详细信息。


宝慕林4294392
浏览 127回答 1
1回答

达令说

您可以添加回调以在 php 或 javascript 中进行格式化。后端解决方案:$columns = array(&nbsp; array( 'db' => 'fortunateID',&nbsp; 'dt' => 0 ),&nbsp; array( 'db' => 'name',&nbsp; 'dt' => 1 ),&nbsp; array( 'db' => 'title',&nbsp; &nbsp; &nbsp;'dt' => 2 ),&nbsp; array( 'db' => 'company',&nbsp; &nbsp; &nbsp;'dt' => 3 ),&nbsp; array( 'db' => 'compensation2019',&nbsp; &nbsp; &nbsp;'dt' => 4 ),&nbsp; array( 'db' => 'median-employee-pay',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'dt' => 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'formatter' => function( $d, $row ) {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '$ '. number_format($number, 2);&nbsp; &nbsp; &nbsp; &nbsp; } ),&nbsp; array( 'db' => 'type-of-position',&nbsp; &nbsp; &nbsp;'dt' => 6 ),&nbsp; array( 'db' => 'stock-price-change-2018-19',&nbsp; &nbsp; &nbsp;'dt' => 7 ),&nbsp; array( 'db' => 'compensation2018',&nbsp; &nbsp; &nbsp;'dt' => 8 ),&nbsp; array( 'db' => 'compensation-increase',&nbsp; &nbsp; &nbsp;'dt' => 9 )&nbsp; &nbsp;);或者您可以在前端进行格式化:$('#example').DataTable( {&nbsp; &nbsp; "processing": true,&nbsp; &nbsp; "serverSide": true,&nbsp; &nbsp; "ajax": "php/getTables.php"&nbsp; &nbsp; "columnDefs" : [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "targets": [3,4,6,7,8] //currency columns (0 indexed)&nbsp; &nbsp; &nbsp; &nbsp; "render": function ( data, type, row, meta ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let num = parseInt(data); //cast to number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let formatted = Number(num.toFixed(1)).toLocaleString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return '$ ' + formatted;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp;]} );
打开App,查看更多内容
随时随地看视频慕课网APP