I'm facing an issue while attempting to compute the sum of data within a column named "hours" using the TSumColumn::class in Yii. The problem arises due to additional content within the cells, specifically encapsulated within <small>(' . Utility::computeDays($model->hours) . 'gg)</small>.
I aim to extract only the Yii::$app->formatter->asDecimal($model->hours) part from each cell to compute the sum at the bottom of the column. However, it's crucial to retain the <small>(' . Utility::computeDays($model->hours) . 'gg)</small> part for display purposes in each cell.
Here's the current column definition:
[
'attribute' => 'hours',
'value' => function ($model, $key, $index, $column) {
return '<span class="font-montserrat bold text-success fs-16">'
. Yii::$app->formatter->asDecimal($model->hours) // only sum this value
. '</span> <small>(' . Utility::computeDays($model->hours) . '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 kartik\grid\DataColumn;
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 \yii\base\InvalidConfigException
*/
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;
}
}
I managed to solve that by extending a new class from TsumColumn and use it in my gridView table.