下面我有一个ajax标签示例:
$this->widget('zii.widgets.jui.CJuiTabs', array(
'tabs' => array(
'StaticTab 1' => 'Content for tab 1',
'StaticTab 2' => array('content' => 'Content for tab 2', 'id' => 'tab2'),
// panel 3 contains the content rendered by a partial view
'AjaxTab' => array('ajax' => $this->createUrl('/AjaxModule/ajax/reqTest01')),
),
// additional javascript options for the tabs plugin
'options' => array(
'collapsible' => true,
),
));
但我不知道/ AjaxModule / ajax / reqTest01会发生什么。 这个例子中有一个缺失的部分,即渲染视图,我不知道如何设计它以便ajax调用工作。感谢。
答案 0 :(得分:3)
根据你提出的代码,特别是这一行:
'AjaxTab' => array('ajax' => $this->createUrl('/AjaxModule/ajax/reqTest01')),
我们知道我们需要ajaxmodule,ajax控制器,reqtest01动作,所以请执行以下步骤:
创建module,它必须命名为 AjaxModule 。
在名为 Ajax 的 AjaxModule 中创建controller。
在此 Ajax 控制器中创建action,名为 ReqTest01 。
在此操作中,您可以直接echo
html或使用renderPartial()
,为ajax部分呈现视图文件。
所以您的控制器 Ajax ,其中的操作看起来有点像
<?php
class AjaxController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionReqTest01(){
// directly echoing output is hardly of any use, like echo "Directly echoing this";
$this->renderPartial('rendpar_ajax'); // renderPartial is way better as we have a view file rendpar_ajax.php that we can manipulate easily
}
}
现在我们可以编写 rendpar_ajax.php 视图文件,在AjaxModule模块的ajaxController控制器的views文件夹中创建此文件。
<?php
// rendpar_ajax.php file for ajax tab
// have any code here, use widgets, form, html helper etc
echo "<h1>AjaxModule--AjaxController--actionReqTest01</h1>";
echo "<p>This view is partially rendered</p>";
详细了解如何创建模块,控制器,操作及其使用方式,以及yii目录层次结构的工作原理 祝你好运!
修改:请注意,对于视图,我们还可以传递数据提供程序以获取复杂的动态视图。