Skip to content

Column Summary

PowerGrid can include data summaries inside each columns() header.

Summaries can display the column's sum, count, average, min and max value.

Usage

Summaries are chained to the Column::add() method.

withSum

  • Will display the sum of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Sum'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $footerIf is true, Powergrid will create a row in the footer of the table.false

WARNING

This will pre-process all the data in your database to work with the sum of all records. ->get(), in all cases, only one request is made;

Example:

php
//...
Column::make(__('Price'), 'price')
    ->withSum('Sum', true, false),
//...
Column::make(__('Price'), 'price')
    ->withSum('Sum', true, false),

Result: Output


withCount

  • Will display the count of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Count'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $footerIf is true, Powergrid will create a row in the footer of the table.false

Example:

php
//...
Column::make(__('Price'), 'price')
    ->withCount('Count', true, false),
//...
Column::make(__('Price'), 'price')
    ->withCount('Count', true, false),

Result: Output


withAvg

  • Will display the avg of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Avg'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $footerIf is true, Powergrid will create a row in the footer of the table.false

Example:

php
//...
Column::make(__('Price'), 'price')
    ->withAvg('Avg', true, false),
//...
Column::make(__('Price'), 'price')
    ->withAvg('Avg', true, false),

Result: Output


withMin

  • Will display the min of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Min'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $footerIf is true, Powergrid will create a row in the footer of the table.false

Example:

php
//...
Column::make(__('Price'), 'price')
    ->withMin('Min', true, false),
//...
Column::make(__('Price'), 'price')
    ->withMin('Min', true, false),

Result: Output


withMax

  • Will display the max of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Max'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $footerIf is true, Powergrid will create a row in the footer of the table.false

Example:

php
//...
Column::make(__('Price'), 'price')
    ->withMax('Max', true, false),
//...
Column::make(__('Price'), 'price')
    ->withMax('Max', true, false),

Result: Output

Summarizing formatted data

PowerGrid provides a convenient way to use summarizeFormat() method to display formatted data in your table.

To summarize formatted data (e.g. currency), you must add the passing summaryFormat method that returns an array of key and value (field, Closure).

Ex:

php
  public function summarizeFormat(): array
  {
        return [
            'price.{sum,avg}' => function ($value) {
               return (new \NumberFormatter('en_US', \NumberFormatter::CURRENCY))
                    ->formatCurrency($value, 'USD');
             },
             'price.{count,min,max}' => fn ($value) => $value,
        ];
  }
  public function summarizeFormat(): array
  {
        return [
            'price.{sum,avg}' => function ($value) {
               return (new \NumberFormatter('en_US', \NumberFormatter::CURRENCY))
                    ->formatCurrency($value, 'USD');
             },
             'price.{count,min,max}' => fn ($value) => $value,
        ];
  }

Output


Created By Luan Freitas and DanSysAnalyst