Yii - 在此视图中包含另一个视图的方法

2022-01-04 00:00:00 php html yii

我的布局页面:

<身体><div id="容器"><ul id="列表"></ul>

<input id="update" value="update"/>

其中 ul 列表是一个部分,当

  1. 整个页面被加载
  2. 当更新按钮被点击时,会发出一个ajax请求,容器层的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() 或其他东西进行调用.

我所做的第三件事是在控制器中创建一个函数(不是 action,只是一个常规的公共方法),它返回 renderPartial 的输出(return $this->renderPartial('/model/_list',array(), true) 我认为).然后在初始加载的视图中我回显该函数,并在 ajax 操作中我在调用 Yii::app()->end(); 之前回显它.

当您要在整个站点上呈现一些部分代码时,即使在来自不同控制器的页面上,小部件也很有用.因此,如果该列表位于整个站点的侧边栏中,最好从中制作一个小部件,否则我将只使用您正在使用的模型的控制器.您对小部件执行的操作与对常规视图执行的操作相同 - 在 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!

相关文章