Friday, February 28, 2014

tinymce editor in dynamical content inside bootstrap modal dialog

I need to use tinymce (http://www.tinymce.com/) editor in dynamically crated content inside bootstrap modal dialogue. The official document doesn't help and my online search found few tips. After a few tries, following are key steps to use tinymce in dynamic content:

First, we need to make the focus in event work properly inside bootstrap dialog  (http://www.tinymce.com/wiki.php/Tutorials:TinyMCE_in_a_boostrap_dialog)

// Prevent bootstrap dialog from blocking focusin
$(document).on('focusin', function(e) {
    if ($(e.target).closest(".mce-window").length) {
        e.stopImmediatePropagation();
    }
});

Second, after the modal dialog statement, initialize tinymce.

$('#myDialogElement').modal();
$('#myTextArea').tinymce( { width: '100%', menubar: false});

Third, remove the tinymce editor when the modal dialog closes, as the first step, this is called in jQuery initialization.

$('#myDialogElement')on('hidden.bs.modal', function (e) {
        // need to remove the editor to make it work the next time
        tinymce.remove();
});

In the above code, we NuGet the jQuery version tinymce. Both jquery.tinymce.min.js and tinymce.js are included in the Web page.

7 comments:

  1. I did that but it takes a long time to load modal with tinymce. While without it, everything works fine and no delay in modal display.

    ReplyDelete
  2. do you mean that you don't need to call tinymce.remove()?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. I mean if i dont use tinymce plugin, modal loads normal, but if i add tinymce it takes a long time to load.

    ReplyDelete
  4. This Works perfectly! Thanks a lot!

    ReplyDelete
  5. i have used tinmyce editor on modal it takes a long time to load ,, can any better solution is there?

    ReplyDelete