jquery - ckeditor disappears when <textarea> gets updated by ajax response -
i using ckeditor jquery adapter in ajax scenario. ckeditor bound textarea element using jquery.
the app:
i designing elearning app client. each course has many steps, drawn database table.
the ui of app form previous , next buttons navigate through course.
the issue admin facility, tutors can create , update steps in each course.
again, admin facility form previous , next buttons. however, there field called steptext want have html rather plain text. hence requirement rich text editor.
the scrolling step step down ajax calls, go off controller, data next (or previous) step, , inject page. ajax calls return html, created partial view.
the problem:
the textarea in partial view. ckeditor binds correctly first time page loaded, not subsequent ajax responses.
the result ckeditor not appear goodness , unappealing nakedness of textarea.
any ideas why happening?
setup (shown refrence) (edited):
the following scripts used:
<script type="text/javascript" src="@url.content("~/ckeditor/ckeditor.js")"></script> <script type="text/javascript" src="@url.content("~/ckeditor/adapters/jquery.js")"></script>
this allows me use ckeditor jquery follows:
$(document).ready(function () { $('#steptext').ckeditor(function () { }, { toolbar: 'basic' }); });
the text area in partial .cshtml file, within div acts target ajax call:
edited!! show how ajax call made !!
<div id="ajaxeditor"> @using (ajax.beginform(mvc.admin.stepeditor.edit(), new ajaxoptions { updatetargetid = "ajaxeditor", httpmethod = "post" }, new { @name = "editstepform", @id = "editstepform" })) { <div class="editor-label"> @html.labelfor(model => model.steptext) </div> <div class="editor-field"> @html.textareafor(model => model.steptext, 20, 68, null) @html.validationmessagefor(model => model.steptext) </div> } </div>
response chris marisic:
i tried rebinding ckeditor in onsuccess of ajax call, when debugging jquery in chrome, following error:
uncaught [ckeditor.editor] instance "mytextarea" exists.
which expect: control name still same , there has not been full page refresh.
calling chris marisic:
if chris marisic wants upgrade comment answer, give him biscuit.
i have solved follows:
ajax form:
<div id="ajaxeditor"> @using (ajax.beginform(mvc.admin.stepeditor.edit(), new ajaxoptions { updatetargetid = "ajaxeditor", httpmethod = "post", oncomplete="rebindckeditor" }, new { @name = "editstepform", @id = "editstepform" })) { <div class="editor-label"> @html.labelfor(model => model.steptext) </div> <div class="editor-field"> @html.textareafor(model => model.steptext, 20, 68, null) @html.validationmessagefor(model => model.steptext) </div> } </div>
javascript:
as error coming (see response chris marisic in question) states ckeditor instance exists, need rid of it. so:
function bindckeditor() { var elem = $('#steptext'); elem.ckeditor(function () {}, { toolbar: [ ['source'], ['preview'], ['cut', 'copy', 'paste', 'pastetext', 'pastefromword', '-', 'scayt'], ['undo', 'redo', '-', 'find', 'replace', '-', 'selectall', 'removeformat'], ['image', 'table', 'horizontalrule'], ['styles', 'format'], ['bold', 'italic', 'strike'], ['numberedlist', 'bulletedlist', '-', 'outdent', 'indent', 'blockquote'], ['link', 'unlink', 'anchor'] ] }); }
bindckeditor() says on tin. configures editor , applies it.
now, when overwrite text area ajax response, need delete instance , recreate it. so:
function rebindckeditor() { delete ckeditor.instances['steptext']; bindckeditor(); }
note use rebindckeditor() in oncomplete ajax form. use oncomplete , not onsuccess, because want rebind regardless of success or otherwise of ajax call.
success!:
i can't recommend ckeditor highly enough. thought going nightmare solve, once sat down , put such thoughts aside, found there concise documentation , user forums.
i looked @ lot of other rich text editors, , none or supported. yahoo's 1 looked good, nightmare , documentation poor unless javascript expert ain't.
i went lwrte, looked good, no support behind it.
i looked @ other shall remain unnamed.
but ckeditor has impressed me. can roll out want no fear or trepidation.
note:
i not working ckeditor nor have connection them. genuine enthusiasm. yes, such thing exist.
Comments
Post a Comment