【发布时间】:2015-09-29 11:40:30
【问题描述】:
我正在尝试通过 AJAX 请求添加一个带有 CKeditor 小部件的 from。请求本身可以正常工作并返回我想要的一般部分视图。除了Ckeditor 小部件,返回一个普通的文本框。
当项目被添加到组并重新加载页面时,正在呈现相同的部分视图(在包含所有组项目的 foreach 中),这一次 CKeditor 很好地到位。
在下面发布了我的控制器,CKeditor 和 Scipt with AJAX 请求的初始化。 (CKeditor 包含在 _ContentItemHtml 视图中)
我查看了this,但我无法从JS 调用任何CKeditor 函数,因为它是作为小部件加载的。
控制器动作
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]);
}
视图中的活动表单:
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();
}
});
});
【问题讨论】:
-
如果我理解正确的话,ckeditor.js 已经包含在主模板中了吗?添加小部件后,您必须再次初始化 ckeditor。为此,首先检查 ckeditor.js 是否已包含在您的主模板中,并可能发布为小部件生成的 html,以便我们帮助找出正确的 jQuery 选择器
-
@hyde 我正在通过 Yii2 框架加载
CKeditor,它处理所有的 javascript。我无法访问这个。 -
@topher 将参数添加到返回
return $this->renderPartial('_ContentItemHtml', ['model' => $model],false,true);但它似乎没有做任何事情。 -
我刚刚注意到您使用的是 yii2 而不是 yii 1。在这种情况下,请尝试使用 renderAjax 而不是 renderPartial
标签: php jquery ajax ckeditor yii2