gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:05:18 25 4
gpt4 key购买 nike

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

$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 请求发送到相关的 Controller 方法,并插入表单中的数据。完成后,深入查看将使用新创建条目的详细信息进行更新。然后 - 这是有问题的行 - yiiGridView 的 update() 方法被调用,并将我的 GridView 的 id 作为参数传入。它抛出错误:

未捕获类型错误:无法读取未定义的属性“更新”

更让事情变得困惑的是,我在 GridView 上的选择更改时调用的 js 函数“updateChild”中尝试了同一行代码(请参阅代码),并且它工作得很好。我怀疑它在该函数中工作得很好,因为它知道它正在执行的上下文。但是,我需要它在我上面定义的单独函数中工作。

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

编辑:回应 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();
}
}

最佳答案

我认为当您在 actionCreate() 中调用 renderPartial 时,jquery 会重新加载。尝试将调用的参数更改为

$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 已经不存在了。

关于javascript - Yii - 当我想调用 update() 函数时,jQuery.fn.yiiGridView 抛出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com