HowTo build related dropdowns in Sugar 5.0.0b
Some time ago I was asked to explain how I succeeded in creating related dropdowns in sugar 5.x.
First of all, I'd like to say that other sugar developers managed to build such dropdowns and some of them helped me understand how to do. Thanks guys.
I started taking ideas from a pdf file found in the wiki pages; that document was about building related dropdowns in Sugar 4.5.1 and was written by Synolia, a french SugarCRM Recognized Developer.
I suggest to those willing to create related dropdowns to give it a look, the file is: Combos Liees dans SugarCRM V1.2.pdf
you can find it here with other documents published by Synolia:
http://www.sugarforge.org/frs/?group_id=11
Now let's see how to build two related dropdowns (I made it for EditView in Accounts module).
- build the first dropdown
- when adding new couples value-text in dropdown, use 3-chars values, like:'AAA', 'BBB', 'XXX'.
- build the second dropdown
- when adding new couples value-text in dropdown, use values having prefix = related value of dropdown one, like 'AAA_1' 'AAA_2' 'BBB_1' 'BBB_2' and so on.
- open file editviewdef.php to be used with the module you are placing the dropdowns in (in my case the module was Accounts, so I opened <sugar-root>\modules\Accounts\metadata\editviewdef.php)
- add an include to a javascript file (in my case I didn't add anything because Accounts.js was already included:
Code:
$viewdefs['Accounts']['EditView'] = array(
'templateMeta' => array(
'form' => array('buttons'=>array('SAVE', 'CANCEL')),
'maxColumns' => '2',
'widths' => array(
array('label' => '10', 'field' => '30'),
array('label' => '10', 'field' => '30'),
),
'includes'=> array(
array('file'=>'modules/Accounts/Account.js'),
),
), - append the following code in the .js file that you just included (in my case <sugar-root>\modules\Accounts\Account.js) :
Code:
var arr;
function Check() {
if(document.EditView.DropDown2_c || document.EditView.DropDown1_c) {
var DropDown2_c = document.EditView.DropDown2_c.options;
arr = new Array;
for(i=0; i<DropDown2_c.length; i++) {
arr.push(DropDown2_c[i].value, DropDown2_c[i].text);
}
}
initData();
}
function initData(){
var current_p= document.EditView.DropDown1_c;
var code_p = current_p.value;
var current_v= document.EditView.DropDown2_c;
var code_v = current_v.value;
var code_v_idx = 0;
var select_ticket = document.EditView.DropDown2_c.options;
select_ticket.length=0;
var l = 0;
for(k=0; k<arr.length; k+=2) {
if(arr[k].substr(0,3) == code_p || arr[k] == '') {
select_ticket.length++;
select_ticket[select_ticket.length-1].value = arr[k];
select_ticket[select_ticket.length-1].text = arr[k+1];
if(code_v == arr[k]){
code_v_idx = l;
}
l++;
}
}
if(code_p == ''){
select_ticket[select_ticket.length-1].value = '';
select_ticket[select_ticket.length-1].text = 'Select from DD1';
}
document.EditView.DropDown2_c.selectedIndex = code_v_idx;;
}
if (window.addEventListener)
window.addEventListener("load", Check, false)
else if (window.attachEvent)
window.attachEvent("onload", Check)
else if (document.getElementById)
window.onload=Check - Open the editviewdefs.php file where your dropdowns are (in my case <sugar-root>\custom\modules\Accounts\metadata\editviewdef s.php and locate your first dropdown:
Code:
array (
'name' => 'DropDown1_c',
'label' => 'LBL_DROPDOWN1',
),
- Add the following code after 'label':
Code:
'displayParams' => array (
'javascript' => 'onchange="initData();"'
),
- Point 8 and 9 should result in this:
Code:
array (
'name' => 'DropDown1_c',
'label' => 'LBL_DROPDOWN1',
'displayParams' =>
array (
'javascript' => 'onchange="initData();"',
),
),
That's all.
Big thanks to Synolia for publishing their document.
Thanks to jstukas for asking me about this.
What do you think the cookie monster eats ?
Bookmarks