CGridView小部件已经有视图,更新,检测选项.但我在我的基于jquery移动的项目中使用CListView小部件,但在创建删除选项的ajax链接时遇到问题.不知道如何在_view.php(视图文件)及其renderPartial()视图文件中创建一个ajax删除链接,以便在成功删除plz之后消失吧帮助提前感谢.这是用于编辑和删除的_view.php文件链接.
<?php
echo CHtml::link(CHtml::encode($data->id),
array('editmember1', 'id' => $data->id),
array('data-role' => 'button', 'data-icon' => 'star')
);
echo CHtml::link(CHtml::encode($data->id), $this->createUrl('customer/delete', array('id' => $data->id)),
array(
// for htmlOptions
'onclick' => ' {' . CHtml::ajax(array(
'beforeSend' => 'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}',
'success' => "js:function(html){ alert('removed'); }")) .
'return false;}', // returning false prevents the default navigation to another url on a new page
'class' => 'delete-icon',
'id' => 'x' . $data->id)
);
Run Code Online (Sandbox Code Playgroud)
?>
发生这种情况是因为:
未调用正确的操作,因为您尚未设置url属性jQuery.ajax().你应该知道Yii CHtml::ajax是建立在jQuery的ajax之上的.所以你可以添加:
CHtml::ajax(array(
...
'url'=>$this->createUrl('customer/delete', array('id' => $data->id,'ajax'=>'delete')),
...
))
Run Code Online (Sandbox Code Playgroud)
同样在url中我传递一个ajax参数,以便动作知道它是一个明确的ajax请求.
然后默认情况下控制器操作(即Gii生成的CRUD)期望请求为post类型,您可以在customer/delete操作行中看到:if(Yii::app()->request->isPostRequest){...}.所以你必须发送一个POST请求,再次修改ajax选项:
CHtml::ajax(array(
...
'type'=>'POST',
'url'=>'',// copy from point 1 above
...
))
Run Code Online (Sandbox Code Playgroud)或者你也可以使用CHtml::ajaxLink().
要在删除后更新CListView,请致电$.fn.yiiListView.update("id_of_the_listview");.就像是:
CHtml::ajax(array(
...
'type'=>'POST',
'url'=>'',// copy from point 1 above
'complete'=>'js:function(jqXHR, textStatus){$.fn.yiiListView.update("mylistview");}'
...
))
Run Code Online (Sandbox Code Playgroud)