简体   繁体   中英

Convert a Textarea in CKEditor in Yii2

I m trying without success to convert a textarea that i have created dynamically into CKEditor in Yii2. I m using the yii2-ckeditor-widget from 2amigos .

Here is how I generate the Textarea in a form:

$form->field($model, 'answer')
     ->textarea(['rows' => 5, 'id' => 'textarea_answer'])
     ->label(false) 

The generated textarea is:

<textarea id="textarea_answer" class="form-control" name="topic[answer]" rows="5">
  <p>test</p>
</textarea>

Then when I add the Textarea dynamically in a div, it works. But directly after that i try to convert the Textarea to CKEditor with:

CKEDITOR.replace('textarea_answer');

or

 $( '#textarea_answer' ).ckeditor();

The textarea get the Style property visibility:hidden and disappear. Like below:

<textarea id="textarea_answer" class="form-control" name="topic[answer]" 
          rows="5" style="visibility: hidden;">

          <p>test</p>

</textarea>

And i get the following error in the console:

Uncaught TypeError: Cannot read property 'registered' of undefined

控制台

How can i solve that? Is it may be a dependencies problem?

Sorry. I have found the problem, the way i was creating the view dynamically was not the good Way. I was using renderPartial instead of renderAjax .

So i just render dynamically CKEditor directly.

The view (_view):

<?= $form->field($model, 'answer')
         ->widget(CKEditor::className(), 
            [
                    'options' => ['rows' => 5, 'id' => 'textarea_answer'],
                    'preset' => 'custom',
                    'clientOptions' => [
                        'extraPlugins' => 'codesnippet',
                        'toolbarGroups' => [
                            ['name' => 'undo'],
                            ['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
                            ['name' => 'colors'],
                            ['name' => 'links', 'groups' => ['links', 'insert']],
                            ['name' => 'others', 'groups' => ['others', 'about']],

                            ['name' => 'codesnippet']
                        ]
                    ]
            ])->label(false); 
?>

and the way i render it in the Controller:

echo $this->renderAjax( '_view', ['model'=> $model] );

I was using renderPartial. that s why it was not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM