Is there a way to set a field to read-only on a UI Page?

andrew_lawlor
Giga Expert

So, I have a UI Page with a <g:ui_form> and a single <g:ui_reference> field. What's the best way to set such a field to read-only? I've tried the readonly HTML attribute to no avail.

Thank you!

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

For <g:ui_form>, that element doesn't take the read-only attribute as it doesn't directly take any input. However, it contains input elements that will take input from the user but those have to be set individually and not through the form element.



For <g:ui_reference>, one way you can set this to read-only is by passing it a name which when rendered will produce an id for the element more than likely with a "sys_display." prepended to the name you gave it.


From there, either in the html/xml field of the UI page or in the client script field create a script that will grab the element and set it to read-only.


For example:



HTML/XML:


<g:ui_reference name="my_ref" table="sys_user" />



Client Script:


var ro = gel('sys_display.my_ref');


ro.readOnly = true;




Note: Doing this will make the input field itself read-only. It won't stop the user from using the lookup icon to select a record. For that you may have to hide the lookup icon, remove the function on the onclick attribute, or other method.


View solution in original post

5 REPLIES 5

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

UI Page usually display only read-only information. Isn't this the case for you?



Using Access Control Rules - ServiceNow Wiki



Regards,


Sergiu


Anurag Tripathi
Mega Patron
Mega Patron

try Disabled attribute



HTML input disabled Attribute


-Anurag

ChrisBurks
Mega Sage

For <g:ui_form>, that element doesn't take the read-only attribute as it doesn't directly take any input. However, it contains input elements that will take input from the user but those have to be set individually and not through the form element.



For <g:ui_reference>, one way you can set this to read-only is by passing it a name which when rendered will produce an id for the element more than likely with a "sys_display." prepended to the name you gave it.


From there, either in the html/xml field of the UI page or in the client script field create a script that will grab the element and set it to read-only.


For example:



HTML/XML:


<g:ui_reference name="my_ref" table="sys_user" />



Client Script:


var ro = gel('sys_display.my_ref');


ro.readOnly = true;




Note: Doing this will make the input field itself read-only. It won't stop the user from using the lookup icon to select a record. For that you may have to hide the lookup icon, remove the function on the onclick attribute, or other method.


Thanks - this worked perfectly. If anyone is curious, you can access the magnifying glass lookup element with 'lookup.fieldName'. So, in my case the code is:



gel('lookup.user_ws').style.display = "none";