Is there a way to show the contents of an HTML field in a modal popup?

YenGar
Mega Sage

Hello all, 

I've been playing around with showing the contents of an html field in a modal window in the sc_cat_item page in the portal. I have tried to use a template to render the information and just passing the sys_id to a different widget but still not able to actually show the html content as it should appear. All I see is the html tags and the content. 

I have come across information for $sce.trustAsHtml thinking that it may do something but not sure how it would work with an ng-click in the widget. 

 

Ideally, the user would just click a button and the HTML content would display in a modal pop up. Below is what I have done... is it actually possible to do this? 

html:

<div>
  <a href="javascript&colon;void(0)" ng-click="c.openOverlay($event)">
    <button type="button" 
            class="btn btn-outline-warning btn-xs" 
            style="font-size:1.5em"> 
      <i ng-class="c.data.favIcon"
         uib-tooltip="{{::c.data.tooltip}}"
         tooltip-placement="bottom">
      </i>Help Me Choose
    </button>
  </a>
</div>

server script:

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.favIcon = 'fa fa-hand-pointer-o';
	data.tooltip = 'Need help making a selection?';

	var cat_sys_id = $sp.getParameter('sys_id');
	console.log(cat_sys_id);
	data.catSysID = cat_sys_id;

})();

controller:

function(spModal) {
	var c = this;
	var item = c.data.catSysID;
	var itemID = JSON.stringify(item);
	console.log(itemID);
	c.openOverlay = function($event){
		//open modal
		spModal.open({
			widget: 'dg_help_me_choose',
			//pass id to  widget
			widgetInput: { sys_id:itemID },
			//force modal to large size
			size: 'lg'
		});
	};	
}

 

then, this is the widget where I am sending the sys id

<div class="panel panel-{{options.color}} b">
  <div class="panel-heading">
    <h2 class="panel-title h4">
      <span class="pull-left">Help me Choose</span>&nbsp;
    </h2>
  </div>

  <div class="panel-body m-b-lg wrapper-lg">

    <div class="row m-b-lg b-b">
      <ng-bind-html>
        {{::data.optionsField}}
      </ng-bind-html>
    </div>
  </div>

server script: 

