Yii - 在这个观点中,包括另一种观点 [英] Yii - Ways to do include another view in this view

查看:145
本文介绍了Yii - 在这个观点中,包括另一种观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局页面:

 < html> 
< body>
< div id =container>
< ul id =list>< / ul>
< / div>
< input id =updatevalue =update/>
< / body>
< / html>

其中ul列表是一个部分,它将在


  1. 整个页面被加载

  2. 当更新按钮被点击时,发出ajax reqeust并且容器的innerHTML图层将被更新为响应ul列表

我是Yii的新手,不确定如何重用ul部分两种情况下,我搜索了一下,小部件似乎是解决方案,并不确定。有任何想法吗?



谢谢。

解决方案

如果我理解你的问题,可以做一些事情......



(我假设你有一个部分视图文件,如views / model / _list.php。)



您可以在初始页面加载时使用renderPartial('/ model / _list'),然后在您的AJAX操作中再次调用renderPartial('/ model / _list')。



或者你可以使用AJAX动作(包含renderPartial('/ model / _list')),并在页面加载时用jQuery调用它,而不是在视图中渲染它(只需调用Yii :: app() - > getClientScript() - > registerScript在你想要加载它的视图中,并使用$ .ajax()或其他方式进行调用)。
$ b $我做的第三件事是在控制器中生成一个函数(不是 action ,只是一个常规的公共方法),它返回renderPartial的输出(返回$ this-> renderPartial( '/ model / _list',array(),true)我认为)。然后,在初始加载的视图中,我回显了该函数,并且在调用Yii :: app() - > end();之前,在ajax动作中回显它。



如果您将在整个站点上渲染一些部分代码,即使在来自不同控制器的页面上,小部件也很有用。因此,如果该列表位于整个站点的侧边栏中,那么最好将其制作出来,否则我只会使用您正在使用的模型的控制器。你对Widget的处理方式与常规视图相同 - 可以在Widget中生成数据并渲染partialView,或通过AJAX调用它。您可能会为AJAX做的事情是向您从中获取数据的控制器发布POST,所以您仍然需要AJAX操作。这个小部件会让它更容易放置在网站的不同位置。



祝你好运!


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

  1. the whole page gets loaded
  2. 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!

这篇关于Yii - 在这个观点中,包括另一种观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