+ Start a Discussion
SaintMichaelSaintMichael 

re-order items in a Set<String>

I have a Set<String> called selectedDesignations.

I have a select list that when a user clicks 'move up' or 'move down', the item is re-positioned within the list.

 

By using debug statements, I can see that the list is getting re-ordered the way I need it to.

The problems is re-Rendering the list, it doesnt want to re-paint.

 

I have had this problem before, and I can't really tell how I solved it. Previously I had some ajax activity that would not fire off. I moved a few pageBlock items and panels around and then it worked. So, not really a realiable solution.

 

Any ideas why I cannot get the outputpanel id = "selected" and the select list id = "selected_designation_list" to re-Render properly?

 

Code:

 

<apex:pageBlockSection title="Designations" collapsible="false" id="des">

	<apex:actionRegion >
		<table>
		<tr>
		<td><apex:outputPanel id="available">
		<apex:selectList id="unselected_designation_list" required="false" value="{!highlightedDesignations}" multiselect="true" size="10">

		<apex:selectOptions value="{!unSelectedOptionsDesignations}" />
		</apex:selectList>
		</apex:outputPanel>
		</td>
		</tr>



		<tr>
		<td><apex:outputPanel id="buttons">
		<apex:commandButton value="Add" action="{!doSelect}" reRender="available,selected">
		<apex:param name="designationListName" value="designations" assignTo="{!listName}" />

		</apex:commandButton>
		<br />
		<apex:commandButton value="Remove" action="{!doUnSelect}" reRender="selected,available">
		<apex:param name="designationListName" value="designations" assignTo="{!listName}" />
		</apex:commandButton>
		</apex:outputPanel>
		</td>
		</tr>



		<tr>
		<td><apex:outputPanel id="selected">
		<apex:selectList id="selected_designation_list" required="false" value="{!unHighlightedDesignations}" multiselect="true" size="10">
		<apex:selectOptions value="{!selectedOptionsDesignations}" />
		</apex:selectList>
		</apex:outputPanel>
		</td>
               <td><apex:outputPanel id="move">
	<apex:commandButton immediate="false" status="processingStatus" action="{!reOrderDesignations}" value="Move Up" reRender="des,available,selected">
	<apex:param name="designationDirection" value="up" assignTo="{!DESIGNATION_MOVE_DIRECTION}" />
	</apex:commandButton>
	<br />
	<br />
	<apex:commandButton immediate="false" status="processingStatus" action="{!reOrderDesignations}" value="Move Down" reRender="des,available,selected">
	<apex:param name="designationDirection" value="down" assignTo="{!DESIGNATION_MOVE_DIRECTION}" />
											</apex:commandButton>
	</apex:outputPanel>
	</td>
</tr>
	</table>

	</apex:actionRegion>

	</apex:pageBlockSection>





public void reOrderDesignations(){
	//re order unHighlightedDesignations(list item 0) inside the list called selectedDesignations(set)
	
	
	String SELECTED = unHighlightedDesignations.get(0);
	
	if(unHighlightedDesignations.size() == 1){
		Integer myCount = 0;
		Integer myResult;
		for(String s: selectedDesignations){
			
			if(s.equals(SELECTED)){
				myResult = myCount;	
				
				break;
			}else{
				myCount++;
			}

		}
		
		
		
		String temp;
		Integer x=0;
		//convert set to list
		List<String> items = new List<String>(selectedDesignations);
		
		if(myResult!= 0){
			if(this.DESIGNATION_MOVE_DIRECTION.equalsIgnoreCase('up')){
				x = myResult-1;
				temp = items.get(x);
				
				items[myResult] = temp;
				items[myResult-1] = SELECTED;
				
						
			}else if(this.DESIGNATION_MOVE_DIRECTION.equalsIgnoreCase('down')){
				x = myResult+1;
				temp = items.get(x);
				items[myResult] = temp;
				items[myResult+1] = SELECTED;
			}
			
		Integer size = items.size();
		this.selectedDesignations.clear();
		//////////////////////////////////////////////////
		for(Integer y = 0; y < size;y++ ){
			this.selectedDesignations.add(items[y]);
			System.debug('Item2: '+items[y]);
		}
	
			
		}
		
		
	}
	
	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

Hi,

 

The first thing I'd try is using lists instead of sets as sets are unordered. You may be putting them into the collection in the correct order but when you come to get them out they'd be incorrect. See http://www.salesforce.com/us/developer/docs/apexcode/index.htm for further info.

 

Rich.

All Answers

Richie DRichie D

Hi,

 

The first thing I'd try is using lists instead of sets as sets are unordered. You may be putting them into the collection in the correct order but when you come to get them out they'd be incorrect. See http://www.salesforce.com/us/developer/docs/apexcode/index.htm for further info.

 

Rich.

This was selected as the best answer
SaintMichaelSaintMichael

I was thinking that yesterday, but didn't want to make many code changes to accomodate.

But it works when I use a list.

 

Still having problems getting it into the production page, probably due to the other visualforce elements.

For now, test page good, production page no go.

 

I'll keep you posted.

 

SaintMichaelSaintMichael

Ok, so I got lucky, placed an action status in the right place now everything works.

I guess it all started with the moving from a Set<String> to a List<String>.

 

 

SaintMichaelSaintMichael

Actually it fails when the second list has no items.

Im lost