Enable / Disable a control on Vanilla Form
Is it possible to disable / deactivate a control (say, drop-down or text box) on a vanilla form - say, on the Render_Before or Render_After events?
If so, any example(s) is(are) appreciated.
Best Answers
-
sahotataran ✭✭✭
what i would do is :
in my hooks.php in
Base_Render_Before() Event
$Sender->Head->AddString(' < script type="text/javascript" >
jQuery(document).ready(function($) {
$('#Form_CategoryID').attr('disabled' , 'disabled');
});
< /script>');the above code with be added to your head section and disable the Select with ID - 'Form_CategoryID'
There was an error rendering this rich post.
0 -
sahotataran ✭✭✭
do you have it live? i can check it out if the string was added or not?
There was an error rendering this rich post.
0 -
sahotataran ✭✭✭
AddString did add it to your page. Then it might have been an ID problem or something else i guess
my fault i think try
$("#Form_CategoryID").attr("disabled", true);
There was an error rendering this rich post.
0
Answers
To clarify (for example, the category control on a new discussion page).
Note i replaced angled brackets with [ and ]
At present:
[select id="Form_CategoryID" name="Discussion/CategoryID"]
In the Render_Before event, i want it to make it like:
[select id="Form_CategoryID" name="Discussion/CategoryID" disabled="disabled"]
How can this be done?
easy way is to use javascript/jquery
There was an error rendering this rich post.
I was thinking along those lines (and noticed similar code using Attr(..) function in numerous VF jscript files) but, being a "newbee", i am struggling with some basic holes in understanding such as:
Location of the .js file (i would imagine the plugin's folder but am not sure)...
What is the object sent to the jscript file ($Sender->Form ?).
I assume the code should be invoked in the Render_Before event but, again, not sure
I will continue exploring this line of thought but if you have a 'short cut' or more specific example, either will be greatly appreciated.
what i would do is :
in my hooks.php in
Base_Render_Before() Event
$Sender->Head->AddString(' < script type="text/javascript" >
jQuery(document).ready(function($) {
$('#Form_CategoryID').attr('disabled' , 'disabled');
});
< /script>');
the above code with be added to your head section and disable the Select with ID - 'Form_CategoryID'
There was an error rendering this rich post.
Feedback from my trials and tribulations...
As is, it crashed - i needed to change all single quotes within the outmost AdString to double quotes. Then, it did not crash... But:
The string was added to the header and was visible - but no action taken. So...
I changed the verb to AddScript instead of AddString. Result: no crash and no action either.
So, i poked around and saw various usages of $(frm).find({specs}), so...
I tried several 'find's by changing $('#Form_CategoryID')... to
$(frm).find("[name$=Form_CategoryID"])... or $(frm).find("#Form_CategoryID"])... none of which worked.
What i think is happening is that the script is either not added (I don't see it in FireBug) or does not run (I introduced into the script '1/0;' statement at the end to see if it is an issue of not finding the control or not running and i got no crash - so, i think, the script is not running.
Sorry for the lengthy response...
do you have it live? i can check it out if the string was added or not?
There was an error rendering this rich post.
No, I host it locally.
FireBug is great - i can see lots of scripts there (12, in fact, in the Head section) but did not see the one i tried to add. When i used 'AddString' - the script string was visible on the page (I did not need FireBug).
AddString did add it to your page. Then it might have been an ID problem or something else i guess
my fault i think try
$("#Form_CategoryID").attr("disabled", true);
There was an error rendering this rich post.
I think your initial use of the setting is correct and is not the issue. For example:
In line 76 of discussion.js there is a statement:
I tried this syntax in one of my attempts (see trials and tribulations above) and did not have any success.
It seems to me that the script does not get executed.
Using FireBug (learning new things everyday) I was able to locate it on the page...
I find the script in the DOM tab with the path of:
window > frames > document > head > childNodes
It is there in one of the nodes but hibernating, i guess. How do i kick it to spring into action?
Saho,
Finally, i figured it out.
Your idea was totally valid the problem was some imbalanced parentheses and braces, etc. (aside of large gaps in my knowledge of jQuery and how the .js scripts work to begin with).
After taking the time to learn just enough of jQuery and using FireBug to sort out the issues (and gain a good balance on my parentheses) i was able to rise to the challenge and disable the Category drop down on the new discussion post page.
I got the exact effect i wanted, namely, the selected category was visible but the drop down was disabled so it could not be changed (this was the purpose of this saga).
But my happiness was short lived as I realized an 'interesting feature' or Vanilla bug. When the drop down is disabled (though a selection exists), Vanilla considers it unselected and assigns the discussion the Root category.
In my book it's a bug.
Following this disappointment and now armed with some jQuery knoledge I took the approach of removing all but the selected option. Now, I have my cake and can eat it too: The user cannot change the category (in some cases) and Vanilla posts the correct value.
Again, i thank you for your help.
Bey
Hey Bey can post this issue with Vanilla devs at Github. they can come up with solution
There was an error rendering this rich post.
As i am new to this, a link would be appreciated... My cursory search did not yield a page with Github Vanilla (and i am on my way out of town).
Thanks
https://github.com/vanillaforums/Garden/issues here you go
There was an error rendering this rich post.
my guess is that as a disabled field, the post may not be returning the value to the controller. i would do a little snooping to see if this is the case.
if it is you could hide the drop down field rather than disable it, then the result should be posted properly. you could add a text field to the view to display the value of your hidden field, in effect making it read only, but visible.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
Thanks.
I am curious about the result of the snooping.
I am happy with the solution i came up with as it involves less changes on my part and involves less changes to the user's experience. By removing all but the desired item from the drop down list I accomplish the same thing. Also, I am finished doing all the work and can move on to the next task. Still, thanks for sharing yet another approach that may be useful in some other scenario.