Setting dropdown value when using SPComplexToSimpleDropdown

Feb 4, 2015 at 11:16 PM
Hi,

I've been trying to work out this issue for some time. Let me know if I missed something simple. Otherwise, here's what I encountered and my eventual solution.

We have a lookup list of vendors that is ~300 items long. SPComplexToSimple saved us a lot of trouble of selecting the desired vendor quickly. Now my goal is to auto-select a specific, popular vendor on form load - just to save the user a few seconds, as we use this vendor 90% of the time.

I've tried a lot of variations of setting the value and none of them worked except the find.each system used below. (I did not want to filter the dropdown because other items needed to stay visible).

However, a new problem showed up: SharePoint did not save the changed field value after form submission. Instead, the column showed the first option on the list.

After reading your documentation and your mention of the change event, I finally guessed that it was not being triggered after my code. I added the .change() to the select element, and voila, it finally worked.

Is there something simple that I overlooked? Have you had success with setting a dropdown value of a field created by SPComplexToSimple? Either way, I wanted to post here in case others were looking for a solution to a similar issue.
$(document).ready(function() {
       $("select[title='Vendor']").find('option').each(function(){
               this.selected = $(this).text() == "Main vendor";
       });
       $('#SPComplexToSimpleDropdown_Vendor').change();
});
Thanks for all your work!
Feb 4, 2015 at 11:46 PM
Instead of making the <option> selected attribute true, try instead to set the Select Value and then trigger the change event (I think mark listens to that event to set the underlying "real SharePoint control. Like:

if($(this).text() == "Main vendor"){
$("select[title='Vendor']").val($(this).val()).change();
}



Paul
-- sent from mobile
Marked as answer by Trikes120 on 2/6/2015 at 5:34 AM
Feb 5, 2015 at 1:07 PM
I have done something similar in the past also, but did not need to use the change. I went to the list, used for the drop-down, looked up the ID of the list item that I wanted to default the form too, and then I checked after load, that if the drop down was showing the first value '0', which is '(None)', when you do not have it as a required field, and set it's value to the ID of the item I wanted as the default. I knew that my form would never use the '(None)' if it was a particular type of uploaded document, so there was no fear of the script resetting the value. Had I configured the column as required, their would have been the possiblity of the first value actually being used, which would mean you might want to enter a dummy list item, that would appear as your first item in the look-up for the drop-down(if you used the IF condition like I did). As I had configured the library to enforce check-in/out, the change was saved on check-in, by the user.
    if($("select[title='Credit Status']").val() == 0){
        $("select[title='Credit Status']").val(14);
    } 
Feb 5, 2015 at 10:03 PM
JSdream, I tried setting just the val property, and while it does change it on the screen, it did not save the value after form submission. The .change() appears necessary for me to get this to work.

So, I learned something new from you guys and my new solution combines elements from both ptavares and JSdream.
Thank you both.
  1. Skip the find('option').each() entirely, probably saving a few milliseconds.
  2. Look up the item ID of the desired vendor in the lookup list
  3. Set the select value using val
  4. Invoke the change event
Really straight forward now.
$(document).ready(function() {
     //Get List ID in lookup list, set, and apply the change.
     //Main Vendor list ID is 25.
     $("select[title='Vendor']").val(25).change();
});
Marked as answer by Trikes120 on 2/6/2015 at 5:34 AM
Feb 6, 2015 at 7:40 PM
That's great Trikes! The only potential problem I see with your solution, is that unless this is placed within some sort of condition, each time the edit properties is invoked on the document, it will always reset that Vendor drop-down to the Main Vendor. So you could have a situation where someone chooses a different vendor, and saves the change, but next time the edit properties is invoke, it will then change the Vendor select, back to the Main vendor. You might want to give thought to this.
Feb 6, 2015 at 8:59 PM
I see what you're saying. The script is bound to the NewForm.aspx only (a CEWP underneath); we do not put the same auto-fill code like that on the EditForm.aspx. That is why we shouldn't need the condition around it. I think :)

(No issues so far... but who knows, maybe we'll hear a complaint soon).
Coordinator
Feb 7, 2015 at 5:14 PM
Edited Feb 7, 2015 at 5:15 PM
Sorry I missed this thread until now.

I take care of firing the underlying change event for the complex dropdown, which I hide when you call SPComplexToSimpleDropdown.

So you'll want to set the dropdown after your call to SPComplexToSimpleDropdown, because that's a lot easier.

I would probably do this:
$(document).ready(function() {
  $().SPServices.SPComplexToSimpleDropdown({    
    columnName: "Vendor"
  });
  var newDropdown = $("select[title='Vendor']");
  newDropdown.find("option").filter(function() {
    return $(this).text() === "Main vendor";
  });
  newDropdown.change();
});
I haven't tested this, but I think it's right. If I returned a reference to the new dropdown from SPComplexToSimpleDropdown, that would sure be helpful, eh?

M.