[英]Save and Load Bootstrap Wysiwyg editor content
I am using Bootstrap Wysiwyg Editor . 我正在使用Bootstrap Wysiwyg编辑器 。 It looks great and adding my text and inserted image into the content area.
它看起来很棒,并将我的文本和插入的图像添加到内容区域。
All i need is i want to save the content into database and again when user loads the template name from dropdown i need to fill the editor from the database. 我只需要将内容保存到数据库中,当用户从下拉列表中加载模板名称时,我需要从数据库中填充编辑器。
I tried $('#editor').html()
but not sure how can i send this data to server 我尝试了
$('#editor').html()
但不知道如何将这些数据发送到服务器
Note: Using ASP.Net MVC
注意:使用
ASP.Net MVC
(I don't know if it is the best way, but it's working) (我不知道这是不是最好的方式,但它有效)
Consider a simple model : 考虑一个简单的模型:
public class Contact
{
public string Notes { get; set; }
}
I'm using a simple jquery script in order to get the content of the wysiwyg and put it into a hidden input. 我正在使用一个简单的jquery脚本来获取所见即所得的内容并将其放入隐藏的输入中。
I do this automatically when the user clicks on the "Save" button. 当用户点击“保存”按钮时,我会自动执行此操作。
Here's the JS : 这是JS:
<script language=javascript>
$(function () {
$('#NotesWysiwyg').wysiwyg();
$('#btnSave').bind('click', function () {
$("#Notes").val($("#NotesWysiwyg").html().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'));
});
});
The HTML content : HTML内容:
<div id="NotesWysiwyg"style="height:166px; width:395px; border:1px solid lightgray;display:table-cell"></div>
@Html.HiddenFor(contact => contact.Notes)
<button id="btnSave" type="submit">Save</button>
On the server side : 在服务器端:
// Workaround : Encode WYSIWYG HTML
if (model.Notes != null)
model.Notes = WebUtility.HtmlDecode(model.Notes);
Use ajax to save the data (and vice versa for load) 使用ajax保存数据(反之亦然)
$.ajax({ type: "POST", url: your_url, data: $('#editor').html(), success: success, dataType: dataType });
For sending the content to server for saving, it seems straightforward, just get the data by $("#editor").html() and send it to the server side. 为了将内容发送到服务器进行保存,看起来很简单,只需通过$(“#editor”)。html()获取数据并将其发送到服务器端。
For loading the data to editor, do this: 要将数据加载到编辑器,请执行以下操作:
$("#editor").html("<div><ul><li>demo</li></ul></div>")
Do NOT put the escaped content to the editor like this: 不要将转义的内容放到编辑器中,如下所示:
$("#editor").html("<div><ul><li>demo</li></ul></div>")
Most server side views escape output, but editor cannot accept the escaped format. 大多数服务器端视图都转义输出,但编辑器无法接受转义格式。
Hope this helps. 希望这可以帮助。
I simply used an hidden textarea and I copied html from the editor on form submit. 我只是使用了一个隐藏的textarea,我在表单提交中从编辑器复制了html。
I also used a data attribute to give the target input name so you can use this method with several editor on the same page. 我还使用了data属性来提供目标输入名称,因此您可以在同一页面上使用此方法和多个编辑器。
Editor Html : 编辑Html:
<div id="editor" name="editor" data-target="content" class="form-control wysiwyg mt-lg">*<?php fill the value on loading ?>*</div>
Hidden Textarea Html : 隐藏的Textarea Html:
<textarea type="text" class="hidden" name="content" id="content"></textarea>
Submit Button html : 提交按钮html:
<button type="submit" class="btn btn-danger copyeditor">Save</button>
Js : Js:
$(".copyeditor").on("click", function() {
var targetName = $("#editor").attr('data-target');
$('#'+targetName).val($('#editor').html());
});
css : css:
.hidden {
display: none !important;
visibility: hidden !important;
}
After trying some options (loading data), i have found a solution. 在尝试了一些选项(加载数据)之后,我找到了一个解决方案。
var shortdescrip = STRING1;
var findInit = '<';
var reInit = new RegExp(findInit,'g');
shortdescrip = shortdescrip.replace(reInit, '<');
var findOut = '>';
var reOut = new RegExp(findOut, 'g');
shortdescrip = shortdescrip.replace(reInit, '<').replace(reOut,'>');
$("#div1").html(shortdescrip);
You must replace div1 by the id of the div with the content and STRING1 with the string to load. 您必须将div的id替换为div,并将STRING1替换为要加载的字符串。 I hope that helps.
我希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.