Beginner - Display index and view in one page

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!

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?

Exactly!

Do you want to use Ajax or a page reload?

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.

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.

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?




        $query = Kunde::find();

        $kunden = new ActiveDataProvider([

            'query' => $query,

        ]);

        

        $kunde = $query->one();        



Thanks! That works great!

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?