使用其他总计列创建总计列

时间:2019-11-12 10:46:11

标签: mysql codeigniter datatable

我有一个使用Codeigniter&Datatables来管理向各种车辆发出的燃料的项目。一些数据如下:

+---------------+--------+----------+------------+
| Registered No |  Fuel  | Quantity | Unit Price |
+---------------+--------+----------+------------+
| A             | Diesel |       25 |    105.00  |
| A             | 2T     |        1 |    400.00  |
| B             | Petrol |       15 |    165.00  |
| B             | 2T     |        2 |    400.00  |
| B             | DS 40  |        4 |    600.00  |
+---------------+--------+----------+------------+

然后,我需要使用上面的数量和单价创建两个计算列,分别为“总计”和“总计”。在数据表中成功创建了“ Sub Total”列。可以创建“ Total”列。但是我的要求如下。

+---------------+--------+----------+------------+------------+------------+
| Registered No |  Fuel  | Quantity | Unit Price | Sub Total  |   Total    |
+---------------+--------+----------+------------+------------+------------+
| A             | Diesel |       25 |    105.00  |  2,625.00  |            |
| A             | 2T     |        1 |    400.00  |    400.00  |  3,025.00  |
| B             | Petrol |       15 |    165.00  |  2,475.00  |            |
| B             | 2T     |        2 |    400.00  |    800.00  |            |
| B             | DS 40  |        4 |    600.00  |  2,400.00  |  5,675.00  |
+---------------+--------+----------+------------+------------+------------+

我的控制器的相关部分如下:

$this->load->library('datatables');
$this->datatables
    ->select("registered_no,item_name, coalesce(fuel_qty, 0) as qty, fuel_price, fuel_qty * fuel_price as sub_total, SUM(fuel_qty * fuel_price) AS total")
    ->from('tbl_vehicle')
    ->join('tbl_direct_fuel', 'tbl_direct_fuel.vehicle=tbl_vehicle.vehicle_id', 'left')
    ->join('store_item', 'tbl_direct_fuel.fuel_item=store_item.item_id', 'left')
    ->where('tbl_direct_fuel.status=1 ');

$this->db->order_by('registered_no', 'DESC');

数据表脚本如下:

"aoColumns": [null,null, null, null,{class:'text-right'},{class:'text-right'}, {"class":"text-right",render: $.fn.dataTable.render.number( ',', '.', 2 )}, {"class":"text-right",render: $.fn.dataTable.render.number( ',', '.', 2 )}, null],
            "fnFooterCallback": function(nRow, aaData, iStart, iEnd, aiDisplay) {
                var api = this.api(), data;
                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };

                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };
                total1 = api
                    .column( 4 )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

               pageTotal1 = api
                    .column( 4, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

                $( api.column( 4 ).footer() ).html(
                   ( new Intl.NumberFormat().format(pageTotal1)) +' Litre (s)'
                );
                total2 = api
                    .column( 6 )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

                pageTotal2 = api
                    .column( 6, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);

                    }, 0 );

                $( api.column( 6 ).footer() ).html(
                new Intl.NumberFormat("SLR",{style: 'currency', currency: 'SLR', minimumFractionDigits: 2,}).format(pageTotal2)
                );

          total3 = api
                    .column( 7 )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

               pageTotal3 = api
                    .column( 7, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);

                    }, 0 );


                $( api.column( 7 ).footer() ).html(
                new Intl.NumberFormat("SLR",{style: 'currency', currency: 'SLR', minimumFractionDigits: 2,}).format(pageTotal3)
                );

Datatable的HTML如下:

<div class="table-responsive">
        <table id="ExData" cellpadding="0" cellspacing="0" border="0"
               class="table table-bordered table-condensed table-hover table-striped reports-table">
            <thead id="th">
                <tr class="primary">
                    <th>Registered No</th>                        
                    <th>Fuel</th>
                    <th>Quantity</th>
                    <th>Unit Price</th>
                    <th>Sub Total</th>
                    <th>Total</th>                  


                </tr>
            </thead>
            <tbody>
                <tr>
                    <td colspan="8" class="dataTables_empty">Loading Data from Server</td>
                </tr>
            </tbody>
            <tfoot class="dtFilter">
                <tr class="active">
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>


            </tr>
            </tfoot>
        </table>
    </div>

-根据要求,所有燃油问题均应显示在表格中-

未找到错误。正确生成了所需的输出,但合计列除外。可能出了什么问题。谁能帮我吗?

0 个答案:

没有答案
相关问题