是否有人知道在 CGridView 中设置默认函数的可能性,该函数将在每次 AJAX 更新后在页面上的所有网格上运行?我在很多页面上都使用了 CGridView,我不想为每个网格单独指定这个函数。我需要这个,因为我使用jQuery 选择菜单作为过滤器下拉菜单,并且在 AJAX 重新加载后,它们需要再次初始化。
'afterAjaxUpdate' => "function(id,data){$('select').selectmenu()}";
值的格式见http://www.yiiframework.com/doc/api/1.1/CGridView#afterAjaxUpdate-detail
在收到成功的 AJAX 响应后将调用的 javascript 函数。函数签名是 function(id, data) 其中“id”是指网格视图的 ID,“data”是接收到的 ajax 响应数据。
你需要设置
'afterAjaxUpdate' => "function(id,data){$('select').selectmenu()}";
更新:
除了创建子级CGridView
并设置$afterAjaxUpdate
. 这是代码:
class GridView extends CGridView{ public $afterAjaxUpdate = "function(id,data){$('select').selectmenu()}"; }
更新:
我查看了小部件的源代码和afterAjaxUpdate
仅在方法中使用的属性registerClientScript
。这就是为什么我提出另一种解决方案。首先 - 您可以更改afterAjaxUpdate
继承类的 init 值:
public function init(){ parent::init(); // after setting all values reset value to desire $this->afterAjaxUpdate = "function(id,data){$('select').selectmenu()}"; }
其次 - 您可以在调用方法之前重新更改它的属性registerClientScript
:
public function registerClientScript(){ $this->afterAjaxUpdate = "function(id,data){$('select').selectmenu()}"; parent::registerClientScript(); }
经过更多研究,我发现,确实不可能设置默认的 afterAjaxUpdate 函数,该函数会自动在所有网格视图上触发。
然而,有 2 种可能性,可以使用:
请参阅CreatoR的答案- 在扩展的 GridView 对象中设置默认事件。只要没有人在特定网格中手动设置 afterAjaxUpdate,它就可以工作。
您可以更改(但这不是一个真正好的“扩展”方式)jquery.yiigridview.js文件以满足此特定需求,方法是添加一个新的 option=function, sth like '***afterAjaxUpdateDefault* '**,然后放置它在调用“ afterAjaxUpdate ”之前或之后。