views:

2417

answers:

5

I have a complex UI with several nested tables, a repeater control and several other HTML controls that have their disable attribute set based on business logic in JQuery.

I need a way to disable the entire table (including nested controls, UI elements etc) and re-enable them when a user toggles a button.

I could iterate through the controls, find each of them and apply the "disable" attribute, but when the user chooses to enable, I will need to go through the trouble of checking if each of the control was initially enabled or not (based on the business logic as explained in para 1)

All I need is a way to leave the initial UI alone and overlay the disable/enable functionality on the UI.

Is there a easy way of accomplishing this?

+1  A: 

checkout the blockUI plugin. you can put a grey div over the entire table with it.

This will have the same effect as disabling every table control.

mkoryak
What about keyboard navigation? It does nothing about that. I can still navigate to a button/link and use Enter/Space to invoke its function.
Josh Stodola
kudos to your users for being that smart. maybe its time for server side validation?
mkoryak
actually i do this differently(along with blockui clone for some stuff) : each control is generated by a jsp custom tag, which registers the id of the element with a JS lib that has a disable() and enable() functions, and its called when neeeded. this only works because every control is created using the custom tags, or else is manually registered with the disabling lib. There is no other easy way
mkoryak
+1  A: 

This is where jQuery Selectors rock. If you give each of these items a class of say "disable" then you can select all of the items with jQuery by doing a simple $(".disable").

Now that you have all of the elements you can do whatever you want with them all at once say for example

$(".disable").attr("disable","disable");
runxc1 Bret Ferrier
Yes, i agree. However, when it comes time to enable a control, I will need to run the business logic again to check if it was originally disabled or not, which I would like to avoid. What I need is a way of disabling all the controls within a table without having to iterate through each control and setting the disabled attribute
DotnetDude
That is easy just add another attribute for holding the data such as "Idisabledthis" $(".disable").attr("disable","disable").attr("Idisabledthis","true"); now when it comes time to enable everything you can select all controls that have an attribute of "Idisabledthis" with a value of true. jQuery Selectors rock!
runxc1 Bret Ferrier
urm.. i think you mean $(".disable").attr("disabled","disabled")
Simon_Weaver
A: 

I would use a div to layer over the entire table, essentially making it "unclickable". Checkout the BlockUI plugin for this.

However, this does not prevent the user from navigating through the underlying input elements and hyperlinks with the TAB key, and then they can invoke them using the ENTER key. To prevent this, you must disable all the input elements and anchors (and re-enable them when appropriate). That can be done easily, of course, using jQuery...

// Disable
$("#tableIdHere").children(":input, a").attr("disabled", "disabled");

// Enable
$("#tableIdHere").children(":input, a").removeAttr("disabled");

Perhaps a more efficient approach would be to disabled the entire table instead of looping through its children. I've done this with success before using IE, but I am not sure if the results will be consistent across browsers; I will leave that up to you to test...

$("#tableIdHere").attr("disabled", "disabled");
Josh Stodola
Alternatively rather than disabling the elements you could add a temporary Click handler that will trap and remove keypresses while 'disabled' by BlockUI.
Erik Forbes
The temporary handler would then be removed once 'enabled' again.
Erik Forbes
You obviously don't understand.
Josh Stodola
A: 

If you choose to write your own script iterate over all the controls when disabling them, check the value of disabled before you do so. For controls that are not disabled when you disable them, chuck them into a window- (or class-) level array variable. Then, when you want to re-enable them, iterate the array only, rather than all of the controls.

Example disable code:

window.enableList = new Array();
controls = tableElement.getElementsByTagName("input"); // May need to repeat this for "select" and "textarea"
for(i = 0; i < controls.length; i++) {
    control = controls[i];
    if(!control.disabled) {
        control.disabled = true;
        window.enableList[enableList.length] = control;
    }
}

Example re-enable code:

for(i = 0; i < window.enableList.length; i++) {
     window.enableList[i].disabled = false;
}
Jason Musgrove
A: 

for(var i =0; i< window.enebalelist.length; i++) { window.enablesList[i].disable = True;}