Stumbling around with dropdowns

Coordinator
Nov 4, 2009 at 6:37 AM

Guys,

I have spent the last two days trying to wrap my head around jPoint/jQuery. I am now down to the wire on something I am supposed to have already completed. I need help (pointers will be good) on how to build a new choice list on a form. I guess my javascript is just not that strong. I really thought that jP.Form.MyList.val(json) would do it, but it doesn’t. Please help.

B

Coordinator
Nov 4, 2009 at 6:37 AM
Edited Nov 4, 2009 at 6:42 AM

B,

The jPoint information you need is how to get a reference to the dropdown object which is a select node.  The jP.Form.MyList.Item is a reference to the cell which is a span which contains all of Microsoft Sharepoint's 'stuff'.  To get the select, use the following code:

var myListDropDown = $(jP.Form.MyList.Item).find("select:visible").get(0);


Now, here is the jQuery or javascript information you need to finish the solution.  Let's say your jSON code looks like a set of countries.  Here is how you would add the jSONCountryCode object to your dropdown list.

var jSONCountryCode = { "Belgium": 103, "China": 106 };
var myListDropDown = $(jP.Form.Status.Item).find("select:visible").get(0);
  $.each(jSONCountryCode, function(ky, val) {
       myListDropDown.options[myListDropDown.options.length] = new Option(ky, val);
  }); 

This adds the jSON data to the dropdown.  If you wanted to replace the current dropdown options with the jSON data, then clear the list before the $.each loop with myListDropDown.options.length=0. 

I hope this helps.

Will
Coordinator
Nov 4, 2009 at 6:38 AM

B,

Oh, I forgot to add, .val() for an option list can only select an existing value.  If the column has "Specify other value" enabled, then jPoint would try to fit any value that does not match an existing option into the "Specify other value" field.  Your need to add new options on the fly is a new requirement that we may need to consider. It may change the integrity of the choices, but so does allowing "Specify other value". 

Good luck with your work.
Will

Nov 4, 2009 at 6:48 AM

Will, In talking to Samir I found the terrific script (actually hiding in the examples) called coverttodropdown.js which builds a drop-down list from a JSON. Changing the list attributes to a simple text field and then building the drop-downs from other list data on the fly is just the ticket for me. I have several dependent lists that I am now building "on the fly". This seems to be a much safer method and allows virtually unlimited extensibility.

Bill