How to add a new column to the gridview in yii2? - yii2

I have 4 columns in my GridView, which contains the data I need, but now I need one more column. How can I create it in my GridView?
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'surname',
'employment_date',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Thanks for the help

you can add more columns according to yii api:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'surname',
'employment_date',
[
'class' => DataColumn::className(), // this line is optional
'attribute' => 'name',
'format' => 'text',
'label' => 'Name',
],
['class' => CheckboxColumn::className()],
['class' => 'yii\grid\ActionColumn'],
]
]); ?>
you can read this link

<?= GridView::widget([
....
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'surname',
'employment_date',
'columns'=>[
'attribute'=>'New Column',
'value'=>'tbl.new_column'
],
//OR This
new_column,
['class' => 'yii\grid\ActionColumn'],
],
]); ?>

Related

Retrieving data from Gridview as array in Yii2

i'd like to ask how to retrieving data as array from gridview
as mentioned in this thread
How can I get the selected data/item rows in CheckboxColumn Gridview - Yii2
what i have been doing is :
in the controller
public function actionCetakdispo{
$action=Yii::$app->request->post('action');
$selection=(array)Yii::$app->request->post('selection');
print_r($selection);
exit;
}
in view :
<?=Html::beginForm(['controller/rekapsm'],'post');?>
<?= Html::a('Cetak Dispo', ['cetakdispo'], ['class' => 'btn btn-
primary']) ?>
<?= Html::a('Cetak Register', ['cetakreg'], ['class' => 'btn btn-
primary']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'kategori',
'nama_wp',
'nama',
'nomor_surat',
'tgl_surat',
'perihal',
'ket',
['class' => 'yii\grid\CheckboxColumn'],
],
]); ?>
but nothing data retrieve to array..please give me lessons or referrence to read..many thanks..
UPDATED :
i change view become likethis :
<div class="row" style="margin-left:870px;" >
<?=Html::beginForm(['cetakdispo'],'post');?>
<?=Html::submitButton('Cetak Dispo', ['class' => 'btn btn-primary']);?>
<?= Html::a('Cetak Register', ['cetakreg'], ['class' => 'btn btn-primary']) ?>
</div>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn',],
'id',
'kategori',
'nama_wp',
'nama',
'nomor_surat',
'tgl_surat',
'perihal',
'ket',
['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $widget) {
return ['value' => $model['id'] ];
},],
],
]); ?>
<?= Html::endForm();?>
</div>
and its works...

Specify Font Size of Gridview in yii2

I'm printing gridview in pdf. The gridview is fine. But the cells are not filling in properly. I think if I can decrease the font a bit, the cell will be filled in properly. I've decreased the width. But the cells are being distorted.
Code of Gridview -
<?= GridView::widget([
'dataProvider' => $dataProvider1,
//'filterModel' => $searchModel,
'layout'=>"{items}",
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'hsncode',
[
'label' => 'Productname',
'attribute' =>'productname',
'headerOptions' => ['style' => 'width:20%'],
//'contentOptions' => ['class' => 'col-lg-1'],
//'format'=>['decimal',2]
],
'batchno',
//'expdate',
[
'attribute'=>'expdate',
'format'=>['DateTime','php:m-y'],
'headerOptions' => ['style' => 'width:6%'],
],
'mrp',
'rate',
'qty',
'free',
'discount',
[
'label' => 'Value',
'attribute' =>'value',
//'contentOptions' => ['class' => 'col-lg-1'],
'format'=>['decimal',2]
],
[
'label' => 'GST%',
'attribute' =>'gstpercent',
//'headerOptions' => ['style' => 'width:6%'],
//'contentOptions' => ['class' => 'col-lg-1'],
'format'=>['decimal',0]
],
[
'label' => 'Total',
'attribute' =>'totalamount',
'headerOptions' => ['style' => 'width:9%'],
//'contentOptions' => ['class' => 'col-lg-1'],
'format'=>['decimal',2]
],
],
]); ?>
Gridview looks like -
Please let me know how to specify the font in gridview.
you could use options for the grid container
<?= GridView::widget([
'dataProvider' => $dataProvider1,
//'filterModel' => $searchModel,
'layout'=>"{items}",
'options' => ['style' => 'font-size:12px;']
'columns' => [
or directly in column
<?= GridView::widget([
'dataProvider' => $dataProvider1,
//'filterModel' => $searchModel,
'layout'=>"{items}",
'options' => ['style' => 'font-size:12px;']
'columns' => [
[
'label' => 'your_label',
'attribute' =>'your_attribute',
'contentOptions' => ['style' => 'font-size:12px;']
]

How to remove summary in yii2 grid but keeping pager

I'm using following code which is generated by the Gii tool while creting CRUD operation
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'text:ntext',
'status',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
You can use layout :
<?= GridView::widget([
'dataProvider' => $dataProvider,
'layout' => "{items}\n{pager}",
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'text:ntext',
'status',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Add "summary" key and pass it blank.
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'summary'=>'',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'text:ntext',
'status',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>

Undefined variable: gridColumns in karik gridview:yii2

I'm trying to use kartik gridview and got the following error. -
Undefined variable: gridColumns
I've checked - Yii2: Kartik Gridview sum of a column in footer. But didn't find the issue in my code.
Code of index.php -
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
if you assign manually the columns you don't need a var iwth columns specification so remove it
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>

How to add a css class to <td> tags in a kv- GridView class in Yii2

I'm Using Kartik's GridView Widget,
GridView::widget([
'id'=>'crud-datatable',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'contentOptions' => ['class' => 'form-control punjabi'],
'headerOptions' => ['class' => 'text-center'],
'columns' => require(__DIR__.'/_columns.php'),
])
In yii\grid\GridView class, It can be done with the following options :
[
'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center']
],
But using the above options in Kartik's GridView Class generates error: Undefined Property contentoptions.
How to go About this?
contentOptions is an attribute of yii\grid\Column not of yii\grid\GridView. The classes need to be applied to the columns i.e.
GridView::widget([
... //Other options here
'columns' => [
[
'attribute' => 'name',
'contentOptions' => ['class' => 'form-control punjabi'],
'headerOptions' => ['class' => 'text-center'],
]
]
]
If you would like to apply the same style to all the cells you can create your own column class, set defaults for contentOptions and headerOptions, and use that class instead of yii\grid\DataColumn.