This is my grid view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'toolbar'=> [
['content'=>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['create'],['class'=>'btn btn-shadow btn-success'])
],
'{toggleData}',
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'Name',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'attribute'=>'Age',
'vAlign'=>GridView::ALIGN_MIDDLE,
'value'=>'Age',
'width'=>'5%',
'hAlign'=>'center',
],
[
'attribute'=>'Address',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
'vAlign'=>GridView::ALIGN_MIDDLE,
'hAlign'=>GridView::ALIGN_CENTER,
'width'=>'15%',
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'options' => ['placeholder' => 'Search Status...'],
'pluginOptions' => [
'allowClear' => true
],
],
'value'=>function($model){
return ($model->Status=='0'?'Pending':($model->Status=='1'?'Accepted':'Rejected'));},
'filter'=>['Pending','Diterima'],
'label' => 'Status',
'refreshGrid'=>true,
'editableOptions'=>[
'format' => Editable::FORMAT_BUTTON,
'asPopover' => true,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>['0'=>'Pending', '1'=>'Accepted', '2'=>'Rejected'],
'options' => ['class'=>'form-control'],
]
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '
<li>{view}</li>
<li>{update}</li>
<li>{delete}</li>',
'dropdown'=>true,
'dropdownOptions'=>['class'=>'pull-right'],
'headerOptions'=>['class'=>'kartik-sheet-style'],
],
],
'panel'=>[
'type'=>GridView::TYPE_DEFAULT,
]
]); ?>
Help me with the Status attribute. The result from this gridview is like this :
what i want is like this :
So, it's like when the Status is Pending, the editableOption is on.But when the Status is Accepted or Rejected, the editableOption is disable. Is that possible ?
You need Change value option of Status Attribute.
Change in value option of Status Attribute.
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
// ------- your code ------------
'format'=>'raw',
'value'=>function($model){
if($model->Status=='0')
{
// if status =0 than create button and append with string
$btn='<button type="button" id='.$model->id.' class="btn btn-default ><i class="fa fa-pencil-square-o"></i></button>';
return 'Pending'.$btn;
}
else
{
return($model->Status=='1'?'Accepted':'Rejected');
}
},
// ------- your code ------------
],
There are multiple ways to do what you have asked, if you have used editableOptions, then
For Editable::INPUT_DROPDOWN_LIST type -> use 'editableButtonOptions' => [ 'disabled' => $data->your_status_variable_condition],
For any other type add readonly property
'readonly' => function($data){
return $data->your_status_variable_condition;
},
I hope it helps someone who is looking for the same issue
Related
I have a kartik\GridView in the view,
GridView
Actually, added a dropDownList on the toolbar
GridView with dropdownlist
But I need the dropDownList as a filter option for status [solicitudes_status], how can I achieve that?. Thank you.
$statusAll = TblEstatus::find()->all();
$statusArray = ArrayHelper::map($statusAll, 'estatus_id', 'estatus_descripcion');
$searchModel = new TblSolicitudesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'solicitudes_solicitud',
'solicitudes_dependencia_destino_id',
'solicitudes_dependencia_tipoapoyo_id',
'solicitudes_status',
[
'attribute' => 'solicitudes_status',
'filter' => [ 1=>"Nuevos", 2=>"Atendiendo", 3=>"Terminados" ]
],
['class' => 'yii\grid\ActionColumn'],
],
'toolbar' => [
['content'=>
Html::dropDownList('status_list_drop', 'null', $statusArray, ['data-pjax'=>0, 'class' => 'form-control']),
],
],
'pjax' => true,
'bordered' => true,
'striped' => false,
'condensed' => false,
'responsive' => true,
'hover' => true,
'floatHeader' => false,
'showPageSummary' => false,
'panel' => [
'type' => GridView::TYPE_DEFAULT
],
]); ?>
You can try to use activeDropDownList to perform filtering:
Html::activeDropDownList($searchModel, 'attribute_name',
ArrayHelper::map(ModelName::find()->asArray()->all(), 'ID', 'Name'),
['class'=>'form-control','prompt' => 'Select status']),
Change values according to your attributes.
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',
....
In Yii2 we have GridView like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'layout' => "{items}\n{summary}\n{pager}",
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
'id',
'size',
'program' => [
'label' => 'Program',
'value' => function($data)
{
return Html::a($data->program, ($data->program), ['target' => '_blank']);
},
'format' => 'raw',
],
'version',
'platform',
'license',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Is it possible to do to hide/show column, if we click, for example on button "Hide platform", then for show "Show platform", or maybe checkbox.
I cannot understand how to do this, help me please
You can do something like this:
- Name the column you want to handle, e.g. an ID
[
'class' => 'yii\grid\SerialColumn',
'options' => [ 'id' => 'serial-column' ],
'width' => '1%',
'vAlign' => 'middle',
'hAlign' => 'right',
]
Then you modify css to have that column disappeared at the beginning
#serial-column {display: none}
Then you apply js for a checkbox to make it appear:
jQuery('#some-chkbox').click(function(){
jQuery('#serial-column').toggle();
})
Yes, you can hide and show column conditionally using "Visible" Attribute.
[
'attribute' => 'email',
'label' => 'Email',
'visible' => ($_GET['type']) == 'b') ? true : false,
],
I believe this is what you are looking for.
In short - you can add custom links and script to toggle columns of the gridview table.
I am trying to create a column that displays a glyphicon. The glyphicon will link to an url which allows the user to download a file. Any help would be massively appreciated. Current code is as follows:
GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => 'common\widgets\Pagination',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Date',
'attribute' => 'call_datetime',
'format' => 'date',
],
[
'label' => 'Time',
'attribute' => 'call_datetime',
'format' => 'time',
],
'call_from',
'call_to',
'duration',
'call_type',
'extension',
[
'label' => 'File',
'attribute' => 'fname',
'value' => 'callRecFiles.fname',
],
It is the last attribute 'fname' that the user will be downloading.
Change your fname field array to:
[
'label' => 'File',
'attribute' => 'fname',
'value' => function($model) {
//here create glyphicon with URL pointing to your action where you can download file, something like
return $model->callRecFiles ? Html::a('Download', ['myController/download-action', 'fname' => $model->callRecFiles->fname]) : null;
}
],
And prepare proper action to allow user to download file.
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