Yii2 Gridview - 如何在页脚上使用总计 属性
Yii2 Gridview - How use totals on footer property
我的 VALUE 列是:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'footer' => ???
],
如何使用 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;
},
],
现在这有几个问题,因为 http://demos.krajee.com/grid 意味着它只会添加它显示的内容,如果你有分页,它只会显示该页面上的总数。
如果您想要所有记录的总数,您应该使用不分页的数据提供程序手动添加它。
同样,我还没有真正尝试过,试一试。
我按照你说的尝试,但告诉我错误:trim() expects parameter 1 to be string, object given
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>",
有效
1. 创建 class 然后 2.create 列数组,3.configure 列,4.configure 网格
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' => function () 使用 ($total)
- 一列的总计在 yii2 中对我来说如下所示:
在您的网格视图中,例如:对于列 total_hours,您需要显示带有该列总和的页脚 total_hours 然后您通过扩展网格视图中的列来执行以下操作,在上面声明变量总计页面和启动 griview 覆盖列通过引用分配总传递以从每行或记录添加 total_hours,最后将值分配给 gridview 页脚属性。
[
'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,
我的 VALUE 列是:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'footer' => ???
],
如何使用 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;
},
],
现在这有几个问题,因为 http://demos.krajee.com/grid 意味着它只会添加它显示的内容,如果你有分页,它只会显示该页面上的总数。
如果您想要所有记录的总数,您应该使用不分页的数据提供程序手动添加它。
同样,我还没有真正尝试过,试一试。
我按照你说的尝试,但告诉我错误:trim() expects parameter 1 to be string, object given
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>",
有效 1. 创建 class 然后 2.create 列数组,3.configure 列,4.configure 网格
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' => function () 使用 ($total)
- 一列的总计在 yii2 中对我来说如下所示:
在您的网格视图中,例如:对于列 total_hours,您需要显示带有该列总和的页脚 total_hours 然后您通过扩展网格视图中的列来执行以下操作,在上面声明变量总计页面和启动 griview 覆盖列通过引用分配总传递以从每行或记录添加 total_hours,最后将值分配给 gridview 页脚属性。
[
'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,