SPCascadeDropdowns: autoselect first item of child dropdown

Jan 6, 2010 at 2:08 PM

Hello!

At first I would like to thank you for sharing this library!

Unfortunately I was not able to solve this at my own since my JScript and CAML is poor.

Hopefully my concern is clear.

For further explanation I will pick up the example of the documentation and demo page:

- If I select the Region "Mountain" for example, then the State "Colorado" (first item) should automatically be selected.

(In my case parent->child is an n:1 relation)

Thanks for your help in advance.

Coordinator
Jan 6, 2010 at 2:46 PM

I'm happy to try to help, but I'm not sure I understand your question.  Is the issue that the dropdown says "Choose State..."?

M.

Jan 7, 2010 at 6:40 AM

Yes you are right. "Choose State..." is not necessary because there is exactly one child (State) in each Region. I know that's not true, but in my case it is.
Instead of saying "Choose State..." the dropdown should show the first (and only) State. 

I tried to comment out the following in line 875:

// Add an explanatory prompt
				switch(childSelect.Type) {
					case "S":
						//childSelect.Obj.attr({ length: 0 }).append("<option value='0'>Choose " + opt.childColumn + "...</option>");
But this caused unexpected behavihour.

Jan 7, 2010 at 8:41 AM

Hi Falker

A solution to your problem may be provided by this function: $.fn.SPServices.SPCascadeDropdownsEx described in Few Extension, available for download here.

When calling this function, you should set autoChoice parameter to "OnlyOne" or "First".

It was written for version 0.4.2, but it should work with version 0.4.7 or at leas be easy to rewrite.

J.

Jan 7, 2010 at 10:05 AM

Hi nidhogg and sympmarc!

Thanks to you both.

The autoChoice option does exactly what i need.

 

Coordinator
Jan 7, 2010 at 12:42 PM
Edited Jan 7, 2010 at 1:37 PM

Glad you got things working!

I think I'm going to add an option to SPCascadeDropdowns that works more like this:

promptText: "Choose {0}...",  // [Optional] Text to use as prompt. If included, {0} will be replaced with the value of childColumn.

If the value of promptText is empty (zero length), then I won't display a prompt at all.  This will, in effect, make the first value selected.

I think that this will cover all of the use cases that I can think of.  Am I missing anything?  It seems like these two options:

  • First - automatically choose first element
  • OnlyOne - automatically choose element if it's the only one

would accomplish the same thing.

M.

Jan 7, 2010 at 4:02 PM

Sounds very good sympmarc!

nidhogg's extension has a little disadvantage:
it isen't possible to use the auto-suggest function anymore.

 

Coordinator
Jan 7, 2010 at 6:35 PM

I've already got what I outlined above working in v0.4.8ALPHA6.  I hope to be posting that later today if I can work out a different issue that I'm having with one of the enhancements I'm working on for $().SPServices.SPScriptAudit.

M.

Jan 13, 2010 at 10:18 AM

Hello sympmarc,

unfortunately the new option

promptText: "", 

to automatically make the first value selected, does not work with "20+ options childColumn-dropdowns".

Are you willing to fix this issue? :-) I would appreciate it very much, because i have to go live, with our SP ticket system today.

Thanks
Falker

 

Coordinator
Jan 13, 2010 at 11:32 AM
Edited Jan 13, 2010 at 11:32 AM

Right. With 20+ options, you should be seeing "(None)" as the first option, which is consistent with what SharePoint shows.  What's your suggestion?

M.

Coordinator
Jan 13, 2010 at 1:32 PM

Falker:

I thought about this a little more and I've got a different fix in place.  If the childColumn is required, then I shouldn't be showing any additional prompt, just like SharePOoint does.  What I mean is that SharePoint will only show the "(None)" option if the column is not required.  I'm now mirroring this logic.  The same will apply to <20 option dropdowns: if required, then no prompt, regardless what you specify in the promptText option.

