This question already has answers here:
Extend GridView ActionColumn with additional icon
(3 answers)
Closed 7 years ago.
I have following grid.
$gridColumns = [
'class'=>'kartik\grid\ActionColumn',
'headerOptions'=>['class'=>'kartik-sheet-style'],
'template' => '{delete}{my_button}',
],
echo GridView::widget([
'dataProvider'=> $dataProvider,
'columns' => $gridColumns,
'filterModel' => $filterModel,
],
]);
I want add to template a new action button for example {my_button}.
You should simply add a buttons param to your column, e.g. :
'template' => '{my_button}',
'buttons' => [
'my_button' => function ($url, $model, $key) {
return Html::a('My Action', ['my-action', 'id'=>$model->id]);
},
]
Read more : http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$buttons-detail
Related
I would like to display two Kartik's Gridviews (with editable columns) within one form.
The problem is, if i would like to edit a value in the second gridview, the editable popup opens for the coresponding row in the first gridview.
Looking into the html, the id of the editables are the same for the between the two gridviews.
Picture of the two gridviews; editable in second grid was clicked.
The definition of the two gridviews in the view:
echo GridView::widget([
'id' => 'your_gridview_one',
'dataProvider'=>$dataProvider,
'columns'=>$gridColumns,
//'filterModel' => $searchModel,
'showHeader' => true,
]);
echo GridView::widget([
'id' => 'your_gridview_two',
'dataProvider'=>$secondDataProvider,
'columns'=>$gridColumns,
//'filterModel' => $searchModel,
'showHeader' => true,
]);
How can i change the ids of the editables?
You can not use the same $gridColumns in both GridView. You have to give to the form (the editable popup) and the input field in it a unique HTML id. Something similar:
[
'class' => 'kartik\grid\EditableColumn',
'attribute' => 'name',
'editableOptions' => function ($model, $key, $index) {
return [
'formOptions' => [
'id' => 'gv1_' . $model->id . '_form_name',
'action' => \yii\helpers\Url::to(['recipe-lang/index'])
],
'options' => [
'id' => 'gv1_' . $model->id . '_name',
],
];
},
],
This question already has answers here:
URL in Yii2 GridView
(8 answers)
Closed 6 years ago.
An ajax request calls a below action whose response is JSON:
\Yii::$app->response->format = 'json';
if($userId){
$dataProvider = new ArrayDataProvider([
'allModels' => Templates::getTemplates($userId,'n'),
]);
$response = $this->renderAjax('index', ['dataProvider' => $dataProvider,]);
return ['status'=>true,'data'=>$response,'total'=>count($dataProvider)];
}
In View of this action, there is a GridView Widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return ''.Html::encode($data->template_name).'';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
But this encodes html value of template name. For Eg: test to test
and this renders at browser:
I do not need this encoding. Please help me to solve this.
you should use format => raw
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'format' => 'raw',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return ''.Html::encode($data->template_name).'';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Does anyone know how to implement a placeholder or tooltip on the Gridview filter of the Yii2 Framework? I need something that stands out to the user to let them know that textbox is in fact a search filter.
Look forward to hearing responses.
Placeholder could be realized with this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'name',
'filterInputOptions' => [
'class' => 'form-control',
'placeholder' => 'Type in some characters...'
]
],
['class' => 'yii\grid\ActionColumn' ],
],
]); ?>
class should be provided, though it is not a must - it is just the default styling class.
Setting this globally
The only way that I found is in config/web.php that is used for the application configuration:
$config = [
...
'on beforeRequest' => function ($event) {
Yii::$container->set('yii\grid\DataColumn', [
'filterInputOptions' => [
'class' => 'form-control',
'placeholder' => 'Type in some characters...'
]
]);
},
...
];
This is an event handler. On each request DataColumn will be configured to use the placeholder. Some detail information can be found here. Now you don't need to adjust any GridView configuration in order to have the placeholder. In the handler you can change other configurations as well, of course.
yon can also use the tooltip/title with filterOptions
[
'attribute' => 'name',
'label' => 'labelname',
...
....
'filterOptions' => [ 'title' => 'prova'],
],
I am using kartik grid view to display my data in yii 2 with pjax enabled. Every time, I search a data in the grid view, the search is done using ajax but the url keeps changing. Is there a way to keep the url unchanged? Please help me with the solution. Here is my code:
<?php use kartik\grid\GridView;?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'pjaxSettings'=>[
'neverTimeout'=>true,
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'hotel_id',
'name',
'address',
'phone_no',
'contact_person',
// 'email_address:email',
// 'website',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
You can disable pushState feature like this:
'pjax' => true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
],
],
This question already has answers here:
Changing value of an attribute in DetailView widget
(2 answers)
Closed 6 years ago.
I get an error when I use a function to get the value of an attribute and it's working normally using Gridview. What I'm doing wrong?
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
I have experienced this kind of problem. The error was
PHP Warning – yii\base\ErrorException
htmlspecialchars() expects parameter 1 to be string, object given
what I did was transfer the function in the model like this.
function functionName($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
and then in your view.php file..
[
'label'=>'subject_type',
'value'=>$model->functionName($data),
],
Just use call_user_func()
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => call_user_func(function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
}, $model),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
Fabrizio Caldarelli, on 05 January 2015 - 03:53 PM, said:
Yes because 'value' attribute is a real value or attribute name, as doc says
So your code should be:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => Lookup::item("SubjectType", $model->subject_type),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>