Mich90
(Michaelrichter1990)
July 7, 2016, 8:33am
#1
Hi everyone!
I am new to Yii and have a problem:
I want to display the index and view page of a model in the index page of my application.
For the first test, I changed my SiteController actionIndex to:
public function actionIndex()
{
$kunden = new ActiveDataProvider([
'query' => Kunde::find(),
]);
$newQuery = clone $kunden->query;
$kunde = $newQuery->limit(1)->one();
return $this->render('index', [
'kunden' => $kunden,
'kunde' => $kunde,
]);
}
and made my index look like:
<?= $this->render('//kunde/index',array('dataProvider' => $kunden));?>
<hr>
<?= $this->render('//kunde/view',array('model' => $kunde));?>
But this just gives me the possibility to show the first dataset in the view - page.
My question:
How is it possible to select the dataset in my kunde/index in a way to select the model which is shown in the kunde/view?
Thank you in advance!
patrickm
(Yiiframework)
July 7, 2016, 11:56am
#2
What exactly do you want to achieve?
When a record is selected in the grid, you want to show it’s details below the grid?
patrickm
(Yiiframework)
July 7, 2016, 2:38pm
#4
Do you want to use Ajax or a page reload?
Mich90
(Michaelrichter1990)
July 8, 2016, 7:04am
#5
Aparently, I don’t really know the consequenses of both of the solutions. But as I didn’t do anything with Ajax, I would suggest to go with the reload.
patrickm
(Yiiframework)
July 8, 2016, 7:37am
#6
Right then, you’ll need a couple of steps. First, your action needs to know, which record to show in detail:
public function actionIndex($idDetail)
{
$kunden = new ActiveDataProvider([
'query' => Kunde::find(),
]);
$kunde = Kunde::findOne($idDetail);
return $this->render('index', [
'kunden' => $kunden,
'kunde' => $kunde,
]);
}
Now you always need to access index with the parameter "idDetail" given. You can probably figure out how to set a default value.
Once you got that working, see if you can add links to your grid.
Mich90
(Michaelrichter1990)
July 8, 2016, 8:27am
#7
Right then, you’ll need a couple of steps. First, your action needs to know, which record to show in detail:
[…]
Once you got that working, see if you can add links to your grid.
Wow, that works great, thank you!
I got one follow up thing:
In the SiteController actionIndex I set the standard parameter to an existing one at the moment.
public function actionIndex($id='260000002')
{
...
}
How can I get the first one of Kunde of the query as standard?
patrickm
(Yiiframework)
July 8, 2016, 8:47am
#8
$query = Kunde::find();
$kunden = new ActiveDataProvider([
'query' => $query,
]);
$kunde = $query->one();
Mich90
(Michaelrichter1990)
July 8, 2016, 1:18pm
#9
Thanks! That works great!
softark
(Softark)
July 9, 2016, 2:07am
#10
I have written a wiki called "A Single Page with a List and a Detail".
It describes how to do it in an ajax way. Please take a look at it.
http://www.yiiframework.com/wiki/845/a-single-page-with-a-list-and-a-detail/
Patrick
Would you please review it and correct any errors?