I can ship you this for you to try if you want it.  Let me know how best to get it to you. You'll be a pre-alpha on this, of course, though it tests fine in my test harness.

M.

Jan 13, 2010 at 5:21 PM
Edited Jan 13, 2010 at 5:26 PM

Thank you. You can upload the pre-alpha here http://upload.arturh.de/

To react on the required mark sounds good. Please consider, that the first value of the child dropdown should be auto-selected when choosing the parent value.
I will test it with your pre-alpha and give you a feedback...

 

edit:
The 20+ options dropdown is shown even when the dropdown holds only one value. Crucial therfore is the number of entrys in the relationshipList.

 

Coordinator
Jan 13, 2010 at 7:39 PM

Your edit is actually a new thing, and inconsistent with what SharePoint does by default so I haven't made that change.

I'm going to post this new version as v0.4.8ALPHA8 so that others will have access to it as well.

Give it a try and let me know how it goes...

M.

Jan 14, 2010 at 8:57 AM

Hi!

I've tested with v0.4.8ALPHA8 now. This is what i did:

<script type="text/javascript">
				    $(document).ready(function() {
				        $().SPServices.SPCascadeDropdowns({
				            relationshipList: "Relations",
				            relationshipListParentColumn: "Port",
				            relationshipListChildColumn: "Title",
				            parentColumn: "Port of destination (UN-Locode)",
				            childColumn: "Relation",
				            promptText: ""
				        });
				    });
	</script>
	<script type="text/javascript">
				    $(document).ready(function() {
				        $().SPServices.SPCascadeDropdowns({
				            relationshipList: "Relations2",
				            relationshipListParentColumn: "Port",
				            relationshipListChildColumn: "Title",
				            parentColumn: "Port of destination (UN-Locode)",
				            childColumn: "Relation <20",
				            promptText: ""
				        });

And the behavior is as follows, when choosing the Port of destination "Abu Dhabi" (for example):

SPCD

Am I wrong, if I think the "Relation" dropdown should show the value "Africa, Europe..." automatically, like the "Relation <20" dropdown does? Or is this not possible?

Best regards
Falker

Coordinator
Jan 14, 2010 at 11:41 AM

You're not wrong to want it to work that way, but that isn't the way SharePoint's control works by default.  If you turn off all of the jQuery, you'll see that nothing is selected in the Relation column on the page load.  Since the <20 column uses a plain old select, the first value is show (but not actually selected).

However, since the Relation column is showing as required, you shouldn't see the "(None)" option.  Are you sure you're using v0.4.8ALPHA8?

M.

Jan 14, 2010 at 12:46 PM

Yep, I definitely using v0.4.8ALPHA8.

sympmarc wrote:

... but that isn't the way SharePoint's control works by default.

I don't like this default behavior ;-) I guess I have to learn some javascripting then. Is there somthing like

DropDownList.SelectedIndex == 1;

 

sympmarc wrote:

if you turn off all of the jQuery, ....  Since the <20 column uses a plain old select, the first value is show (but not actually selected).

I think the value is not only shown but also selected. The value will be saved when clicking OK anyway.

Coordinator
Jan 14, 2010 at 1:07 PM

Crap. I just posted a big long reply and it got lost when I submitted. Here's the short version...

I'll look at adding something like Nidhogg's autoChoice idea into the function.  I probably won't get to it today, though.

If you're seeing the "(None)" option on a required column with v0.4.8ALPHA8, then my fix isn't working.  I'll also look at that some more, but it seems to be working fine in my test environment.

M.

Jan 14, 2010 at 3:13 PM
sympmarc wrote:

Crap. I just posted a big long reply and it got lost when I submitted. Here's the short version...

damn... I know the feeling...

sympmarc wrote:

I'll look at adding something like Nidhogg's autoChoice idea into the function.  I probably won't get to it today, though.

Thats great. I'am looking forward to this.

Thank you
Falker

Coordinator
Jan 19, 2010 at 4:03 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.