Yii2: Kartik Gridview sum of a column in footer - php

I have installed the Kartik gridview extension, which is working fine.
But I couldn't find or missed it in the docs, how I can show the sum of a column in the footer.
This is my complete code in index.php
<?php
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'id',
[
//'attribute'=>'service_name',
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
[
'attribute'=>'room_category',
'value'=>'roomCategory.room_category'
],
'charges_cash',
'charges_cashless',
['class' => 'yii\grid\ActionColumn']
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'fontAwesome' => true,
'showPageSummary' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-default'
]
])
?>
</div></div>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
//'service_name',
[
//'attribute'=>'service_name',
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
// 'room_category',
[
'attribute'=>'room_category',
'value'=>'roomCategory.room_category'
],
'charges_cash',
'charges_cashless',
['class' => 'yii\grid\ActionColumn'],
],
'showFooter' => true
]); ?>
</div>
Looking for some help on this one.
Thanks.

I think you just need to add the page summary;
use kartik\grid\GridView;
// Create a panel layout for your GridView widget
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
Kartik describes it pretty well in the demo and plugin details.

Complete example:
GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => 'kartik\grid\ActionColumn',
'urlCreator' => function($action, $model, $key, $index) {
// using the column name as key, not mapping to 'id' like the standard generator
$params = is_array($key) ? $key : [$model->primaryKey()[0] => (string) $key];
$params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;
return Url::toRoute($params);
},
'contentOptions' => ['nowrap'=>'nowrap']
],
'id',
'name',
[
'attribute'=>'total_quantity',
'pageSummary' => true
],
[
'attribute'=>'quantity_sold',
'pageSummary' => true
],
],
'showPageSummary' => true
]);
Note: please rename column class from yii\grid to kartik\grid\ . This goes for DataColum, ActionColumn etc

Related

Modifty GridView::Widget content Yii

I'm trying to modify a field of the GridView that I obtained following the Gii tutorial on the Yii framework website.
GII PAGE
I'm not satisfied on how the population field looks, so I'm trying to convert it with some separators.
This is the index.php of the Country View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'label' => 'Population',
'value' => 'population',
/* 'value' => Yii::$app->formatter->asDecimal((integer)population) */
/* 'value' => Yii::$app->formatter->asDecimal($model->population) */
/* 'value' => Yii::$app->formatter->asDecimal('population') */
/*'value' => Yii::$app()->format->number('population')*/
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
I don't know why in the CountryModel the population field is listed as integer:
['population'], 'integer'
And then when I try to convert it in the view I have some problems because 'population' is basically a String.
I commented out some of my attempts.
You can use yii\i18n\Formatter
Go to your common\config\main.php if you are using app-advanced or the app/config/main.php if app-basic and add the following under components array.
'formatter' => [
'thousandSeparator' => ',',
],
Now you can format any given number like below
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'attribute' => 'population',
'label' => 'Population',
'value' => function($model){
return Yii::$app->formatter->asInteger($model->population); //Output 1,000
},
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
Update: Like was mentioned in comments, You can also use this two technics to format the value:
//Gridview
....
[
'attribute' => 'population',
'format' => 'integer',
],
....
and/or:
//Gridview
....
'population:integer',
....

how to hide gridview before search?

<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'pk_int_payroll_id',
[
'attribute' => 'fk_int_emp_id',
'value' => 'fkIntEmp.vchr_name',
'filter' => Html::activeDropDownList($searchModel, 'fk_int_emp_id', ArrayHelper::map(TblPayroll::find()->asArray()->all(), 'fk_int_emp_id', 'fk_int_emp_id'),['class'=>'kartik\grid\ActionColumn','prompt' => 'Select name'])
],
'vchr_worked_hours',
'vchr_actual_hours',
[
'attribute' => 'fk_int_payroll_month',
'value' => 'fkIntPayrollMonth.vchr_month'
],
[
'attribute' => 'fk_int_payroll_year',
'value' => 'fkIntPayrollYear.year'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?> </div>
this is my gridview with search. i want to search and result should show in
gridView. it working correctly, but before search the gridview show all the data,## how to hide gridview before search??? is it possible

add a button to grid view in yii2

i'm a new yii2 developer !
i made a GridView and the code is shown below :
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\ActionColumn'],
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
'id',
'countryCode',
'countryName',
'currencyCode',
],
]); ?>
<?php Pjax::end(); ?>
a screenshot of output :
OUTPUT
now i want to have a column contain some button and that button for example open a page or somthing else !
my problem is how can i create that column ?
You can also add the button (or as many as you like) to the existing action column like this
<?= GridView::widget([
::
::
'columns' => [
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete} {myButton}', // the default buttons + your custom button
'buttons' => [
'myButton' => function($url, $model, $key) { // render your custom button
return Html::a(..);
}
]
]
::
::
'currencyCode'
]
]); ?>
Example:
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\ActionColumn'],
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
'id',
'countryCode',
'countryName',
'currencyCode',
[
'label' => 'My Label',
'format' => 'raw',
'value' => Html::a('Click me', ['site/index'], ['class' => 'btn btn-success btn-xs', 'data-pjax' => 0])
]
],
]); ?>
<?php Pjax::end(); ?>
Try this way:
[
'header' => 'Button',
'content' => function($model) {
return Html::a(..);
}
],
More Info

How to create grid view in Yii2 (after clicking on particular id from list open all detail of that id )?

In index.php of Member module I have write this widget to open detail after click on id.but here open a Url code as it is not url page.please give me solution. and suggestion what i use because i am not familiar with Yii2.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=>'practiceCode',
'format' => 'url',
'value'=>function ($data) {
return Html::a(Html::encode("View"),'practice/view');
},
],
'memberCode',
'firstName',
'lastName',
'email:email',
'mobile',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'lable'=>'practiceCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget){
return Html::a($model->practiceCode,['practice/view','id'=>$model->practiceCode],['title'=>'Go!','target'=>'_blank']
);
// return Html::a(Html::encode("View"),'practice/view');
// return Html::a(Html::encode($data- >practiceCode),'practice/view');
},
],
Now it's working 'attribute'=>'practiceCode' instead of 'lable'=>'practiceCode',

Yii2: Kartik Gridview export config?

I am using Kartik Gridview. Export is also working fine, but summary data is not getting exported, but showing fine in the view.
As also I want to change the filename of the downloaded file.
I have tried this code, but nothing seems to be working
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true,
'exportConfig'=> [
GridView::EXCEL=>[
'filename' => Yii::t('kvgrid', 'Appointments'),
'showPageSummary' => true,
]
],
.....
That is I have added the export config in the gridview widget but it doesn't seems to be working.
What I am doing wrong here?
please suggest any changes.
try this
<?= ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'columnSelectorOptions'=>[
'label' => 'Columns',
'class' => 'btn btn-danger'
],
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-primary'
]
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
],
]); ?>

Categories