Sometimes you want to show data without the user being able to change it. The easiest way in Apex to use reports and read only items. There are use cases however for which this is not good enough.
For example when you want to change from read only to editable item dynamically or vv. Or if you want to disable items in a tabular form selectively.
In these cases it is necessary to generate the editable items and to disable them afterwards. In this blogpost you can read how you can do this without compromising the functioning of your page. A special chapter describes the way to selectively disable items in a tabular form.
How to disable items correctly with JavaScript
If you want to disable items, the two most obvious options are the HTML attributes disabled and readonly. But these both have their shortcomings: disabled hides the item and its content from the form and readonly only applies to text inputs and textarea’s.
The attribute disabled is the most dangerous one to use. If you disable one input item on line 1 in a tabular form all the cells below shift one place up. Look at this example of a tabular form on the EMP table. On the right is the starting situation. The first item of Commission is disabled. To the right you see the result after saving. All the values have shifted up one place! So the data has been transferred to other records! Ward is a lucky guy now and Martin is left with no commission. Pretty scary, isn’t it?

The success message indicates exactly what happened, 4 rows have been changed.
So disabled is dangerous to use and readonly is limited in item types.
Luckily we have the class apex_disabled that can be used to disable all types of items. This class uses the css property pointer-events that prevents any mouse action. It also takes care that the item looks not editable (grey background etc. ).
This class does however not prevent the user from navigating to the item with keyboard navigation, i.e. with the Tab key. This can be prevented by applying the attribute tabindex=”-1″.
So the correct way to disable an item is applying the class apex_disabled and the attribute tabindex=”-1″. It takes care that the item cannot be reached by the user either by mouse or by keyboard.
function disable_item ( itemName ) { $('#'+itemName).addClass('apex_disabled').attr('tabindex','-1'); } function enable_item ( itemName ) { $('#'+itemName).removeClass('apex_disabled').removeAttr('tabindex'); }
NB To be sure the application always has to perform a server side check on autorisation because the item values can easily be manipulated using a browser inspector.
How to selectively disable items in a tabular form
If you want to disable items selectively meaning specific rows or cells you have two options:
1. generate the tabular form yourself using apex_item and create readonly items when applicable
2. use an Apex tabular form and disable the items using JavaScript
The first option takes a lot of coding and is hard to maintain. You will soon get a big query which combines data and logic.
The second option delivers much clearer code and you can style the items the Apex way. The only thing you need to do is to supply Apex with the items that need to be read only for each row. It should take the form of a delimited list of column names (equal to the ones in Apex). You can code that into your query or use a function supply the list.
An attribute data-item=”DISABLE_ITEMS” is added to the column containing this list in order to find the data.
In the after refresh Dynamic Action JavaScript is used to loop along all the rows and perform the disabling of the items. You can use the code below:
function disable_tabform_items ( tabSelector ) { // loop along all the rows with a DISABLE_ITEMS cell $(tabSelector).find('[data-item="DISABLE_ITEMS"]').each( function() { // make array of item names to be disabled list = $(this).val().split(':'); if ( list.length > 0 ) { // identify the row var tr = $(this).closest('tr'); // disable all the item in the list for ( i=0 ; i < list.length ; i++ ) { $(tr).find('[headers="'+list[i]+'"]') .find('input,textarea,select,button.ui-datepicker-trigger,a.a-Button--popupLOV') .addClass('apex_disabled') .attr('tabindex','-1'); } } }); }
An example of this you can find in action here.
There you can also download the demo application in order to see how it works form the inside.
Happy apexing!
Original Poste here: https://dickdral.blogspot.com/2016/11/disabling-apex-items-right-way.html