Prepopulate Multi Select in form after SPFilterDropdown

Jun 15, 2012 at 2:18 PM
Edited Jun 15, 2012 at 4:17 PM

 

$(document).ready(function(){
	var currentUserTitle = $().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false});
	
	$().SPServices.SPFilterDropdown({
  		relationshipList: "{E88EBD45-C352-49D9-9A79-58671D1404F4}",
  		relationshipListColumn: "Title",
  		columnName: "Living Spaces",
  		CAMLQuery: "<Eq><FieldRef Name='Seller'/><Value Type='User'>"+currentUserTitle +"</Value></Eq>",
		completefunc: null,
  		debug: false
	});

	var select = getTagFromIdentifierAndTitle("select","SelectCandidate","Living Spaces possible values");
	select.selectedOptions=select.children;
	GipAddSelectedItems(ctl00_m_g_88d67ac2_88a5_453b_bd21_a31c5ea94ebd_ff31_ctl00_ctl00_MultiLookupPicker_m);
	
	
});

I have the SPFilterDropdown working properly. But now I want to prepopulate the multiselect with every option that made it through the filter. I tried brute forcing it by just changing the selectedOptions of my select element to the children but it doesn't seem to reflect that change in the form. GipAddSelectedItems() is the function I found attached to the Add button. That adds all selectedOptions from the SelectCandidate select element over to the SelectResult select element. Below was my first attempt at prepopulating the multi select.

        var select = getTagFromIdentifierAndTitle("select","SelectCandidate","Living Spaces possible values");
	var l=select.options.length;
	for(var i =0; i<l;i++){
		
		select.options.selectedIndex=i;
		GipAddSelectedItems(ctl00_m_g_88d67ac2_88a5_453b_bd21_a31c5ea94ebd_ff31_ctl00_ctl00_MultiLookupPicker_m);
	}
However, it would only place some of the values from the SelectCandidate element and move it over to the SelectResult element, while the rest were left behind. The problem was that in the middle of the loop, it would sometimes update my select variable so the latter half of the loop doesn't modify any values. I changed it to this.  
        var select = getTagFromIdentifierAndTitle("select","SelectCandidate","Living Spaces possible values");
	var l=select.options.length;
	for(var i =0; i<l;i++){
		select = getTagFromIdentifierAndTitle("select","SelectCandidate","Living Spaces possible values");
		select.options.selectedIndex=0;
		GipAddSelectedItems(ctl00_m_g_88d67ac2_88a5_453b_bd21_a31c5ea94ebd_ff31_ctl00_ctl00_MultiLookupPicker_m);
	}	

Now it works, but I also have a small function (the function call isn't shown in this code) to set the multi select to read only. The problem is that it takes a good 1 or 2 seconds before the values are swapped over and the viewer has temporary write privileges. That is why I tried the method I first posted because I thought it would work faster. I've tried each solution with _spBodyOnLoadFunctionNames.push(); as well as $(document).ready(function(){});
Coordinator
Jun 22, 2012 at 1:01 PM

Take a look at the SPCascadeDropdowns function in SPServices. You can see how I manage things there, which may help.

M.

Feb 6, 2014 at 10:11 PM
I am doing something similar, but my problem is that one of my users is using SPCascadeDropdowns and it's not working with the custom calls.

Basically, I want to put a nicer looking multi-select field to replace the LookUp field (with multiple values allowed). My way to do this is to create a new select with better UI, and call functions like GipAddSelectedItems when the user checks/unchecks items. The out of box fields are hidden, although they are still being used in the background by SharePoint and functions like GipAddSelectedItems. This all works perfectly on its own, but it seems to not trigger something that SPCascadeDropdowns requires -- nothing happens to the 'child' field when I add/remove items in the 'parent' field. The SPCascadeDropdowns functions fine when the custom UI code is removed.. they just don't get along together.

Any ideas? My guess is something isn't triggering.. how does SPCascadeDropdowns keep track of changes to a parent field? Let me know if this needs more clarification.

-Steven
Feb 7, 2014 at 5:08 PM
To answer my own question (sort of), I found that calling the click function on the Add button rather than trying to do what the Add button does solved this. Weird thing though, calling click once is sufficient for the LK to move from the source to destination list, but the click needs to be called again for the cascade child field to be updated. Literally I am doing something like myAddButton.click(); myAddButton.click();

It works, so I'm not complaining too much.
Coordinator
Feb 7, 2014 at 6:01 PM
You probably can switch to myAddButton.dblclick(); if you're dealing with the multi-selector control. That's the event which causes a value to move from left to right or vice versa.

M.
Feb 7, 2014 at 8:00 PM
Edited Feb 7, 2014 at 8:06 PM
I thought the same, I tried it and it complained that the object doesn't support that method. I may have overlooked something, but I didn't play with it too much. My guess was that SPCascadeDropdowns hooks onto the dblclick event and somehow two clicks in a row gets interpreted as a double click. It's confusing and weird.

Note that myAddButton is the actual Add button object used with the multi LK field, not to be confused with the source list object (SelectCandidate) which relies on the user double clicking an item. So as a button, I guess it wouldn't have a dblclick method. Even stranger, part of the underlying behavior by SharePoint is to disable the Add button after clicking it, so it's like the second click() is with a disabled button..
Coordinator
Feb 7, 2014 at 8:31 PM
Ah, I see what you mean. I was thinking of the dblclick on the data values.

Well, if it works, it works, I guess!

M.