2amigos / yii2-tinymce-widget

TinyMCE WYSIWYG widget for Yii2
http://yiiwheels.com
Other
100 stars 48 forks source link

TinyMCE is disappeared when I use Pjax plugin #50

Closed palexandrite closed 4 years ago

palexandrite commented 4 years ago

Hello!

I use dosamigos\tinymce\TinyMce plugin in yii\bootstrap4\ActiveForm with yii\widgets\Pjax.

TinyMCE is disappeared when I push the submit button but I need that it is not disappeared.

How to fix it?

Here is the code:

in the view:

<?php Pjax::begin(); ?>
<?php if(!empty($message)): ?>
    <p><code>We got this!</code></p>
<?php endif; ?>
<?php $form = ActiveForm::begin([
        'options' => ['data-pjax' => true],
]); ?>

<?= $form->field($commentForm, 'comment')->widget(TinyMce::class, [
    'options' => ['id' => 'foobartiny'],
    'language' => Yii::$app->language,
    'clientOptions' => [
        'branding' => false, // To show or not who powered (TinyMCE label)
        'menubar' => false, // To show or not the highest menu, but can use for layout of this menu
        'plugins' => [
            'advlist', 'autolink', 'lists', 'link', 'charmap', 'print', 'preview', 'anchor',
            'searchreplace', 'visualblocks', 'fullscreen', 'wordcount',
            'insertdatetime', 'contextmenu', 'paste', 'autoresize'
        ],
        'toolbar' => "undo redo | forecolor backcolor | bold italic underline strikethrough  | bullist numlist",
        'fontsize_formats' => '11px 12px 14px 16px 18px 24px 36px 48px',
        'min_height' => 10,
        'max_height' => 500,
    ]
])->label(Yii::t('x', 'Leave a comment')) ?>

<?= Html::submitButton(Yii::t('x', 'Send'), ['class' => 'btn btn-green mb-3']) ?>

<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>

in the action:

public function actionSingle($slug)
{
    $event = Event::find()->active()->one();

    if (!$event) {
        throw new NotFoundHttpException(Yii::t('x', 'Not found'));
    }

    $message = '';
    if ($commentForm->load(Yii::$app->request->post())) {
        if (Yii::$app->request->isPjax) {
            $message = Yii::t('x', 'Thank you');
        }
    }

    return $this->render('single', [
        'event' => $event,
        'commentForm' => $commentForm,
    ]);
}
palexandrite commented 4 years ago

I found the solution by myself.

Here is: we need to remove the initialized TinyMce before send pjax-request like that:

$(document).on("pjax:beforeSend", function() {
            tinyMCE.remove();
});