Changes
Title
unchanged
A Single Page with a List and a Detail
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
gridview, detail, ajax
Content
changed
The CRUD generator of Gii has done a wonderful job for you, and you already have a list of items in
the "index" page and a detailed view of a specified item in
the "view" page.
But you may want to show both
of themthe index and the view in a single page
, in which. That way you can click on an item in the grid to show its detail
in the same page on the fly,s without having to go to another
view page.
Is it possible with Yii? Yes, it is
.!
Let's start with a review of the current code.[...]
## Original Index Page
For example, assume that the following is an "index" page that Gii has generated for the Post model for you.[...]
You can see "date" and "title" in the grid, but you have to go to a "view" page to see other attributes like "body" and "created_by".
Now we want to show all the attributes of the selected post in the same page when you click on an row in the grid.
## Sub View for Detail
The first thing we do is to create a sub
-view that shows all the attributes of a post. We will
name it ascall it "_view.php".[...]
```
It uses a DetailView to show all the attributes of a post.
As you may notice, this is almost the sameidentical with "view.php"
, that Gii has generated for you. You can make it very easily with copy-and-pasting.
Now we
callinclude this sub
-view
fromin the "index.php"
view scriptpage.[...]
The sub view is located after the grid. You may locate it before the grid if you prefer. But the important point is that the sub view should be outside of the Pjax area.
You may be aware that we have to supply an additional parameter called "post" to the index view, since the sub -view need
s it to work as expected. We will modify the "index" action next.
## Modifying Index Action
The original controller code
goelooks like this:[...]
## Ajax Updating
Now, here comes athe magic of ajax updating. Insert the following lines in "index.php":[...]
You may want to do something more to improve your page. The first thing you may want to do might be supplying more appropriate initial display of the selected post. It's up to you.
Have fun.!