0

I'm having problems with CGridView and one custom Formatter that uses javascript. When CGridView triggers an ajax request of any kind, my Formatter that works with javascript stops working.

Let's try one trivial example:

The Formatter

class DFormatter extends CFormatter {

    public function formatTest(){
        $js      = <<<EOD
        console.log("test");
        EOD;

        $cs = Yii::app()->getClientScript();
        $cs->registerScript("testjs", $js);

        return false;
    }
}

The view:

<?php $this->widget('zii.widgets.grid.CGridView',
  array(
       'id'           => 'development-grid',
       'dataProvider' => $model->search(),
       'filter'       => $model,
       'columns'      => array(
         'id',
         array(
           'name'  => 'testField',
           'type'  => 'test',
         ),
         array(
           'class' => 'CButtonColumn',
         ),
       ),
  )); ?>

After the first Ajax Request, the javascript code used in the formatter stops working, How can I get my javascript code works after every ajax call made by CGridView Widget?

Thanks.

4

1 回答 1

1

您不应该将脚本放入格式化程序中。

消除:

    $js      = <<<EOD
    console.log("test");
    EOD;

    $cs = Yii::app()->getClientScript();
    $cs->registerScript("testjs", $js);

配置您的网格视图:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    //.. other options

    // ADD THIS
    'afterAjaxUpdate' => 'function() { afterAjaxUpdate(); }',

添加<head>/layouts/main.php

<script src="<?= Yii::app()->getBaseUrl(true); ?>/js/myCustomJS.js"></script>

在/js/myCustomJS.js创建新的 JS 文件:

function afterAjaxUpdate() {
    // do your formatting here
}
于 2013-07-30T08:18:14.500 回答