档案位置为:yiiroot/framework/widjets/assets/gridview/jquery.yiigridview.js 我需要在此文件中更改一行,以便我可以在我的要求中使用它。
//original code line 79,80
if (settings.ajaxUpdate.length > 0) {
$(document).on('click.yiiGridView', settings.updateSelector, function () {
//I need to change it to :
if (settings.ajaxUpdate.length > 0) {
$(this).parent().on('click.yiiGridView', settings.updateSelector, function () {
在不需要更改源代码文件的情况下覆盖它的正确方法是什么?
答案 0 :(得分:2)
将文件夹yiiroot/framework/zii/widgets/assets/gridview/
的全部内容复制到客户端可访问的位置,例如your_web_app_dir/gridview
并进行所需的修改。然后在gridview中添加baseScriptUrl
属性并将其设置为修改后的文件文件夹的路径,如下所示
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'model-grid',
'baseScriptUrl'=>Yii::app()->baseUrl.DIRECTORY_SEPARATOR.'gridview',
'dataProvider'=>$datatProvider,
'columns'=>array(
....
....
),
)); ?>
答案 1 :(得分:2)
执行此操作的另一种方法是使用jQuery's off
method。
使用off
,您可以删除已在 jquery.yiigridview.js 中添加的事件处理程序,然后使用on
添加新处理程序。
像这样的东西(在具有网格视图的视图中):
<?php
Yii::app()->clientScript->registerScript('myownhandlers',
"
$(document).off('click.yiiGridView', $.fn.yiiGridView.settings['your-grid-id'].updateSelector); // this will remove the jquery.yiigridview.js handler
// assign the new handler, instead of $(this) you have to use the id of the element you are interested in
$('#the-id-you-are-interested-in').parent().on('click.yiiGridView', settings.updateSelector, function () {
// your function's code, you'll have to write your function
});
",
CClientScript::POS_LOAD // this is important because the default is POS_READY, and the gridview is available only after load
);
?>
继续阅读the on
method,以便更好地理解代码。
注意:强>
请注意事件名称空间,即click.yiiGridView
是正确的,实际上在yii的版本中,我没有名称空间,只有click
,所以在覆盖之前检查一下。