KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

How could I remove [invoice number and Party] fields from detail list #1809

Open Ali opened this topic on on Dec 30, 2020 - 4 comments

Ali commented on Dec 30, 2020

I am using Datatable[RowGroup], and I want to remove fields[invoice_no and Party] from the list of details, and should be remain at the top. `php

<?php

    DataTables::create(array(
        "dataSource"=>$this->dataStore("invoice_details"),
        "cssClass"=>array(
            "table"=>"table table-striped table-bordered"
        ),
        "columns" => [
            "Dated","salesman", "Party", "invoice_no", "iteme_code","Desctiption", "Unit", "Qty", "gross_sales",
            "discount", "net_amount", "vat", "total_amount"
        ],
        "fastRender" => true,
        "clientRowGroup" => [
            "Dated" => [
                'direction' => 'desc',
                'calculate' => [
                    'totalSales' => [
                        'sum',  //'sum', 'count', 'avg', 'min', 'max'
                        'total_amount',
                        "format" => "function(value) {
                          return (1*value).toLocaleString(); // this for thousand comma separator
                        }"
                    ],
                ],
                "top" => "<td colspan='999'>{expandCollapseIcon} Top: Date: {Dated} | Total: {totalSales}</td>",
                "bottom" => "<td colspan='999'>{expandCollapseIcon} Bottom: Date: {Dated} | Customer sales: {totalSales}</td>",
            ],
            "salesman" => [
                'direction' => 'desc',
                'calculate' => [
                    'totalSales' => ['sum', 'total_amount',
                        "format" => "function(value) {
                          return (1*value).toLocaleString(); // this for thousand comma separator 
                        }"
                    ],
                    'total_qty' => ['sum', 'Qty',
                    ],
                    'countDistinctLine' => [
                        'field' => 'invoice_no',
                        "aggregate" => "function(rows, group, aggFieldIndex) 
                        {
                                var initAgg = {
                                    values: {},
                                    count: 0
                                };
                                return rows
                                .data()
                                .pluck(aggFieldIndex)
                                .reduce( function (agg, b) {
                                      if (agg.values[b] !== true) {
                                          agg.count += 1;
                                          agg.values[b] = true;
                                      } 
                                      return agg;
                                  }, initAgg)
                          }",
                        "format" => "function(agg) {return agg.count;}",
                    ],
                ],
                "top" => "<td colspan='999'>{expandCollapseIcon} Top: Line: {salesman} | Total: {totalSales} | Total Qty: {total_qty} | Count: {countDistinctLine} </td>",
            ],
            "invoice_no" => [
                'direction' => 'desc',
                'calculate' => [
                    'totalSales' => ['sum', 'total_amount',
                        "format" => "function(value) {return (1*value).toLocaleString();}"
                    ],
                    'total_qty' => ['sum', 'Qty'],
                    'countSales' => ['count', 'total_amount'],
                ],
                "top" => "<td colspan='999'>{expandCollapseIcon} Top: Line: {invoice_no} | Total: {totalSales} | Total Qty: {total_qty} | Count: {countSales} </td>",
                // "bottom" => "<td colspan='999'>{expandCollapseIcon} Bottom: Line: {iteme_code} | Line sales: {totalSales}</td>",
            ],
        ],

my problem if I removed invoice_no from columns attribute it will thrown error index not found
so how could I do that without lossing my fields in the top?


![image](https://cdn.koolreport.com/assets/images/editor/c5/image5fec60ebdf265.png)
David Winterburn commented on Dec 31, 2020

Hi Ali,

Hide it instead of removing it. It's as simple as this:

DataTables::create(array(
    ...
    "columns" => array(
        "invoice_no" => array(
            ...
            "visible" => false
        ),
    ),
));
Ali commented on Dec 31, 2020

Okay it's work.

so also how could I display Party column in the top like bewlow in picture .


"Party" => [
                    'direction' => 'desc',
                ],
                "invoice_no" => [
                    'direction' => 'desc',
                    'calculate' => [
                        'totalSales' => ['sum', 'total_amount',
                            "format" => "function(value) {return (1*value).toLocaleString();}"
                        ],
                        'total_qty' => ['sum', 'Qty'],
                        'countSales' => ['count', 'total_amount'],
                    ],
                    "top" => "<td colspan='999'>{expandCollapseIcon} Invoice #: {invoice_no} | Customer:{Party} | Total: {totalSales} | Total Qty: {total_qty} | Count: {countSales} </td>",
                  
                ],

David Winterburn commented on Jan 4, 2021

Hi Ali,

So far DataTables' row group can only replace its most current group name, i.e only the group row of customer can only replace customer name while the group row of invoice can replace only replace the invoice number.

We will update DataTables so that a detail group row could replace all of its parent group names beside its own. In the mean time if you want this feature please open the file koolreport/datagrid/DataTables.php and replace this function:

    protected function buildClientRowGroup()
    {
        ...
    }

with this one:

    protected function buildClientRowGroup()
    {
        $showColumnKeys = $this->showColumnKeys;
        $showColumnKeys = array_flip($showColumnKeys);
        $orderOption = [];
        $rowGroupOption = [
            'dataSrc' => []
        ];
        $startRender = $endRender = "var startRenderLevels = endRenderLevels = {}; ";
        $expandIcon = Util::get($this->params, 'expandIcon', "<i class=\'far fa-plus-square\' aria-hidden=\'true\'></i>");
        $collapseIcon = Util::get($this->params, 'collapseIcon', "<i class=\'far fa-minus-square\' aria-hidden=\'true\'></i>");
        $grLevel = 0;
        $grCols = [];
        foreach ($this->clientRowGroup as $grCol => $grOption) {
            $grCols[$grLevel] = $grCol;
            $colIndex = $showColumnKeys[$grCol];
            $dir = Util::get($grOption, 'direction', 'asc');
            $orderOption[] = [$colIndex, $dir];
            $rowGroupOption['dataSrc'][] = $colIndex;

            //Build agg values
            $calculate = Util::get($grOption, 'calculate', []);
            $aggValues = [];
            foreach ($calculate as $aggName => $aggConfig) {
                $aggOp = Util::get($aggConfig, 0, Util::get($aggConfig, 'operator'));
                $aggFunc = Util::get($aggConfig, 'aggregate');
                $aggField = Util::get($aggConfig, 1, Util::get($aggConfig, 'field'));
                $aggColIndex = $showColumnKeys[$aggField];
                if ($aggFunc) {
                    $aggVarName = "{$aggField}AggValue{$grLevel}" . md5($aggFunc);
                    $aggValue = "
                    var $aggVarName = $aggFunc(rows, group, $aggColIndex);
                    ";
                } else {
                    $aggVarName = "{$aggField}AggValue{$grLevel}" . md5($aggOp);
                    switch ($aggOp) {
                        case "avg":
                        case "sum":
                            $reduceFunc = "return a + 1*b.replace(/[^\d\.\-]/g, '');";
                            $initValue = 0;
                            break;
                        case "count":
                            $reduceFunc = "return a + 1;";
                            $initValue = 0;
                            break;
                        case "min":
                            $reduceFunc = "return a < b ? a : b;";
                            $initValue = "Number.MAX_VALUE";
                            break;
                        case "max":
                            $reduceFunc = "return a > b ? a : b;";
                            $initValue = "- Number.MAX_VALUE";
                            break;
                    }
                    $aggValue = "
                        var $aggVarName = rows
                            .data()
                            .pluck($aggColIndex)
                            // .pluck('$aggField')
                            .reduce( function (a, b) {
                                $reduceFunc
                            }, $initValue);
                    ";
                    if ($aggOp === 'avg') $aggValue .= " / rows.count()";
                }

                $formatFunc = Util::get($aggConfig, 'format');
                if ($formatFunc) {
                    $aggValue .= "$aggVarName = $formatFunc($aggVarName);";
                }

                $aggValues[$aggName] = [
                    'name' => $aggVarName,
                    'value' => $aggValue
                ];
            }

            //Replace agg place holders
            $top = addslashes(Util::get($grOption, "top", ""));
            $bottom = addslashes(Util::get($grOption, "bottom", ""));
            foreach ($calculate as $aggName => $aggConfig) {
                $aggValue = $aggValues[$aggName]['value'];
                $startRender .= " $aggValue ";
                $endRender .= " $aggValue ";
                $aggVarName = $aggValues[$aggName]['name'];
                $top = str_replace("{{$aggName}}", "' + $aggVarName + '", $top);
                $bottom = str_replace("{{$aggName}}", "' + $aggVarName + '", $bottom);
            }
            $onclick = "onclick=\'KR{$this->name}.expandCollapse(this);\'";
            $top = str_replace("{expandCollapseIcon}", "<span class=\'group-expand\' style=\'display:none;\' $onclick >$expandIcon</span><span class=\'group-collapse\' $onclick >$collapseIcon</span>", $top);
            $replaceTop = "
                var top = '$top';
            ";
            for ($i =0; $i<=$grLevel; $i++) {
                $replaceTop .= "
                    top = top.replace(/{{$grCols[$i]}}/g, window['dtRowGroups'][{$i}]);
                ";
            }
            $startRender .= "
                $replaceTop
                startRenderLevels[{$grLevel}] = $('<tr/>')
                    .append( top )
                ;
            ";

            $bottom = str_replace("{expandCollapseIcon}", "<span class=\'group-expand\' style=\'display:none;\' $onclick >$expandIcon</span><span class=\'group-collapse\' $onclick >$collapseIcon</span>", $bottom);
            $replaceBottom = "
                var bottom = '$bottom';
            ";
            for ($i =0; $i<=$grLevel; $i++) {
                $replaceBottom .= "
                    bottom = bottom.replace(/{{$grCols[$i]}}/g, window['dtRowGroups'][{$i}]);
                ";
            }
            $endRender .= "
                $replaceBottom
                endRenderLevels[{$grLevel}] = $('<tr/>')
                    .append( bottom )
                ;
            ";

            $grLevel++;
        }

        $startRenderFunc = "
            function ( rows, group, level ) {
                // console.log('start render group row');
                // console.log(rows, group, level);
                window['dtRowGroups'] = window['dtRowGroups'] || {};
                window['dtRowGroups'][level] = group;
                {$startRender}
                return startRenderLevels[level];
            }
        ";
        $rowGroupOption['startRender'] = $startRenderFunc;
        $endRenderFunc = "
            function ( rows, group, level ) {
                // console.log(rows, group, level);
                {$endRender}
                return endRenderLevels[level];
            }
        ";
        $rowGroupOption['endRender'] = $endRenderFunc;

        $this->options['order'] = $orderOption;
        $this->options['rowGroup'] = $rowGroupOption;
    }

Let us know the result. Thanks!

Ali commented on Jan 5, 2021

Thanks, it worked :)

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None