How to make collapse of container in service portal

Prins Kumar Gup
Giga Guru

Hi Folk,

I have a requirement in the service catalog variable to make a container as a collapse in the service portal. I know this functionality available in native view through a variable set it's working fine but this is not working as the collapse in service portal. Please see the below SS:

1. In a native view:

find_real_file.png

2. In service Portal view:

find_real_file.png

Guy's is there any solutions so, please share to me.

Thanks In Advance

PKG

1 ACCEPTED SOLUTION

Oleg
Mega Sage

One can add mission collapsing button by DOM manipulation inside of Client Script.

First of all, one should create Start and End containers in the variable set to make the block collapsible in Desktop environment. You did the step already. I just insert screenshot to show that one need the name of one variable later, which is inside of the block:

find_real_file.png

In the above example I'll use later the name of variable requested_for later in Catalog Client Script. The start container looks for example like the following:

find_real_file.png

Now you can add the following Catalog Client Script:

function onLoad () {
	var $ = this.jQuery,
		fieldName = "requested_for"; // !!! it's the name of any variable from variable set !!!

	setTimeout(function () {
		var $fieldset = $("#sp_formfield_" + fieldName).closest("fieldset"),
			$row = $fieldset.children("div.row"),
			$button = $("<button style='margin-right:10px;margin-bottom:2px;' class='btn btn-icon'>" +
						"<i class='fa fa-plus fa-lg'></i></button>");

		$fieldset.find("legend") // find(">div>legend")
			.prepend($button);
		// one can comment the line if one don't want collapse the 
		$row.hide();

		$button.click(function () {
			$row.toggle();
			$button.find("i")
				.toggleClass("fa-plus fa-minus");
		});
	}, 0);
}

which should be executed onLoad on Service Portal:

find_real_file.png

The script finds <fieldset> element, which exists on Service Portal and which enclose all variables. The code prepends collapsing button before the field set and binds small onclick event handler which toggle the fieldset. As the result one get results like on the picture below:

find_real_file.png

with button, which can be used for collapsing. If one want to have expended initial state (like on Desktop version) then the code need be modified in 2 lines to the following

function onLoad () {
	var $ = this.jQuery,
		fieldName = "requested_for"; // !!! it's the name of any variable from variable set !!!

	setTimeout(function () {
		var $fieldset = $("#sp_formfield_" + fieldName).closest("fieldset"),
			$row = $fieldset.children("div.row"),
			$button = $("<button style='margin-right:10px;margin-bottom:2px;' class='btn btn-icon'>" +
						"<i class='fa fa-minus fa-lg'></i></button>");

		$fieldset.find("legend") // find(">div>legend")
			.prepend($button);

		$button.click(function () {
			$row.toggle();
			$button.find("i")
				.toggleClass("fa-plus fa-minus");
		});
	}, 0);
}

In above code the class in $button is replaced from fa-plus to fa-minus and the line $row.hide() is removed.

One can of cause customize the collapse/expend button in different way by adding title attribute, changing the style and so on. For example, adding "font-size:12px;padding:.4em .6em;background-color:transparent;color:#848a91; border:1px solid #848a91;" to the the style="margin-right:10px;margin-bottom:2px;" one will get the look of the button close to the the look on the desktop (classical UI).

View solution in original post

42 REPLIES 42

I suppose that the reason of your problem: the existence of multiple fieldsets inside of the container, which you need to collapse. For example, if tsc_maintain_levels would have type "Multiple Choice" then the code of Catalog Client Script could work incorrectly.

I'd suggest you to change the line

var $fieldset = $("#sp_formfield_" + fieldName).closest("fieldset"),

to

var $fieldset = $("#sp_formfield_" + fieldName).parent().closest("fieldset"),

and to change the line

$fieldset.find("legend")

to

$fieldset.children("div").children("legend")

or

$fieldset.find("legend").first()

In other words, I suggest you to change the code of Catalog Client Script to the following:

function onLoad () {
	var $ = this.jQuery,
		fieldName = "tsc_maintain_levels"; // !!! it's the name of any variable from variable set !!!

	setTimeout(function () {
		var $fieldset = $("#sp_formfield_" + fieldName).parent().closest("fieldset"),
			$row = $fieldset.children("div.row"),
			$button = $("<button style='margin-right:10px;margin-bottom:2px;' class='btn btn-icon'>" +
						"<i class='fa fa-plus fa-lg'></i></button>");

		$fieldset.find("legend").first() // find(">div>legend")
			.prepend($button);
		// one can comment the line if one don't want collapse the 
		$row.hide();

		$button.click(function () {
			$row.toggle();
			$button.find("i")
				.toggleClass("fa-plus fa-minus");
		});
	}, 0);
}

Hello Oleg,

I want to use this functionality on RITM and CatalogTask not on Portal. So, we can expand/collapse the containers/Variable set.

Can you please help.

Thank you.

 

The functionality already exist in ServiceNow on RITM and Catalog Tasks. One need just create variable block with "Container Start" and "Container End" (optionally you can use Container Split between). It's important only that you check "Display title" on "Container Start" like I shown on my answer.

The problem only that out-of-the-box functionality with collapsing of block of variables don't work in Service Portal. To make it working in Service Portal I added Client Script with the code described in my answer.

Hi Oleg,

 

Thanks for the code, we used this in one of our catalog items with multiple variable sets & toggle buttons in a single request form. Everything is working fine in Orlando version but when upgraded to Quebec the toggle is only working for the first button in the UI.

 

Fix: If multiple toggle buttons are required in the same form (from Quebec version), then follow below steps.

 

We need to modify in three places, rest of the code remains the same. 

// 1. Add an ID to the btn element, I used fieldName variable + 'Btn'
var $button = $("<button style='margin-right:10px;margin-bottom:2px;' class='btn btn-icon' id='"+ fieldName +"Btn'><i class='fa fa-plus fa-lg'></i></button>");


// 2. Use the ID to find the element with jquery and attach click event
$("#"+fieldName+"Btn").click(function () {
  $row.toggle();

  // 3. Use the ID to find the element and toggle the button icon
  $("#"+fieldName+"Btn").find("i").toggleClass("fa-plus fa-minus");
});

 

pradeep37
Tera Contributor

Hi Oleg, 

 

I tried the same, it is not working for me.  need to add collaps and expand option to grouping variable in service portal.

can you please help me on this.

 

Pradeep Sharma