function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kon DeleKon Dele 

How to diplay apex:inputField as readonly on edit page

Hely yall,
We have a custom VF QuoteLineItem page that automatically calculates Total price based on Discount and Unit Price entered. Is there a way to lock down the UnitPrice  field so that it's only editable by certain profiles? It's an editable apex:inputField. I've tried using an outputField, which displays the UnitPrice but can no longer calculate the Total price. Here's a screenshot and code snippet.User-added image
<apex:inputField value="{!item.Item.UnitPrice}" rendered="{!IF(($Profile.Name =='System Administrator'), true , false)}"/>


 
Best Answer chosen by Kon Dele
William LópezWilliam López
Hello Kon Dele,

Since the condition that you have to change its the disable status I think the best way to go its Javascript + Jquery. You might need to add jquery as an static resource or enable the remote site for google apis so you can include that script.

I am assuming you can have a table with several rows. 

Then you can do something like this:

on the top of the pages add this:
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
	$(function (){
			var status = "{!IF(($Profile.Name =='System Administrator'), false  , true)}"; 
			var fieldList = $(".fieldRef");
			if(fieldList.length > 0){                         	
				for (var i = 0, len = fieldList.length; i < len; i++) {
					fieldList[i].disabled = status== "true" ? true : false;    
				}
			}
		}
	)
</script>
​if you don't have a table of field change the JavaScript to a single result:
 
var status = "{!IF(($Profile.Name =='System Administrator'), false  , true)}"; 
var fieldList = $(".fieldRef");
fieldList.disabled = status== "true" ? true : false;

Then you need to add a class selector your field (you can do this by Id but it can get complex), this way you can find all fields with the same selector.
 
<apex:inputField value="{!item.Item.UnitPrice}" styleClass="fieldRef" />

Please give it a try and let me know how it goes.

Regards.

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 



 

All Answers

Mathew Andresen 5Mathew Andresen 5
HI,

A couple of different one, you should be able to do math inside of the field.  For example, I did the following
 
<apex:outputfield  value="{!(guageChartMargin - GuageMaxMargin)*1000}"/>

Or, if you did want to do it by a profile, you could use a lookup, and then an if statement within the field
 
<apex:output field value={! if (true, do this, do this instead }" />

For that to work of course you would need to pull over the user profile, and use the if statement against it.
William LópezWilliam López
Hello Kon Dele,

Since the condition that you have to change its the disable status I think the best way to go its Javascript + Jquery. You might need to add jquery as an static resource or enable the remote site for google apis so you can include that script.

I am assuming you can have a table with several rows. 

Then you can do something like this:

on the top of the pages add this:
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
	$(function (){
			var status = "{!IF(($Profile.Name =='System Administrator'), false  , true)}"; 
			var fieldList = $(".fieldRef");
			if(fieldList.length > 0){                         	
				for (var i = 0, len = fieldList.length; i < len; i++) {
					fieldList[i].disabled = status== "true" ? true : false;    
				}
			}
		}
	)
</script>
​if you don't have a table of field change the JavaScript to a single result:
 
var status = "{!IF(($Profile.Name =='System Administrator'), false  , true)}"; 
var fieldList = $(".fieldRef");
fieldList.disabled = status== "true" ? true : false;

Then you need to add a class selector your field (you can do this by Id but it can get complex), this way you can find all fields with the same selector.
 
<apex:inputField value="{!item.Item.UnitPrice}" styleClass="fieldRef" />

Please give it a try and let me know how it goes.

Regards.

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 



 
This was selected as the best answer
Kon DeleKon Dele
Thanks Bill, that worked perfectly!