gpt4 book ai didi

javascript - Yii2,Javascript 只能在模态中工作

转载 作者:行者123 更新时间:2023-12-02 14:08:35 27 4
gpt4 key购买 nike

我正在为我的 crud 表单使用模态 Bootstrap ,并且还使用 yii2-ajaxcrud 扩展,该扩展扩展了单页 crud 的 gii 生成器。

一切都工作正常,保存模型、验证等......但是,我正在尝试使用 javascript 插件:(Leafo 的 Sticky-kit)将我的操作按钮粘贴在顶部。当我在模式中访问“创建” View 时,会发生以下错误:

"Uncaught TypeError: $ (...) stick_in_parent is not a function."

从索引 View 中,我访问“创建” View 。当模式打开时,我在 chrome 检查器中看到 jquery 和 Stick 插件的注入(inject)。此外,通过 ajax crud 扩展,我还可以访问直接 URL 生成的 View ,因此 Stick 插件可以工作。只有在模式中访问 View 才会给出错误。

可能是什么?下面是我的主要相关代码。

//namespace app\modules\crm\controllers
public function actionCreate()
{
$request = Yii::$app->request;
$model = new Classes();

if($request->isAjax){
/*
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
if($request->isGet){
return [
'title'=> "Create new Classes",
'content'=>$this->renderAjax('create', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])

];
}else if($model->load($request->post()) && $model->save()){
return [
'forceReload'=>$this->htmlTableContainerId ,
'title'=> "Create new Classes",
'content'=>'<span class="text-success">Create Classes success</span>',
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::a('Create More',['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])

];
}else{
return [
'title'=> "Create new Classes",
'content'=>$this->renderAjax('create', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])

];
}
}else{
/*
* Process for non-ajax request
*/
if ($model->load($request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ID]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

}

景色

// create view
<div class="panel-body" style="height: 600px">
<div class="classes-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

</div>

$this->registerJsFile('http://leafo.net/sticky-kit/src/sticky-kit.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

$this->registerJs("$('.panel-heading').stick_in_parent();");

编辑

当模式打开时, View 中的实际 html 生成。我尝试删除 jquery 的重复包含(在模态和索引中),我得到了它(使用 Yii::$app->assetManager->bundles = ['yii\web\JqueryAsset' => false,],... )但错误仍然存​​在,所以我保留它。

<html lang="pt-BR">
<body>
// omitted content, body content of index view, with gridview etc.
<div id="ajaxCrudModal" class="fade modal in" role="dialog" tabindex="-1" style="display: block;">
<div class="modal-dialog ">
<div class="modal-content">
<div class="modal-body"><div class="panel">
<div class="panel-body" style="height: 600px">
// omitted content
</div>
</div>
<script src="/yii2/basic/assets/32e507bc/jquery.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.validation.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.activeForm.js"></script>
<script src="http://leafo.net/sticky-kit/src/sticky-kit.js"></script>
<script type="text/javascript">
//$('body').on('load', function(){
$('.panel-heading').stick_in_parent();
//});
</script>
</div>
<div class="modal-footer"></div>
</div>
</div>
</div>

<script src="/yii2/basic/assets/32e507bc/jquery.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.js"></script>
<script src="/yii2/basic/assets/fb64637f/js/bootstrap.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.validation.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.activeForm.js"></script>
<script src="/yii2/basic/assets/28255cf7/dist/js/bootstrap-dialog.js"></script>
<script src="/yii2/basic/assets/9f23c46b/yii.gridView.js"></script>
<script src="/yii2/basic/assets/75fbd502/js/kv-grid-action.js"></script>
<script src="/yii2/basic/assets/8d08c7cb/jquery.pjax.js"></script>
</body></html>

最佳答案

您应该使用 renderAjax 而不是 renderPartial,因为 renderPartial 不加载 jquery 库。

关于javascript - Yii2,Javascript 只能在模态中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39839104/

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