Yii-当我想调用update()函数时,jQuery.fn.yiiGridView引发未定义的错误 [英] Yii - jQuery.fn.yiiGridView throwing undefined error when I want to call update() function

查看:147
本文介绍了Yii-当我想调用update()函数时,jQuery.fn.yiiGridView引发未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my view I have the following widget:

$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外观.

It is an extension of CGridView in Yii, but given the Bootstrap look.

dataProvider提供一个列表来填充网格(所涉及的表包含有关人员的详细信息).作为页面的一部分,可以创建一个新人.在这种情况下(以及删除时),我的意图是使用新条目更新GridView.当用户按下提交"时,将触发此AJAX事件:

The dataProvider gives a list to populate the grid (the table in question contains details about people). As part of the page, a new person can be created. On this happening (and also on deletion), it is my intent to update the GridView with the new entry. This AJAX event is triggered when the user presses 'submit':

    //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作为参数.它引发错误:

It sends an AJAX POST request to the relevant controller method, and inserts the data from the form. On done, the drill-down view will be updated with the details of the newly created entry. Then - and this is the problematic line - the update() method for yiiGridView is called with the id of my GridView passed in as the parameter. It throws the error:

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

Uncaught TypeError: Cannot read property 'update' of undefined

让我感到困惑的是,我在GridView的选择更改(请参见代码)上调用了js函数"updateChild"中尝试了同一行代码,并且工作得很好.我怀疑它在该功能中可以正常工作,因为它知道执行该操作的上下文.但是,我需要它才能在上面定义的单独函数中起作用.

What further muddies the waters is that I have tried the same line of code within 'updateChild' the js function that is called on a selection change on my GridView (see the code), and it works perfectly fine. My suspicion is that it works fine in that function because it is aware of the context in which it is being performed. However, I need this to work in the separate function I defined above.

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

Does anybody have any idea what's going on here? Thanks.

响应Jagsler的评论(代码有点混乱,将在部署前将其清除):

In response to Jagsler's comment (code is a bit of a mess, it will be cleaned up before deployment):

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();
    }
}

推荐答案

我认为在actionCreate()中调用renderPartial时会重新加载jquery.尝试将呼叫参数更改为

I think jquery is reloaded when you call the renderPartial in your actionCreate(). Try changing the parameters of the call to

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

或将以下代码添加到_form中,以防止重新加载jquery(可能还有其他javascript文件).

Or add the following code to _form to prevent jquery (and maybe other javascript files) from being reloaded.

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

重新加载jquery时,它会忘记以前知道的所有内容.因此yiiListView不再存在.

When jquery is reloaded it forgets everything it knew before. So the yiiListView doesn't exist anymore.

这篇关于Yii-当我想调用update()函数时,jQuery.fn.yiiGridView引发未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