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',
],
];
},
],
i want to display array in view form but i don't know how ?
can any one help me :
my controller code is :
$query = new Query;
$query
->select(['Day_Name'])
->from('days')
->join( 'INNER JOIN','doctorsworkday','doctorsworkday.Day_id =days.dayid');
$command = $query->createCommand();
$dataProvider = $command->queryAll();
$dataProvider= array($dataProvider);
return $this->render('view',
[
'model' => $this->findModel($id),
'days'=>$days,
'doctorsworkday'=>$doctorsworkday,
'dataProvider' => $dataProvider,
])
my view code is :
<?=
DetailView::widget([
'model' => $dataProvider,
'attributes' => [
'Day_Name'
],
])
?>
but its shows : (not set)
when i use vat_dump($dataProvider) there is an array .. but i don't know how to show it
enter Var_Dump
Well firstly you are using a DetailView for displaying multiple results. DetailView is used to display the detail of a single data model as said in the docs. You could use a GridView to display multiple results. GridView accepts a dataProvider not an array so you'll need to turn your array into a dataProvider.
fortunately you can use the class ArrayDataProvider to do just that:
$gridViewDataProvider = new \yii\data\ArrayDataProvider([
'allModels' => $dataProvider,
'sort' => [
'attributes' => ['Day_Name'],
],
'pagination' => ['pageSize' => 10]
]);
something like this should work.
Then you pass this $gridViewDataProvider to the gridview like this:
<?= GridView::widget([
'dataProvider' => $gridViewDataProvider,
'columns' => [
'name'
]
]) ?>
Also note that in your controller you wrapped your $dataProvider in an array with this line:
$dataProvider= array($dataProvider);
You should remove that line and then everything should work as far as I can tell.
I want to append a parameter to the auto generated parameters of filtermodel in GridView
How do I add additional parameters?
You can use 'filterUrl' property of GridView
In your GridView
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterUrl' => ['YOUR_CONTROLLER_ACTIONID_HERE','date' => date('Y-m-d')],
'columns' => [
],
]); ?>
first parameter to 'filterUrl' is your action id. After that you can give your additional url parameters as key value pair
I have two database tables 'user' and 'role'. I used Yii framework 2.0 Gii to create CRUD with User model and UserSearch model. By default, Gii uses GridView::widget for the index page for the 'user' model.
In the search($params) method inside of the UserSearch model, I used the following code to join the above tables together
$query = User::find()->with('role');
Everything works fine with the query.
By default Gii does not include the data from the joined table 'role' in the GridView::widget inside of the views/user/index.php page. With the join query above I could retrieve data from both tables. In the views/user/index.php page I have injected the GridView::widget with the following code so that it also includes the data and column names from the joined table (role).
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
'role.role_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Everything works fine with the role data 'role_name included in the GridView::widget. But the problem is that there is no search box for the role_name. The GridView::widget creates search box for the User properties only. Is there any way to add search box for the properties of the joined table 'role' because I also would like to search through 'role_name' as well as through other properties of the User model.
Try this way:
In your UserSearch model add
UserSearch extends ...
{
public $roleFilterInputName; //the name of the filter search input
//important
function rules()
{
//add roleFilterInputName as safe
return [
[['xxx', 'roleFilterInputName'], 'safe'], //!!!!
];
}
}
in your grid:
'columns':
[
//...
[
'attribute' => 'roleFilterInputName',
'value' => 'role.role_name'
],
//...
]
in UserSearch::search()
$query->andFilterWhere(['like', 'role.role_name', $this->roleFilterInputName])
But I guess you'll have to use 'joinWith' instead of 'with'.
Inside CGridView add below code. It will enable filter with dropDownList.
[
'attribute' => 'act_role_id',
'label' => 'Actor Role',
'value' => 'actRole.role_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\ActorRole::find()->orderBy('role_name')->asArray()->all(),'act_role_id','role_name')
],
CGridView code Snippet is as below:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
[
'attribute' => 'act_role_id',
'label' => 'Actor Role',
'value' => 'actRole.role_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\ActorRole::find()->orderBy('role_name')->asArray()->all(),'act_role_id','role_name')
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Try it with
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
//'role.role_name',
['attribute' => 'role', 'value' => 'role.role_name'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I just have tried it in my code, so I'm not sure if this works also with your code. But if so, I don't know why it has to be defined this way.
I guess the answer of rajesh ujade also includes this definition, however, for Yii 1.
This worked for me
Table = Lead (id , year_id)
Table = Year (id, text)
Added text in lead (index.php)
Year::find()->all() = This code pull all value from table/ all years.
[
'attribute'=> 'year_id',
'format' => 'html',
'value' => 'year.value',
'label' => 'Year',
'filter' => Html::activeDropDownList($searchModel, 'year', yii\helpers\ArrayHelper::map(Year::find()->all(), 'year_id', 'value'), ['class' => 'form-control', 'prompt' => '---']),
],
Also it shows dropdown as well sorting.
image Showing Dropdown in Grdiview
#Ekonoval is going in right way.
Just add following in serch function of UserSearch:
After initializing ActiveDataProvider object like this:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
]);
$dataProvider->sort->attributes['roleFilterInputName'] = [
'asc' => ['role.role_name' => SORT_ASC],
'desc' => ['role.role_name' => SORT_DESC]
];
You may write query in your UserSearch model like
if($this->role)
{
$query->join('LEFT JOIN','role','role.user_id = user.id')->andFilterWhere(['role.item_name' => $this->role]);
}
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view',
'ajaxUpdate' => true,
'pager' => array(
'class' => 'CLinkPager',
),
'template' => '{pager}{items}',
));
?>
after executing this the pager components are displaying as column format.is their any way to change that into row format?
I guess you getting this widget through renderPartial() method. When using the renderPartial() method controller not sending js and css files with response by default. If you want to get response with these files you should pass true in fourth parameter:
$this->renderPartial('myView',array(), false, true);