Add sub-total row to lists

Is there any way possible to append a row to show information about a subtotal for certains columns in ListController? OC v.3

Example:

Name Qty Price
Product 1 1 100
Product 2 1 200
Product 3 1 100
------------- - -----
Sub-total 3 400

Hi @andrewJEE

This is not a feature available in the List widget. Sub totals are much simpler in execution, they don’t use pagination or complex queries, so it would be much easier to just use a HTML partial for this.

Thank you very much. I will do just that.

Hi @andrewJEE

Thinking more about this today, you should be able to specify a customViewPath and then override any of the List widget’s view files.

It might be worth exploring…

This works really well. I over-rided (over-rode?) 1 file
modules/backend/widgets/lists/partials/_list.php

and added this after tbody:

<tfoot>
      <?= $this->makePartial('list_foot_row', ['records' => $records]) ?>
</tfoot>

Or could you just add above to the core?

Then in my new list_foot_row I have…

<?php 
// Example: collect totals for the lesson_hrs_per_day column
$totals = [];
$index = 0;
foreach ($records as $record):
    foreach ($columns as $key => $column):
        if (! isset ($totals[$key]))
        {
            $index++;
            $totals[$key]['classes'] = [
                'list-cell-index-'.$index,
                'list-cell-name-'.$column->getName(),
                'list-cell-type-'.$column->type,
                $column->getAlignClass(),
                $column->cssClass
            ];
            switch ($key)
            {
                case 'lesson_hrs_per_day':
                    $totals[$column->columnName]['value'] = 0;
                break;
                
                default:
                    $totals[$column->columnName]['value'] = '';
            }
        }
        switch ($key)
        {
            case 'lesson_hrs_per_day':
                $totals[$key]['value'] += $this->getColumnValue($record, $column);
            break;
        }
        
    endforeach;
endforeach;
?>
<tr class="rowLink">
    <?php if ($showCheckboxes): ?>
        <td class="list-checkbox nolink"><i class="icon-add-bold"></i>  </td>
    <?php endif; ?>
    <?php foreach ($totals as $key => $total): ?>
        <th class="<?= implode(' ', $total['classes']) ?>"><span>
            <?php 
            switch ($key)
            {
                case 'lesson_hrs_per_day':
                    $total['value'] = number_format($total['value'], 2);
                break;
                
            }
            ?>
            <?= $total['value'] ?>
            </span>
        </th>
    <?php endforeach ?>
</tr>
1 Like

Its safer to use:
$totals[$key][‘value’] += $record->{$column->columnName};
instead of
$totals[$key][‘value’] += $this->getColumnValue($record, $column);

because getColumnValue() returns formated value