Php Yii2 Gridview-如何使用页脚总计属性
我的值列是:Php Yii2 Gridview-如何使用页脚总计属性,php,yii2,Php,Yii2,我的值列是: [ 'attribute' => 'value', 'format' => 'raw', 'contentOptions'=>['style'=>'width: 10%;text-align:left'], 'footer' => ??? ], 如何使用页脚上的行总数属性?我没有尝试过这样做,但尝试像这样定义列 在文件的顶部定义 $total = 0; 之后,按如下方式定义列: [
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'footer' => ???
],
如何使用页脚上的行总数属性?我没有尝试过这样做,但尝试像这样定义列 在文件的顶部定义
$total = 0;
之后,按如下方式定义列:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'value'=>function ($model, $key, $index, $widget) use ($total) {
$total += $model->value;
return $model->value;
},
'footer' => function () use ($total)
{
//format the total here
return $total;
},
],
现在有几个问题,因为这意味着它将只添加显示的内容,如果你有分页,它将只显示该页面上的总数
如果需要所有记录的总计,则应使用数据提供程序手动添加,无需分页
再一次,我没有真正尝试过这个,只是尝试一下。我按照你说的做了尝试,但显示了错误:trim()希望参数1是字符串,对象是给定的。
protected function renderFooterCellContent()
{
return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
}
在Yii 1中,我可以很好地工作,我只是在模型中创建了这个函数
public function getTotals($ids)
{
if($ids){
$ids = implode(",",$ids);
$connection=Yii::app()->db;
$command=$connection->createCommand("SELECT SUM(value) FROM `tb_cashbook` where id in ($ids)");
$amount = $command->queryScalar();
return "R$ ".Yii::app()->format->formatNumber($amount);
}
else
return null;
}
在视图中,按此方式在页脚属性中:
'footer'=>"<strong>".$model->getTotals($model->search()->getKeys())." </strong>",
'footer'=>“”$model->getTotals($model->search()->getKeys())。“”,
它的工作原理
1.创建类,然后2.创建列数组,3.配置列,4.配置网格
namespace app\components;
class PTotal {
public static function pageTotal($provider, $fieldName)
{
$total=0;
foreach($provider as $item){
$total+=$item[$fieldName];
}
return $total;
}
$provider = new ActiveDataProvider([
'query' => $query,
'sort' => $sort,
]);
$grid_columns=[
[
'attribute' => 'saldo_in',
'footer'=>PTotal::pageTotal($provider->models,'saldo_in'),
]
]
echo GridView::widget([
'dataProvider' => $provider,
'showFooter'=>TRUE,
'footerRowOptions'=>['style'=>'font-weight:bold;text-decoration: underline;'],
'columns' =>$grid_columns,
]);
在上面的视图文件中,通过以下代码计算页脚和
$amount = 0;
if (!empty($dataProvider->getModels())) {
foreach ($dataProvider->getModels() as $key => $val) {
$amount += $val->amount;
}
}
现在在gridview中,只需按如下方式传递amount变量
[
'attribute' => 'amount', 'label' => 'Charged Amount',
'value' => function ($model, $key, $index, $widget) {
return Yii::$app->formatter->asCurrency($model->amount, 'INR');
},
'footer' => $amount,
],
我已经试过了,它会工作的…yii2不支持'footer'=>函数()使用($total)
- 在yii2中,我的专栏作品总计如下:
[
'header' => 'total_hours',
'attribute' => 'total_hours',
'value'=>function ($model, $key, $index, $widget) use (&$total) {
$total += $model->total_hours;
$widget->footer = $total;
return $model->total_hours ;
},
],
并添加'showFooter'=>true,您可以使用它来构建您想要的大部分内容。当然,但是在没有扩展的情况下,本机有没有正确的方法来实现这一点?它工作正常,我只是在我的模型类中创建函数。。谢谢Mihai和leonid@leonid-我还在gridview中查找页脚中的总计。我只是想知道上面代码的哪一部分将出现在类和视图中。谢谢。我认为选项名“footerRowOptions”是错误的,它应该是“footerOptions”。只是需要添加:“showFooter”=>TRUE,在GridView::widget。是的,使用
使用(&$total)
在函数中传递变量。无需在模型中创建另一个函数,它就非常有用<代码>$widget->footer=$total以存储变量