When using Easyui,datagrid, the data cannot be displayed if there is a newline character in the JSON

Source: Internet
Author: User

Http://www.xuebuyuan.com/2103538.html

Each value needs to handle line breaks

Item = Item. Replace ("\ r \ n", ""); or \\r\\n to replace newline characters

Http://www.cnblogs.com/lhws/archive/2013/08/16/3262301.html

TextArea is saved to the MySQL database \ n by using inline editing in the DataGrid in Easyui
Enter a carriage return in textarea in JS read textarea value has \ r \ n and then into the business layer into a string it is possible to become a blank space and then be stored in the database, when the value is taken out into the form of a space, so we need to do not display \ r \ n replace it.

When JS takes textarea:

If you use

var str = document.getElementById ("textarea"). Value;
str = str.replace ("\ r \ n", "<br>");

will only replace the first \ r \ n with <br>, how do you replace all n \ r \ nthe <br>? Does it have to be infinitely more than a replace?

In fact, replace can be combined with regular expressions to replace all \ r \ n at once.

var reg=new RegExp ("\ r \ n", "G");
Str= str.replace (Reg, "\ r \ n");

So all the str \ r \ n is replaced by <br>. You can then save it in the database.

When you remove it, just replace it with the value.

var reg=new RegExp ("<br>", "G");
stt= str.replace (Reg, "\ r \ n");
document.getElementById ("textarea"). Value=str;

More detailed analysis and application can refer to JavaScript's Replace method and regular expression in conjunction with the application explained

Here are the 2 functions I wrote directly into the universal package later using can be replaced

function ReplaceTextarea1 (str) {
var reg=new RegExp ("\ r \ n", "G");
var reg1=new RegExp ("", "G");

str = str.replace (Reg, "<br>");
str = str.replace (REG1, "<p>");

return str;
}


function ReplaceTextarea2 (str) {
var reg=new RegExp ("<br>", "G");
var reg1=new RegExp ("<p>", "G");

str = str.replace (Reg, "\ r \ n");
str = str.replace (REG1, "");

return str;
}

2. When the contents of the HTML <textarea> tag are converted to "\ r \ n" and the submitted content is placed in the <textarea> tab, the carriage return will revert to the normal line feed.

However, when a string with "\ r \ n" is placed in an ordinary text field of HTML, the original line break is converted to a space, so you need to replace "\ r \ n" with "</br>" in the string before displaying it.

Also, it is important to note that the El expression transforms the "</br>" line break into "&lt;/br&gt;", "&lt;" and "&gt;" corresponding to the "<" and ">", that is, to the page, or display as "</br>" (Oh, El is too intelligent), in order to remedy this, you need to use the JSP tag (<%=%>) to display.

So when you need to display the content submitted by <textarea> in a normal text field, you can store the non-processed string directly in the database, instead of "\ r \ n" with "</br>" before the display, then use <%= on the page%> To display the content.



3. If the spaces and carriage returns that are submitted to the database via the textbox are displayed correctly
public string Myreplace (string mystr)
{
if (mystr = = NULL | | MyStr = = "")
{
Return ("&nbsp;");
}
Else
{
MyStr = mystr. Replace ("\n\r", "<br>");
MyStr = mystr. Replace ("\ R", "<br>");
MyStr = mystr. Replace ("\ T", "");
return (MYSTR);
}
}

When using Easyui,datagrid, the data cannot be displayed if there is a newline character in the JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.