Yii - 当我想调用update()函数时,jQuery.fn.yiiGridView抛出未定义的错误

时间:2014-08-13 21:36:45

标签: javascript jquery ajax gridview yii

在我看来,我有以下小部件:

$this->widget('bootstrap.widgets.TbGridView', array(
    'dataProvider' => $model->search(),
    'filter' => $model,
    'ajaxUpdate' => true,
    'afterAjaxUpdate' => "updateChild",
     /* When a user makes a selection, my js function 'updateChild' updates the
        drill-down view with the details of the selection */
    'selectionChanged' => "updateChild",
    'columns' => array(
    'firstName', 'lastName'
    )
));

它是Yii中CGridView的扩展,但是给出了Bootstrap外观。

dataProvider提供一个列表来填充网格(有问题的表包含有关人员的详细信息)。作为页面的一部分,可以创建新人。关于这种情况(以及删除),我打算用新条目更新GridView。当用户按下“提交”时会触发此AJAX事件:

    //prevent the form from submitting in the traditional manner
    e.preventDefault();
    var jqxhr = $.ajax( {
        type: 'POST',
        url: 'person/create',
        data: { Person: $('#person-form').serialize() },
    })
    .done(function(data) {
        // Update the drill-down view with the newly submitted details
        $('#updateData').html(data);
        // Attempt to update the gridView by ID - throwing error
        jQuery.fn.yiiGridView.update('yw0');
        //alert( 'success' );
    })
    .fail(function() {
       // alert( 'error' );
    })

它向相关的控制器方法发送AJAX POST请求,并从表单中插入数据。完成后,将使用新创建的条目的详细信息更新向下钻取视图。然后 - 这是有问题的行 - 调用yiiGridView的update()方法,并将我的GridView的id作为参数传入。它抛出错误:

未捕获的TypeError:无法读取未定义的属性'update'

我还在'updateChild'中尝试了相同的代码行,在我的GridView上调用选择更改时调用了js函数(参见代码),这样可以完全正常工作。我怀疑它在该函数中工作正常,因为它知道它正在执行的上下文。但是,我需要在上面定义的单独函数中使用它。

有人知道这里发生了什么吗?感谢。

编辑:回应Jagsler的评论(代码有点乱,它会在部署之前被清除):

public function actionCreate() {
    $model = new Person;

    // When form submit button is pressed
    if (isset($_POST['Person'])) {
        $params = array(); 
        parse_str($_POST['Person'], $params); // Parsing JSON object back to PHP array
        $model->attributes = $params['Person']; // Massive assignment to model from JSON parsed array
        if ($model->validate()) {
            $command = Yii::app()->db->createCommand();
            $command->insert('person', // $model->save() didn't work, threw that memory leak error we saw before
                array( 'Title' => $model->Title // So we had to resort to the good old fashioned way
                     , 'firstName' => $model->firstName
                     , 'middleName' => $model->middleName
                     , 'lastName' => $model->lastName
                     , 'DOB' => $model->DOB
                     , 'Address1' => $model->Address1
                     , 'Address2' => $model->Address2
                     , 'Address3' => $model->Address3
                     , 'City' => $model->City
                     , 'ZIP' => $model->ZIP
                     , 'State' => $model->State
                     , 'Occupation' => $model->Occupation
                     , 'homePhone' => $model->homePhone
                     , 'cellPhone' => $model->cellPhone
                     , 'workPhone' => $model->workPhone
                     , 'homeEmail' => $model->homeEmail
                     , 'workEmail' => $model->workEmail
                     , 'memberStatus' => $model->memberStatus
                     , 'dateJoined' => $model->dateJoined
                     , 'Gender' => $model->Gender
                     , 'maritalStatus' => $model->maritalStatus
                     , 'Notes' => $model->Notes
                     , 'Active' => $model->Active,));
            /* To my knowledge, there's no decent way to get back a full 
             * Person model after inserting to DB, so I had to get the model
             * by selecting the row with the latest dateCreated stamp */
            $Details = Person::model()->findBySql("SELECT * FROM person "
                    . "ORDER BY dateCreated DESC "
                    . "LIMIT 1;");
            /* Why couldn't I just put through $model? Good question, all I 
             * know is that it didn't work */
            $contributions = array();
            $this->renderPartial('_viewAjax', array(
            'Details' => $Details,
            'contributions' => $contributions,
            'createSuccess' => true,
                ), false, true);
        } else {
            echo "failure";
        }
        die();
    }

    // Code for showing the entry form
    if (isset($_POST['create'])) {
        $model = new Person();
        $this->renderPartial('_form', array(
            'model' => $model
                ), false, true);
        die();
    }
}

2 个答案:

答案 0 :(得分:2)

我想在您jquery中拨打renderPartial时会重新加载actionCreate()。尝试将调用参数更改为

$this->renderPartial('_form', array(...), false, false);

或者将以下代码添加到_form以防止重新加载jquery(以及其他javascript文件)。

<?php
if (Yii::app()->request->isAjaxRequest) {
    $cs = Yii::app()->clientScript;
    $cs->scriptMap['jquery.js'] = false;
    $cs->scriptMap['jquery.min.js'] = false;
}
?>

当重新加载jquery时,它会忘记之前知道的一切。所以yiiListView不再存在了。

答案 1 :(得分:0)

找到了这个答案,解决方案对我有用:

将此代码放入&#34; components&#34;:

下的主配置文件中
'clientScript' => array(
        'scriptMap' => array(
            'jquery.js' => '/path/to/your/jquery/file.js',
        ),
    ),

并将其添加到主视图文件的头部(view / layouts / main):

<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

这解决了这个问题。