How can I show line-breaks from string field in UI Page

Cris P
Tera Guru

Good morning everyone,

I have created a string field on the company table which looks like this, with this text in it:

find_real_file.png

I then have a client script which passes the value of that same field to a UI Page (using setPreference()):

	function invokeMessage(){
		//console.log('crisOut');
		var company = g_form.getReference('company', alertNote);

		function alertNote(company){
			var an = company.u_active_notification;
			if(!an){
				//console.log('crisNo ');
				return;
			}
			//console.log('crisYes ');
			var gdw = new GlideModal('active_notification');
			gdw.setTitle('Active Notification');
			gdw.setPreference('sysparm_activeNotification', an); //pass the active note to the ui page
			gdw.render();
		}
	}

 

I then grab the value in the UI Page like this:

	<j:set var="jvar_active_note" value="${sysparm_activeNotification}"/>
		
	<p class="modal-title h4 modal-header">${jvar_active_note}</p>

 

The end result is it displays the message fine, but note the line-breaks are not present in the UI Page:

find_real_file.png

What would be the best way to show the line-breaks, which are present on the string field on the form, in the UI Page too?

9 REPLIES 9

Community Alums
Not applicable

Hi Cris,

If your string is picking up on the newline characters in the top text box, you could try a string.replace(/\n/g, '<br></br>') to replace the javascript new lines with with HTML line breaks when it moves it to the lower text box.

 

Please mark my answer as Correct & Helpful, if applicable.

Thanks

Sandeep

Hi Sandeep,

 

I have replaced the line breaks with the <br> tags as you suggested, also changed the Jelly tags so it does not escape them like so:

<table>
	<tr>
		<td>

		<j:set var="jvar_active_note" value="${sysparm_activeNotification}"/>
		<g2:no_escape>${HTML:jvar_active_note}</g2:no_escape>

		</td>
	</tr>
</table>

 

But the text is still escaping the br tags:

find_real_file.png

 

Any idea why it is escaping them? I have also tried the <g:no_escape> tag

Cris P
Tera Guru

So far none of these worked:

  1. <pre> tags
  2. ${jvar_active_note.getHTMLValue()} (using getHTMLValue() method)
  3. white-space: pre styling (also tried adding the style inline)
  4. white-space: pre-line styling (also tried adding the style inline)
  5. <g2:no_escape> tag
  6. <g:no_escape> tag
  7. <j2:whitespace trim="false"> tag

Using all of the above, the string would still ignore the line breaks. Here is what ended up working for me:

 

  • I took the suggestion @Sandeep Dutta kindly made to replace the line breaks with <br> tags in my Client Script using RegEx:
	function invokeMessage(){
		//console.log('crisOut');
		var c = g_form.getReference('company', function(company){
		var an = company.u_active_notification;
		var lineBreaks = an.replace(/\n/g, '<br></br>'); //input <br> tags
		//console.log('crisAN ' + an);
		//console.log('crisAN1 ' + lineBreaks);
		if(!an){
			//console.log('crisNo ');
			return;
		}
		//console.log('crisYes ');
		var gdw = new GlideModal('active_notification', false, 'modal-lg');
		gdw.setTitle('Active Notification');
		gdw.setPreference('sysparm_activeNotification', lineBreaks); //pass the HTML version to the GlideModal
		gdw.render();
	});
}
  •  In the UI Page HTML, I then tried to use the 'getHTMLValue()' method mentioned above, with no luck. The <br> tags were being escaped in the GlideModal.
  • The solution:
  1. In the UI Page Client script I added this Load Event:
addLoadEvent(function(){
	var m = GlideModal.prototype.get("active_notification");
	var p = m.getPreference("sysparm_activeNotification");
	document.getElementById("crisDiv").innerHTML = p; //create div with same id in HTML and set the innerHTML to the preference we grabbed from the Client Script
});

 

End result: (Spoiler alert! We have the line breaks!):

find_real_file.png

 

I was very close to giving up on this; So many things that would work in a regular HTML implementation (such as the <pre> tags) did not work on UI Page. Did not think something as simple as just showing the line breaks from a string field would have been so difficult!

Jesper Slot
Tera Guru

Hi @Cris P and everyone else dropping by.

 

I know this is an old post, but in case someone else comes across, wondering how to do this (like me), I wanted to share a much more simple solution.

I found that simply adding an html tag inside the setPreference method enabled you to add additional html tags, like the <br></br>:

 

glidemodal.setPreference("title", "<html> your text here followed by a line break<br></br>more of your text here</html>");

 

Maybe there are even smarter tags.. I'm not an html expert, but this did the trick for me. 

dbook
Kilo Sage

Solution is very simple, add 

' style="white-space:pre-line" ' to the area you want to be in scope to include line breaks from String fields. 

E.g. 

    <tr>
                <td class="section-header border-bottom border-left border-right" colspan="12" valign="top">
                    <p class="margin-0">Impact:</p>
                </td>
            </tr>
            <tr style="height: 14.25pt;">
                <td class="value-cell border-bottom border-left border-right" colspan="12" valign="top" style="white-space:pre-line">
                    <p class="text-black">
                        ${gs.getMessage('{0}',[incidentGr.getDisplayValue('business_impact')])}
                    </p>
                </td>

            </tr>