KoolReport's Forum

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

Table grouping calculate issue when trying to hide zero in column #2917

Open Andrew Guattery opened this topic on on Dec 26, 2022 - 3 comments

Andrew Guattery commented on Dec 26, 2022

Good morning, I am having an issue with Table grouping not summing values if I do something like this in the column format:

        "label"=>"Holiday",
        
    'formatValue'=>function($value) {
        if($value == 8){
        return 8;
        }else{
        return "";
        }
        },

Notice the return ""; this is to hide the zero value. Problem is when I do this in grouping:

            'pdholidayAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'IsHoliday',
                   //"format" => "function(value) {return value.toFixed(2) * 8;}",
                ],

It returns blank. If I remove the column formatting to hide the zeros the group sum works. Little help?

Sebastian Morales commented on Dec 27, 2022

Would you mind providing the full DataTables' create code as well as some screenshots for us to check this issue for you? Tks,

Andrew Guattery commented on Dec 27, 2022

Good morning Sebastian, Unfortunately I've already coded around the issue so I can't provide screenshots with the original code. I can however explain it in detail and it can be replicated - I don't think it is a "bug" per-se; more like a methodology thing.

Ok, so the table create was pretty simple - here is a cobbled-together example:

    Table::create(array(
       "name" => "payrollbyemp",
       "excludedColumns"=>array("LSTNAM","USRNAME"),
     //  "themeBase"=>"bs4", // Optional option to work with Bootsrap 4
           "dataStore"=>$this->dataStore("payrollpdf"), 
           "sorting"=>array(
        "LSTNAM"=>"asc",
        "PERIOD_DT"=>"asc"
    ),
           "options"=>array(
           'autoWidth' => false,
           ),

   "method"=>"post",
    "columns"=>array (
        "HAS_COMMNTS"=>[
    "label"=>"<b>+</b>",
    'formatValue'=>function($value, $row, $cKey) {
        if($row["HAS_COMMNTS"] == "*"){
        return '<i class="far fa-plus-square expand-collapse-detail-icon" aria-hidden="true"></i>';
        }else{
        return '';
        }
        }
    ],
    
    "STR_ID"=>[
    "label"=>"Store",
    "format"=>"string"],
    "USR_ID"=>[
    "label"=>"User ID",

    ],

    "PERIOD_DT"=>[
    "label"=>"Clock Date",
    'formatValue'=>function($value){
    return date('D m-d',strtotime(str_replace('-','/',$value)));
    },
   // "displayFormat"=>"m-d-Y",
   ],

    "IsHoliday"=>[
    "label"=>"Holiday",
    'formatValue'=>function($value) {
        if($value == 8){
        return 8;
        }else{
        return "";
        }
        },
    ],
    ),
    "grouping" => [
        "LSTNAM" => [
   
            //'direction' => 'asc', //'asc', 'desc'
            'calculate' => [
            'username' =>[
            'min',
            'USRNAME'
            ],
            'pdholidayAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'IsHoliday',
                   //"format" => "function(value) {return value.toFixed(2) * 8;}",
                ],
                'ttlbreakAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'BREAK_TIM',
                   "format" => "function(value) {return value.toFixed(2);}",
                ],
                 'pdMealAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'MEAL_TIM',
                 //  "format" => "function(value) {return value.toFixed(2);}",
                ],
            'pdptoAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'PTO_TO_PAY',
                 //  "format" => "function(value) {return value.toFixed(2);}",
                ],
                'pdhrsAmount' => [
                    'sum', //'sum', 'count', 'avg', 'min', 'max'
                    'MINUTES_WRKD',
                 //  "format" => "function(value) {return value.toFixed(2);}",
                ],
                'totalhoursAmount' => [
                'sum',
                'TOTALPD',
                ],
            ],
                      "top"=>"",
           "bottom"=>"<td colspan=\"6\"><b>Total for user username</b></td><td><b>pdholidayAmount</b></td><td stle=\"font-size: 12px;\"><b>(ttlbreakAmount)</b></td><td><b>pdMealAmount</b></td><td><b>pdptoAmount</b></td><td><b>pdhrsAmount</b></td><td><b>totalhoursAmount</b></td>"
           ],
           ],
               "cssClass"=>array(
        "table"=>"table table-bordered table-height",
        "tr"=>function($row){

        },
        "th"=>function($columnName){
             if($columnName == "SEQ_NO_CLCK_IN" || $columnName == "SEQ_NO_CLCK_OUT"){ return 'nodisplay';}
        },
        "td"=>function($row,$columnName){
       

 if($columnName == "IsHoliday" && $row["IsHoliday"] == "0"){ return "notvisible"; }
   },

    ),
 ));//End Table::create

THIS MAY BE MISSING A CLOSING BRACE/BRACKET/SOMETHING AS I CUT AND PASTED IT TOGETHER! Ok, so the column "IsHoliday" format function is where the issue occurs; as posted here it will return 8 if it is a holiday or blank - it will display ok BUT then in the grouping 'pdholidayAmount' will be blank. If I change that function to return 0 instead of blank then the group sum works fine.

So how to fix this?

Well, I did it with css! Notice the "td" css class returns class "notvisible" for IsHoliday if the value is 0."notvisible" class sets

.notvisible{
visibility: hidden;
}

which hides the cell data. Also note that if you change the css formula to look for a blank:

 if($columnName == "IsHoliday" && $row["IsHoliday"] == ""){ return "notvisible"; }

It will not work! Also note that

.nodisplay{
display: none;
}

does not work! Aside from the fact it removes the column cell (thus breaking the format) it also does not allow the group-sum to work. I tested this just out of curiosity while trying to figure out what was going on here.

So, it looks like the better option to maintain programmability is to return zero and set visibility to hidden.

hope this helps, Andy

Sebastian Morales commented on Dec 29, 2022

Your solution to hide the 0-value cells is very creative. Tks,

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
help needed

DataGrid