//var t = data;
data.testing = "testing --- " + input.sys_id.toString();
var idStr = input.sys_id;
var newIDStr = idStr.replace(/['"]+/g, '');
data.newtesting = "testing -- " + newIDStr;

console.log("server script");
var itemID = new GlideRecord('sc_cat_item');
itemID.addQuery('sys_id', newIDStr);
itemID.query();
if(itemID.next()){
	console.log("Found item");
	console.log("options: " + itemID.u_user_options.getDisplayValue());
	
	data.optionsField = itemID.u_user_options.getDisplayValue();
}

 

this is what the button would look like:

button.PNG

This is the modal: 

 

modal.PNG

 

ideally, it should look like this:

actual html.PNG

 

Any advice or suggestions would be very helpful!

Thank you!

Yen

1 ACCEPTED SOLUTION

Tom Sienkiewicz
Mega Sage

Hi, what you can try is to populate the modal in your client script, you could inject the $sce first like so:

 

(function($scope, $sce) {

 

and then set the content of the HTML field like so:

 

$scope.data.some_var = $sce.trustAsHtml(your_html_text)

 

then you can bind in the HTML to this var.

View solution in original post

4 REPLIES 4

Tom Sienkiewicz
Mega Sage

Hi, what you can try is to populate the modal in your client script, you could inject the $sce first like so:

 

(function($scope, $sce) {

 

and then set the content of the HTML field like so:

 

$scope.data.some_var = $sce.trustAsHtml(your_html_text)

 

then you can bind in the HTML to this var.

Hi Tom!

Ah! That is exactly right! Thank you! That is exactly what I needed to understand how to get the html to show up!

 

This is what i added to the client: 

$scope.html = '<p>' + c.data.optionsField + '</p>';
$scope.trustedHtml = $sce.trustAsHtml($scope.html);

and html

 <div class="panel-body modal-xl">
    <div ng-bind-html="trustedHtml">
    </div>
  </div>

 

and that did it! now I just have to style the content in the html field on how i want it to look! 🙂

html content in modal.PNG

 

Thank you so much!

Yen

Cool, glad it worked!

Vishwanath5
Tera Expert

Hi I have a Similar kind of issue i tried to replicate by looking at above code but there is no luck can anyone help me out on this . I am sharing HTML server and client scripts.

 

HTML code

<div class="col-lg-12">
<div class="row panel-container">
<div class="col-xs-12 no-padding sub-panel">


<div class="panel panel-default">
<div class="panel-heading clearfix" style = "background-color :#dcafff !important";>
<span class=" pull-left" style="font-size: 24px !important;">Announcement</span>
<!--<a class="pull-right" id="ModalNews" ng-click="c.openModal()" ng-if="data.showView">View All</a> -->
</div>
<div class="panel-body" style = "height: 500px !important";>
<div class="table" ng-repeat="anounce in anounces">
<h4 href='' style = "color: black" ><a ng-click="c.openModal(anounce.sys_id)">{{anounce.anouncement_title}}</a></h4>
<b>{{anounce.anouncedate}}</b>
<br>
<div class="dividersplit"></div>
</div>
</div>
</div>

</div>
</div>
</div>


<!-- Model Pop up Script -->
<script type="text/ng-template" id="wd-modalnew">
<div class="panel panel-default" style = "width: 175%; margin-left: -215px;" >
<div class="panel-heading clearfix" style = "background-color :#eeeeee !important; font-size: 24px !important; font-weight: 600;">Anouncement</div>
<div class="panel-body">
<h4 style = "color: #a100ff; font-weight: 600;">{{data.anounce1.anouncement_title}}</h4>
<b>{{data.anounce1.anouncedate}}</b>
<br>
<div> {{data.anounce1.anouncedetails}}</div>
<div ng-bind-html="trustedHtml">
</div>

</div>
<div class="panel-footer clearfix">
<button class="btn btn-primary pull-right" style="margin-right : 15px;" ng-click="c.closeModal()">Close</button>
</div>
</div>
</script>

 

===========================================

Server Script

 

(function() {
data.anounces=[];
 
var gr = new GlideRecord('u_support_2_0_anouncement');
gr.addEncodedQuery('u_active=true');
gr.orderByDesc('u_date_to_sort');
gr.query();
 
var count = gr.getRowCount();
 
while(gr.next()){
var sys = gr.getDisplayValue("sys_id");
 
var anounce =
{
'anouncement_title':gr.getDisplayValue('u_anouncement_title'),
'sys_id' : gr.getDisplayValue("sys_id"),
'anouncedetails': gr.getValue('u_anouncement_details'),
'anouncedate': gr.getValue('u_anouncement_date')
 
};
data.anounces.push(anounce);
//gs.addInfoMessage(JSON.stringify(anounce));
}
// On click open the clicked record in model pop up window
if(input && input.action == 'load-wd'){
var gr = new GlideRecord('u_support_2_0_anouncement');
gr.addEncodedQuery('sys_id='+input.reordId+'');
gr.query();
 
//var count = gr.getRowCount();
while(gr.next()){
data.anounce1 =
{
'anouncement_title':gr.getDisplayValue('u_anouncement_title'),
'u_test_field':gr.getDisplayValue('u_test_field'),
'anouncedate': gr.getValue('u_anouncement_date'),
'sys_id' :gr.getDisplayValue("sys_id"),
'anouncedetails': gr.getValue('u_anouncement_details'),
'anouncedetails1' :  $sp.stripHTML(gr.u_anouncement_details)
};
 
}
 
}
 
})();
 
========================================
Client Script
api.controller=function($scope,$sce, $http, $uibModal,$timeout,$interval,spUtil) {
/* widget controller */
var c = this;
 
$scope.anounces = c.data.anounces;
//$scope.optionsfields = c.data.optionsfields;
$scope.html = c.data.optionsfields;
var html = $scope.html;
alert(html);
$scope.trustedHtml = $sce.trustAsHtml($scope.html);
//$scope.trustedHtml1 = $sce.trustAsHtml(html1);
 
c.openModal = function(e){
c.data.action="load-wd";
c.data.reordId=e;
c.server.update().then(function(res){
//alert(JSON.stringify(c.data.anounce1));
c.modalInstance = $uibModal.open({
templateUrl : 'wd-modalnew',
scope : $scope,
backdrop  : 'static',
keyboard  : false
 
});
});
};
 
 
c.closeModal = function(){
c.modalInstance.close();
return;
};
};