Difference between revisions of "Remove Options from a Combo Box Using JavaScript"
(→See Also) |
|||
(13 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
You can use a JavaScript function to modify the options available in a pull-down box on the page. | You can use a JavaScript function to modify the options available in a pull-down box on the page. | ||
− | The following example removes two different [[Types]] available for '''Level 2''' records: | + | ==Remove One or More Options== |
+ | ''Example 1,'' | ||
+ | |||
+ | The following example removes two different [[Types]] available for new '''Level 2''' records (useful if you need to define complex criteria for what type of Level 2 records can be created): | ||
<pre> | <pre> | ||
− | function RemoveTypes() | + | function RemoveTypes(){ |
− | |||
var i=0; | var i=0; | ||
− | var frm = document. | + | var frm = document.frmevent; |
− | // at Level 1 use document.form1 instead of document. | + | // at Level 1 use document.form1 instead of document.frmevent |
+ | |||
+ | if (frm.eventid.value==0) | ||
// Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | ||
for (i=frm.eventtype.options.length-1;i>=0;i--) | for (i=frm.eventtype.options.length-1;i>=0;i--) | ||
{ | { | ||
− | if (frm.eventtype.options[i].text=='FirstType') frm.eventtype.options[i]=null; | + | if (frm.eventtype.options[i].text=='FirstType' || frm.eventtype.options[i].text=='SecondType') |
− | if (frm. | + | frm.eventtype.options[i]=null; |
+ | } | ||
+ | } | ||
+ | } | ||
+ | RemoveTypes(); | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ''Example 2,'' | ||
+ | |||
+ | The following example removes a Branch on '''Level1''' from the Branch drop down list. | ||
+ | |||
+ | <pre> | ||
+ | function RemoveBranches() | ||
+ | { | ||
+ | var i=0; | ||
+ | var frm=document.form1; | ||
+ | // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | ||
+ | for(i=3;i>=0;i--) | ||
+ | { | ||
+ | if (frm.branchid.options[i].value=='448060') | ||
+ | //448060 is a Branch id | ||
+ | frm.branchid.remove(i); | ||
+ | } | ||
} | } | ||
+ | RemoveBranches(); | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ''Example 3,'' | ||
+ | |||
+ | The following example also removes Branches on '''Level1''' from the Branch drop down list. | ||
+ | |||
+ | <pre> | ||
+ | function RemoveBranches() | ||
+ | { | ||
+ | var i=0; | ||
+ | var frm=document.form1; | ||
+ | if(frm){ | ||
+ | // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | ||
+ | for(i=(frm.branchid.options.length-1);i>=0;i--){ | ||
+ | if (frm.branchid.options[i].value!=1111 && frm.branchid.options[i].value!=2222 && frm.branchid.options[i].value!=3333 &&frm.branchid.options[i].value!=4444){ | ||
+ | frm.branchid.remove(i); | ||
+ | } | ||
+ | } | ||
+ | } | ||
} | } | ||
− | + | RemoveBranches(); | |
+ | |||
+ | </pre> | ||
+ | |||
+ | If you wish to control this by [[Role|role]] (for example if you want a role to be able to view existing Level 2 records of a given [[Type]], but not create them...) | ||
+ | |||
+ | Include this in the variable section at the top of the script: | ||
+ | var myrolelist="@me.rolelist@"; | ||
+ | |||
+ | And replace or include the following in the if logic section: | ||
+ | if (myrolelist.indexOf(",1234,")>=0) | ||
+ | |||
+ | ::''Note that you must test for greater than or equal to 0. If the role is the first listed in the [[User|user]]'s role list it will return a 0 rather than a 1 because of the preceding comma.'' | ||
+ | ==To Remove All Options Except the Selected== | ||
+ | * The following script will prevent users from changing the Level 2 Type once the record has been saved: | ||
+ | <pre> | ||
+ | // For existing Types remove all options from the Type field (except the selected) so it can't be changed | ||
+ | if (frm.eventid.value!=0) | ||
+ | { | ||
+ | for (i=frm.eventtype.options.length-1;i>=0;i--) | ||
+ | { | ||
+ | if (frm.eventtype.options[i].value!=frm.eventtype.value) | ||
+ | frm.eventtype.options[i]=null; | ||
+ | } | ||
+ | } | ||
</pre> | </pre> | ||
− | |||
==See Also== | ==See Also== | ||
* [[Add Options to a Combo Box Using JavaScript]] | * [[Add Options to a Combo Box Using JavaScript]] | ||
+ | * [[Conditional Lists of Values]] | ||
[[Category:JavaScript]] | [[Category:JavaScript]] |
Latest revision as of 11:30, 12 June 2014
You can use a JavaScript function to modify the options available in a pull-down box on the page.
Remove One or More Options
Example 1,
The following example removes two different Types available for new Level 2 records (useful if you need to define complex criteria for what type of Level 2 records can be created):
function RemoveTypes(){ var i=0; var frm = document.frmevent; // at Level 1 use document.form1 instead of document.frmevent if (frm.eventid.value==0) // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. for (i=frm.eventtype.options.length-1;i>=0;i--) { if (frm.eventtype.options[i].text=='FirstType' || frm.eventtype.options[i].text=='SecondType') frm.eventtype.options[i]=null; } } } RemoveTypes();
Example 2,
The following example removes a Branch on Level1 from the Branch drop down list.
function RemoveBranches() { var i=0; var frm=document.form1; // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. for(i=3;i>=0;i--) { if (frm.branchid.options[i].value=='448060') //448060 is a Branch id frm.branchid.remove(i); } } RemoveBranches();
Example 3,
The following example also removes Branches on Level1 from the Branch drop down list.
function RemoveBranches() { var i=0; var frm=document.form1; if(frm){ // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. for(i=(frm.branchid.options.length-1);i>=0;i--){ if (frm.branchid.options[i].value!=1111 && frm.branchid.options[i].value!=2222 && frm.branchid.options[i].value!=3333 &&frm.branchid.options[i].value!=4444){ frm.branchid.remove(i); } } } } RemoveBranches();
If you wish to control this by role (for example if you want a role to be able to view existing Level 2 records of a given Type, but not create them...)
Include this in the variable section at the top of the script:
var myrolelist="@me.rolelist@";
And replace or include the following in the if logic section:
if (myrolelist.indexOf(",1234,")>=0)
- Note that you must test for greater than or equal to 0. If the role is the first listed in the user's role list it will return a 0 rather than a 1 because of the preceding comma.
To Remove All Options Except the Selected
- The following script will prevent users from changing the Level 2 Type once the record has been saved:
// For existing Types remove all options from the Type field (except the selected) so it can't be changed if (frm.eventid.value!=0) { for (i=frm.eventtype.options.length-1;i>=0;i--) { if (frm.eventtype.options[i].value!=frm.eventtype.value) frm.eventtype.options[i]=null; } }