I’m facing an issue while attempting to compute the sum of data within a column named "ore" using the TSumColumn::class in Yii. The problem arises due to additional content within the cells, specifically encapsulated within <small>(' . Utility::calcoloGiornate($model->ore) . 'gg)</small>.
I aim to extract only the Yii::$app->formatter->asDecimal($model->ore)
part from each cell to compute the sum at the bottom of the column. However, it’s crucial to retain the <small>(' . Utility::calcoloGiornate($model->ore) . 'gg)</small>
part for display purposes in each cell.
Here’s the current column definition:
[
'attribute' => 'ore',
'value' => function ($model, $key, $index, $column) {
return '<span class="font-montserrat bold text-success fs-16">'
. Yii::$app->formatter->asDecimal($model->ore) // only sum this value
. '</span> <small>(' . Utility::calcoloGiornate($model->ore) . 'gg)</small>'; //don't include this value while summing but keep it displayed in the cell
},
'format' => 'html',
'class' => TSumColumn::class,
],
Here is the TsumColumn class I’m using:
use kartikgridDataColumn;
class TSumColumn extends DataColumn
{
public function getDataCellValue($model, $key, $index)
{
$value = parent::getDataCellValue($model, $key, $index);
if (is_numeric(str_replace(',','.',strip_tags($value)))) {
$this->footer += round(str_replace(',','.',strip_tags($value)),2);
}
return $value;
}
/**
* Renders the footer cell content.
* The default implementation simply renders [[footer]].
* This method may be overridden to customize the rendering of the footer cell.
* @return string the rendering result
* @throws yiibaseInvalidConfigException
*/
protected function renderFooterCellContent()
{
if($this->footer !== null && trim($this->footer) !== '') {
if($this->format == 'currency') {
return Yii::$app->formatter->asCurrency($this->footer);
}
return Yii::$app->formatter->asDecimal($this->footer);
}
return $this->grid->emptyCell;
}
}
2
Answers
I managed to solve that by extending a new class from TsumColumn and use it in my gridView table.
The main issue with your code is that you are formatting value for output in
value
callback instead offormat
callback. Because of that you have to parse already formatted html code to get value for footer.If you defined your column like this:
Then you wouldn’t need to parse html code in your
TSumColumn
class becauseparent::getDataCellValue($model, $key, $index);
would return the value of$model->ore
property. So yourTSumColumn::getDataCellValue
would simply look like this:Ultimately, if the purpose of
TSumColumn
class is just to sum values you can get rid of it completely and sum values usingarray_sum
andArrayHelper::getColumn()
:Assuming
$dataProvider
is a variable where you have the instance of your data provider. The$dataProvider->getModels()
will return array of all models on current page. I’ve used callback as second parameter inArrayHelper::getColumn()
method call to apply rounding to values ofore
property.