misc-code-samples/easydependentpicklists.js
/** Applies the filtering to the dependent picklist hierarchy that contains a particular attribute. */
Applies the filtering to the dependent picklist hierarchy that contains a particular attribute.
Applies the filtering to the dependent picklist hierarchy that contains a particular attribute .
function applyFilters(callingControlName, executionContext){
	var currentFilteredPicklist = getFilteredPicklist(callingControlName, _filteredPicklists);
	if(!currentFilteredPicklist){
		console.log("Error");
		return;
	}
	var callingControlName = executionContext.getEventSource().getName();
	var currentFilteredPicklist = getFilteredPicklist(callingControlName, _filteredPicklists);
	if(!currentFilteredPicklist){
		console.log("Error");
		return;
	}
	var hierarchy = currentFilteredPicklist.hierarchy;
	var optionTree = currentFilteredPicklist.optionTree;
	var separator = currentFilteredPicklist.separator;
	var startinglevel = 0;
	var selectedOptions = [hierarchy.length];
	for(var i=startinglevel;i<hierarchy.length;i++){
		var picklistName = hierarchy[i];
		var attribute = Xrm.Page.getAttribute(picklistName);
		var existingattributeoptions = attribute.getOptions();
		if(i<hierarchy.length-1){
			attribute.removeOnChange(filterOptions);
		}
		var selectedOption = Xrm.Page.getAttribute(picklistName).getSelectedOption();
		selectedOptions[i]=selectedOption;
		if(i>0){
			attribute.controls.forEach(function (control) {
				control.clearOptions();
				control.setDisabled(true);
			}); 
		}
	}
	setTimeout(function(){
		var workingPath = "";
		for(var i=startinglevel;i<hierarchy.length-1;i++){
			var picklistName = hierarchy[i];
			var childPicklistName = hierarchy[i+1];
			var selectedOption = selectedOptions[i];
			var selectedChildOption = selectedOptions[i+1];
			if(selectedOption){
				if (Xrm.Page.context.client.getClient() == "Mobile") {
					Xrm.Page.getAttribute(childPicklistName).controls.forEach(function (control) {
						control.addOption({ text: "", value: -1 });
					});
				}
				var selectedText = selectedOption.text;
				workingPath = workingPath + selectedText + separator;
				var alreadyAdded = [];
				optionTree.forEach(function(optionPath){
					if(optionPath.indexOf(workingPath)==0) {
						var pathComponents = optionPath.split(separator);
						var optionTextToAdd = pathComponents[i+1];
						var selectedOptionPresent = false;
						var optionToAdd = getOptionByText(Xrm.Page.getAttribute(childPicklistName).getOptions(),optionTextToAdd);
						if(optionToAdd){
							if(alreadyAdded.indexOf(optionToAdd.text)<0){
								Xrm.Page.getAttribute(childPicklistName).controls.forEach(function (control) {
									control.addOption(optionToAdd);
									control.setDisabled(false);
								});
								alreadyAdded.push(optionToAdd.text);
								if(selectedChildOption){
									if(optionToAdd.text==selectedChildOption.text){
										selectedOptionPresent = true;
									}
								}
							}
						}
						if(selectedOptionPresent){
							Xrm.Page.getAttribute(childPicklistName).setValue(parseInt(selectedChildOption.value));
						}
					}
				});
			}
		}
		for(var i=startinglevel;i<hierarchy.length-1;i++){
			Xrm.Page.getAttribute(hierarchy[i]).addOnChange(filterOptions);
		}
	},_globalDelay);
}
function applyFilters ( callingControlName , executionContext ) { var currentFilteredPicklist = getFilteredPicklist ( callingControlName , _filteredPicklists ) ; if ( ! currentFilteredPicklist ) { console . log ( "Error" ) ; return ; } var callingControlName = executionContext . getEventSource ( ) . getName ( ) ; var currentFilteredPicklist = getFilteredPicklist ( callingControlName , _filteredPicklists ) ; if ( ! currentFilteredPicklist ) { console . log ( "Error" ) ; return ; } var hierarchy = currentFilteredPicklist . hierarchy ; var optionTree = currentFilteredPicklist . optionTree ; var separator = currentFilteredPicklist . separator ; var startinglevel = 0 ; var selectedOptions = [ hierarchy . length ] ; for ( var i = startinglevel ; i < hierarchy . length ; i ++ ) { var picklistName = hierarchy [ i ] ; var attribute = Xrm . Page . getAttribute ( picklistName ) ; var existingattributeoptions = attribute . getOptions ( ) ; if ( i < hierarchy . length - 1 ) { attribute . removeOnChange ( filterOptions ) ; } var selectedOption = Xrm . Page . getAttribute ( picklistName ) . getSelectedOption ( ) ; selectedOptions [ i ] = selectedOption ; if ( i > 0 ) { attribute . controls . forEach ( function ( control ) { control . clearOptions ( ) ; control . setDisabled ( true ) ; } ) ; } } setTimeout ( function ( ) { var workingPath = "" ; for ( var i = startinglevel ; i < hierarchy . length - 1 ; i ++ ) { var picklistName = hierarchy [ i ] ; var childPicklistName = hierarchy [ i + 1 ] ; var selectedOption = selectedOptions [ i ] ; var selectedChildOption = selectedOptions [ i + 1 ] ; if ( selectedOption ) { if ( Xrm . Page . context . client . getClient ( ) == "Mobile" ) { Xrm . Page . getAttribute ( childPicklistName ) . controls . forEach ( function ( control ) { control . addOption ( { text : "" , value : - 1 } ) ; } ) ; } var selectedText = selectedOption . text ; workingPath = workingPath + selectedText + separator ; var alreadyAdded = [ ] ; optionTree . forEach ( function ( optionPath ) { if ( optionPath . indexOf ( workingPath ) == 0 ) { var pathComponents = optionPath . split ( separator ) ; var optionTextToAdd = pathComponents [ i + 1 ] ; var selectedOptionPresent = false ; var optionToAdd = getOptionByText ( Xrm . Page . getAttribute ( childPicklistName ) . getOptions ( ) , optionTextToAdd ) ; if ( optionToAdd ) { if ( alreadyAdded . indexOf ( optionToAdd . text ) < 0 ) { Xrm . Page . getAttribute ( childPicklistName ) . controls . forEach ( function ( control ) { control . addOption ( optionToAdd ) ; control . setDisabled ( false ) ; } ) ; alreadyAdded . push ( optionToAdd . text ) ; if ( selectedChildOption ) { if ( optionToAdd . text == selectedChildOption . text ) { selectedOptionPresent = true ; } } } } if ( selectedOptionPresent ) { Xrm . Page . getAttribute ( childPicklistName ) . setValue ( parseInt ( selectedChildOption . value ) ) ; } } } ) ; } } for ( var i = startinglevel ; i < hierarchy . length - 1 ; i ++ ) { Xrm . Page . getAttribute ( hierarchy [ i ] ) . addOnChange ( filterOptions ) ; } } , _globalDelay ) ; }
Applies the filtering to the dependent picklist hierarchy that contains a particular attribute.
Applies the filtering to the dependent picklist hierarchy that contains a particular attribute .
//get the filteredpicklist object that contains the calling picklist //get the name of the picklist that just changed //get the filteredpicklist object that contains the calling picklist //get the filteredpicklist values //we traverse the entire hierarchy right now. should come back and only go from what has changed->down - lpa 2016-09-26 //create an array to hold the selected options for each picklist in the hierarchy //loop through the hierarchy to get the selected options, clear child options and temporarily disable the onchange event for each picklist //get the picklist name //temporarily remove onchange event for every picklist except the last child //get the currently selected option and store it for use later (will be null if nothing is selected) //clear the picklist options for every child picklist //disable child controls until we put something in them //set a delay because IE is stupid - this is not necessary in Chrome! - lpa 2016-09-26 //represents the full path from first parent to last child - start with empty value and will build out by looping //loop through all parent picklists to set the corresponding child options //get the picklist name //get the childpicklist name //get the currently selected option //get the currently selected child option //if the currently selected option is null, we can stop //include an "empty" option for mobile clients //get the text for the selected option //build up the working path with this level's selected option text //instantiate an array to keep track of options already added so we don't add duplicates //find options in the optiontree array that start with the workingpath value //figure out which level we are in and find the corresponding options to add to the appropriate picklist //assume the previously selected child option is not present in our "new" picklist options //see if there is valid corresponding option in the child picklist to add //if we are adding an option, enable it //if previously selected child option is present in our "new" picklist options, set flag //if we had a selected option in the picklist before we started, set it back //add onchange eventhandler back to picklists in this hierarchy