SPCascadeDropdowns using simpleChild

Dec 28, 2011 at 9:56 PM

Hello Marc,

 

So I have done quite a bit of 'homework' and come up with an interesting problem.  This problem only exists in Internet Explorer ( I use IE8 so I am unsure of other versions).  With the setup below after the cascaded columns are converted from complex to simple dropdowns the original values are not being reassigned appropriately.  I have come up with a simple workaround however I still run into another wall.  After I assign the 'original' value the next column choices are not being updated.  Is there a way that I could trigger the same events which are triggered when users select the dropdown options?  I.e.  I am using jQuery to hook into the dropdown element and assigning a new value

var originalValue = /*result from a function which returns an int*/;
$('select[title="Ticket SubCategory1"]').val(originalValue);

After this assignment I want to update the next dropdown field with the appropriate choices

 

Here is the environment setup...

<script language="javascript" type="text/javascript" src=" /_layouts/jQuery/jquery-1.6.2.min.js"></script>
<script language="javascript" type="text/javascript" src=" /_layouts/jQuery/jquery.SPServices-0.7.0.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
        $().SPServices.SPCascadeDropdowns({
		relationshipList: "CategoriesSub1",
		relationshipListParentColumn: "Category",
		relationshipListChildColumn: "Title",
		parentColumn: "Ticket Category",
		childColumn: "Ticket SubCategory1",
		simpleChild: true
	});
	$().SPServices.SPCascadeDropdowns({
		relationshipList: "CategoriesSub2",
		relationshipListParentColumn: "CategorySub1",
		relationshipListChildColumn: "Title",
		parentColumn: "Ticket SubCategory1",
		childColumn: "Ticket SubCategory2",
		simpleChild: true
	});

        var subOne = $('select[title="Ticket SubCategory1"]');
	subOne.val(getOriginalValue(subOne, subOneVal));
	//alert(subOne.val());
	
	var subTwo = $('select[title="Ticket SubCategory2"]');
	subTwo.val(getOriginalValue(subTwo, subTwoVal));
	//alert(subTwo.val());

	function getOriginalValue(dropdown, originalValue){
		var value = 0;
		$(dropdown).find('option').each(function(){
			if($(this).text() == originalValue){
				value = $(this).val();
			}
		});
		return value;
	}
});
</script>

Coordinator
Dec 28, 2011 at 10:00 PM
You shouldn't have to do any workaround if the function is working correctly. It may be that you've stumbled on a bug. Can you explain what you are seeing that means it isn't assigning the values properly?

M.
Dec 28, 2011 at 11:15 PM

Well,

Thank you again Marc!!!

I was looking through SPServices under $.fn.SPServices.SPComplexToSimpleDropdown and saw this :)

// Trigger a change to ensure that the selected value registers in the complex dropdown
$("#" + simpleSelectId).trigger("change");
appending the .trigger("change"); to the end of the val() assignment fixed my smaller problem.  Still unsure why the value assignment fails in IE 8 for me.

Dec 28, 2011 at 11:20 PM
sympmarc wrote:
You shouldn't have to do any workaround if the function is working correctly. It may be that you've stumbled on a bug. Can you explain what you are seeing that means it isn't assigning the values properly?

M.

Well I have 3 columns, Ticket Category, Ticket SubCategory1 and Ticket SubCategory2 that cascade in that order.  Both the SubCat1 and SubCat2 have more than 20 items so they both need to be converted to the simple dropdown.  When the page loads before $().SPServices.SPCascadeDropdowns is called the values are there.  But after that function runs SubCat1 and SubCat2 values are being left to the default option 0 (Select + Column Name...).  This only happens in IE8.  If I use firefox or chrome the function executes properly and the original values are assigned to the new simple dropdown columns.

Dec 30, 2011 at 12:02 AM
Edited Dec 30, 2011 at 12:03 AM

Okay so a really interesting update.  I removed the simpleChild option from the SPCascadeDropdown function and simply called the SPComplexToSimpleDropdown on the child column and it works!!!  The selected option is reset after the page loads.  Here is what I have now...

 

$(document).ready(function(){
	$().SPServices.SPCascadeDropdowns({
		relationshipList: "SubCat1",
		relationshipListParentColumn: "Categories",
		relationshipListChildColumn: "Title",
		parentColumn: "Ticket Category",
		childColumn: "Ticket SubCategory1",
		debug: true
	});
	$().SPServices.SPCascadeDropdowns({
		relationshipList: "SubCat2",
		relationshipListParentColumn: "SubCat1",
		relationshipListChildColumn: "Title",
		parentColumn: "Ticket SubCategory1",
		childColumn: "Ticket SubCategory2",
		debug: true
	});
	$().SPServices.SPComplexToSimpleDropdown({  
		columnName: "Ticket SubCategory1"
	});
	$().SPServices.SPComplexToSimpleDropdown({  
		columnName: "Ticket SubCategory2"
	});
});

Also a work friend of mine suggested a small change in the SPComplexToSimpleDropdown function.  
He added an if statement while you are creating the simple dropdown that compares the original 
value and sets that one to selected rather than going through the trouble of using jQuery
to hook back into that dropdown.
Here is how it looks:
// Build up the simple dropdown, giving it an easy to select id
var simpleSelectId = genContainerId("SPComplexToSimpleDropdown", opt.columnName);
var simpleSelect = "<select id='" + simpleSelectId + "' title='" + opt.columnName + "'>";	
for(i=0; i < choices.length; i=i+2) {
	if(columnSelectSelected == choices[i]){
		simpleSelect += "<option value='" + choices[i+1] + "' selected='selected'>" + choices[i] + "</option>";
	}else {
		simpleSelect += "<option value='" + choices[i+1] + "'>" + choices[i] + "</option>";
	}
}
simpleSelect += "</select>";

Coordinator
Dec 30, 2011 at 2:15 PM

_JT_:

I'm glad you got something working, but I still think I have a bug. I've got several other reports of issues with this function. I'll let you know when I have a fix to try.

The reason I set the selected options after I create the select's options is that there could be multiple options with the same value. (I know that this violates database normalization rules, but people do it all the time.) SharePoint always selects the first matching *text*, which is what I'm trying to mimic. However, enough people have pointed that out as a problem that I am going to add an option to match on the ID. That should make things work better for those who are savvy enough to want it.

M.

Coordinator
Dec 30, 2011 at 3:41 PM

Please try the alpha I've just posted and see if it fixes your issues.

Thanks a lot for sticking with me on this.

M.