gpt4 book ai didi

php - 通过 ajax 请求时 Yii2 CKeditor 未初始化

转载 作者:行者123 更新时间:2023-12-01 07:08:48 25 4
gpt4 key购买 nike

我正在尝试添加一个通过 AJAX 请求嵌入的 CKeditor 小部件。请求本身工作正常并返回我想要的一般部分 View 。除了 Ckeditor 小部件之外,都会返回一个普通的文本框。

当将项目添加到组中并重新加载页面时,将呈现相同的partialView(在包含所有组项​​目的foreach中),并且这次CKeditor很好地就位。

在下面发布了我的 Controller 、CKeditorScipt with AJAX 请求的初始化。 (CKeditor 包含在 _ContentItemHtml View 中)

我看过this ,但我无法从 JS 调用任何 CKeditor 函数,因为它是作为小部件加载的。

Controller 操作

public function actionCreateHtml($contentitemgroupid)
{
$model = new ContentItemHtml();
if (isset(Yii::$app->request->post()['ContentItemHtml'])) {
$item = Yii::$app->request->post()['ContentItemHtml'];
$model->contentitemgroupid = $contentitemgroupid;
$model->title = $item['title'];
$model->body = $item['body'];
$model->save();
// return $this->redirect(['edit', 'id' => $model->contentitemgroupid]);
}
else
return $this->renderPartial('_ContentItemHtml', ['model' => $model]);
}

View 中的事件表单:

echo $form->field($model, 'body')->widget(CKEditor::className(), [
'preset' => 'custom',
'clientOptions' => [
'height' => 200,
'toolbarGroups' => [
['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
['name' => 'paragraph', 'groups' => ['templates', 'list']],
['name' => 'mode']]
]])->label(false);

Script.js

$('#addNewContentItem').on('click', function (e) {
e.preventDefault();
var url = 'create-' + $('#itemSelector').val().toLowerCase() + '?contentitemgroupid=' + $('#itemSelector').attr('contentitemgroupid');
$.ajax({
type: 'POST',
url: url,
cache: false,
success: function(res) {
$('.contentItemsManager').append('<div class="ContentItemContainer row">' + res + '</div>');
AddSaveEventListener();
AddSaveMediafileEventListener();
AddRemoveEventListener();
}
});
});

最佳答案

使用renderAjax而不是renderPartial。来自文档:

[renderAjax] Renders a view in response to an AJAX request.

This method is similar to renderPartial() except that it will inject into the rendering result with JS/CSS scripts and files which are registered with the view. For this reason, you should use this method instead of renderPartial() to render a view to respond to an AJAX request.

关于php - 通过 ajax 请求时 Yii2 CKeditor 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842985/

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