Yii - Ways to do include another view in this view
My layout page:
<html>
<body>
<div id="container">
<ul id="list"></ul>
</div>
<input id="update" value="update" />
</body>
</html>
in which the ul list is a partial which will be rendered either when
- the whole page gets loaded
- the when the update button gets clicked, an ajax reqeust is issued and the innerHTML of the container layer will be updated with the response ul list
I'm new to Yii and am not sure how to reuse the ul partial 开发者_运维技巧both in both cases, I googled a bit and widgets seems to be the solution, not sure tho. Any ideas?
Thanks.
If I understand your question right, you can do a few things...
(I am assuming that you have a partial view file like views/model/_list.php.)
You can either renderPartial('/model/_list') on the initial page load, and then in your AJAX action just call renderPartial('/model/_list') again.
Or you can just have the AJAX action (containing renderPartial('/model/_list')), and call it on page load with jQuery instead of rendering it in the view (just call Yii::app()->getClientScript()->registerScript in your view where you want to load it, and use $.ajax() or something to make the call).
A third thing that I have done is make a function in the controller (not an action, just a regular public method) that returns the output of renderPartial (return $this->renderPartial('/model/_list',array(), true) I think). Then in view on the initial load I echo that function, and in the ajax action I echo it before calling Yii::app()->end();.
Widgets are useful when you will be rendering a bit of partial code all over the site, even on pages from different controllers. So if that list is in a sidebar all over the site it might be better to make a widget out of it, otherwise I would just use the controller of the model you are working with. What you would do with the widget is the same as you would with the regular view - either build the data in the Widget and render the partialView, or call it via AJAX. What you would probably do for the AJAX is POST to the controller you are getting the data from, so you will still need the AJAX action. The widget will just make it easier to drop it different places around the site.
Good luck!
精彩评论