2

Here is My Problem:

I have a New Form in a list and I have added some custom JS code to it. In the form is a "Registration number" lookup field with more than 20 items in it. I need to populate it with data(the data is correct) and then hide it so that it is invisible.

Here is The Code(or part of it):

$().SPServices.SPComplexToSimpleDropdown({
    columnName: "Registration number"
});

var RNField = getTagFromIdentifierAndTitle("select","","Registration number");

setSelectedOption(RNField,some_value);

function setSelectedOption(select, value){
    var opts = select.options;
    var l = opts.length; 
    if (select == null) return;
    for (var i=0; i < l; i++)  
        if (opts[i].text.replace(/\s/g, "") == value.replace(/\s/g, "")){     
            select.selectedIndex = i;
            $(select).closest('tr').hide();
            return true;
        }
    return false;
}

This code seems to work: it populates the field and hides it(when inspected with firebug the values are correct).

However when I add all the data and save the list item, in the list the field "Registration Number" is empty. Why is that?

What I saw:

  • $().SPServices.SPComplexToSimpleDropdown creates a new DropDown and hides the original Complex DropDown. When the data is changed in the new DropDown the data in the Complex DropDown is not changed.
  • The "Registration number" is not a required field on the form.
  • I tried to implement my own version of the SPComplexToSimpleDropdown but it still works almost the same. The field is still blank in the end.
2
  • check for the js error when you are changing SPComplexToSimpleDropdown's value , if any Aug 11, 2014 at 11:30
  • No, everything is ok there and visually it all works. I was wondering. I do not need to show the DropDown. Can I work with the input(part of the complex dropdown) instead without the SPComplexToSimpleDropdown. I tried setting its value but it still didn't work. Any ideas?
    – Gesh
    Aug 11, 2014 at 11:57

1 Answer 1

0

I found a working Solution:

First I decided to show the DropDowns (the simple ones - generated from the SPServices). On the page they are autofilled as they should be. Still the original Complex DropDowns were not populated with the data.

Then I desided to manually select (despite already being selected) data in these Simple DropDowns. What happened was that the original Complex DropDowns were filled, the data was copied to them. The item was saved and all data was ok.

I looked in the SPServices.js, found the code for the SPComplexToSimpleDropdown and found out that it attaches an event to the new Simple DropDowns (event with name "change"). However when I populate those Simple DropDowns via code, I just change the "Selected index" of the Simple DorpDown and this action does not trigger the "change" event.

What you have to do to make it work is to just trigger the event manually, after filling the Simple DropDowns. Like this:

...
for (var i=0; i < l; i++)  
        if (opts[i].text.replace(/\s/g, "") == value.replace(/\s/g, "")){     
            select.selectedIndex = i;

            $(select).trigger("change");//manually trigger the event

            $(select).closest('tr').hide();
            return true;
        }
    return false;
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.