SPComplexToSimpleDropdown not selecting correct value in some cases

Dec 14, 2011 at 3:48 PM
Edited Dec 14, 2011 at 3:52 PM

Hey Marc, I've been using the SPServices library for several months now and I love it. I'm using 0.6.2 and I took a crack at utilizing the SPComplexToSimpleDropdown function. Everything was working great with it until I ran across an issue where the selected value was "Reports", but the function was choosing "Forms & Reports" which was a bit higher in the list. I looked at the code and found that the :contains selector was the cause of my problems.

 

// Set the selected value, matching the way SharePoint does it: first match
$("#" + simpleSelectId).find("option:contains('" + columnSelectSelected + "'):eq(0)")
.attr("selected", "selected");

 

I was able to change this to the following and got it to work as expected.

 

// Set the selected value, matching the way SharePoint does it: first match
$("#" + simpleSelectId).find("option[text='" + columnSelectSelected + "']):eq(0)")
.attr("selected", "selected");

 

The only change is using an equals selector rather than a contains.

I was a little hesitant to just keep that and cross the 0.7.0 bridge when I came to it, so I went ahead and downloaded the new code to see if it had been fixed. I see that the function has been updated in 0.7.0, but it's not working (at least in my scenario). Here's what the section in question looks like in 0.7.0:

 

// Set the selected value, matching the way SharePoint does it: first match
$("#" + simpleSelectId).find("option").filter(function() { 
	return $(this).text() == columnSelectSelected; 
}).find(":first").attr("selected", "selected");

 

The problem is that you're calling .find(":first") on an element that doesn't have any descendents, so everything returns null and nothing is selected. The only change that needs to be made on this function for it to work in my case is changing .find(":first") to .filter(":first").

 

// Set the selected value, matching the way SharePoint does it: first match
$("#" + simpleSelectId).find("option").filter(function() { 
	return $(this).text() == columnSelectSelected; 
}).filter(':first').attr("selected", "selected");

 

Making those changes in both versions fixed my problems, so I thought I'd share for anybody else experiencing something similar.

P.S. I'm using jQuery 1.4.2.

Coordinator
Dec 30, 2011 at 2:41 PM

jmduchnowski:

You're spot on with your analysis on this. Thanks for the work on it.

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

Thanks!

M.