FORCE.COM
Creating MultiSelect with Search button
VisualForce Page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
<apex:component controller="AlarmRule_MultiSelect_Comp_Contr"> <style> .myspacingtable { border-collapse: separate; border-spacing: 10px; } </style> <apex:attribute name="aLeftLabel" description="Label on left listbox." type="String" required="true" /> <apex:attribute name="aRightLabel" description="Label on right listbox." type="String" required="true" /> <apex:attribute name="aSize" description="Size of listboxes." type="Integer" required="true" /> <apex:attribute name="aWidth" description="Width of listboxes." type="String" required="true" /> <apex:attribute name="aInitialRightList" description="Initial record Ids in right box." type="String[]" required="false" assignTo="{!InitialRightList}" /> <apex:attribute name="aCurrentRightList" description="Current record Ids in right box." type="String[]" required="true" assignTo="{!CurrentRightList}" /> <apex:actionRegion > <apex:outputPanel id="BoxPanel"> <table class="myspacingtable" style="width: {!aWidth}"> <tr> <td style="width:5%;position: relative; top:3px;" ><span >Search:</span></td> <td style="width: 20%;"><apex:inputText value="{!SearchText}" /></td> <td style="width: 1%;padding-right:3px !important;position: relative; top:3px;"> By </td> <td style="width: 20%"><apex:selectList value="{!selectedVal}" size="1"><apex:selectOptions value="{!openPresentationOptions}" /> </apex:selectList></td> <td> <span style="float: left;"> </span> <apex:actionStatus id="SearchIcon"> <apex:facet name="start"> <apex:outputPanel > <apex:commandButton style="float: left;" disabled="true" value=" Find " /> <img style="float: left;" src="/img/loading24.gif" /> </apex:outputPanel> </apex:facet> <apex:facet name="stop"> <apex:outputPanel > <apex:commandButton style="position:relative;top:-3px;" action="{!Find}" value=" Find " rerender="BoxPanel" status="SearchIcon" /> </apex:outputPanel> </apex:facet> </apex:actionStatus> <span style="padding-left:5px;" title="Search the text by providing or click to find all"><img style="relative; top:-3px;" src="http://png-3.findicons.com/files/icons/1156/fugue/16/question_frame.png" /></span> </td> </tr> </table> <table style="width: {!aWidth}"> <tr> <th style="width:50%" class="selectTitle">{!aLeftLabel}</th> <th style="width:30px"></th> <th style="width:50%" class="selectTitle">{!aRightLabel}</th> </tr> <tr> <td><apex:selectList value="{!LeftSelectedList}" multiselect="true" style="width:100%" size="{!aSize}"> <apex:selectOptions value="{!LeftOptionList}" /> </apex:selectList></td> <td style="vertical-align: middle;align:center;"> <div> <apex:image styleClass="picklistArrowRight" value="/s.gif"> <apex:actionSupport event="onclick" action="{!ClickRight}" reRender="BoxPanel" /> </apex:image> <br /> <br /> <apex:image styleClass="picklistArrowLeft" value="/s.gif"> <apex:actionSupport event="onclick" action="{!ClickLeft}" reRender="BoxPanel" /> </apex:image> <br /> <br /> <apex:commandButton value="Clear" action="{!clearMe}" rerender="BoxPanel"/> </div> </td> <td><apex:selectList value="{!RightSelectedList}" multiselect="true" style="width:100%" size="{!aSize}"> <apex:selectOptions value="{!RightOptionList}" /> </apex:selectList></td> </tr> </table> </apex:outputPanel> </apex:actionRegion> </apex:component> Apex Code: public with sharing class AlarmRule_MultiSelect_Comp_Contr { public String selectedVal{get;set;} public List<SelectOption> getopenPresentationOptions() { List<SelectOption> selectOptions = new List<Selectoption>(); Set<SelectOption> selectOptionsNoDupes = new Set<SelectOption>(); List<Asset__c> queryResult = [SELECT id, Make__r.Name,Model__r.Name,Model_Year__c FROM Asset__c]; for(Asset__c obj : queryResult) { Make__c make = (Make__c)obj.getSobject('Make__r'); Model__c model = (Model__c)obj.getSobject('Model__r'); if(make != null && model !=null) { string make_model = make.Name + ' / ' + model.Name + ' / ' + obj.Model_Year__c; selectOptionsNoDupes.add(new selectOption(make.Id, make_model)); //optns.add(new selectOption(make.Id, make_model)); } } selectOptions.add(new SelectOption('Select...', 'Select...')); selectOptions.addAll(selectOptionsNoDupes); selectedVal = 'Select...'; return selectOptions; } public static final String USERTYPE_STD = 'Standard'; public list<String> InitialRightList {get;set;} public list<String> CurrentRightList {get;set;} public String SearchText {get;set;} public list<String> LeftSelectedList {get;set;} public list<String> RightSelectedList {get;set;} map<String, Asset__c> LeftOptionMap = new map<String, Asset__c>(); map<String, Asset__c> RightOptionMap = new map<String, Asset__c>(); /**** * Controller - instantiate lists ****/ public traceIT_AlarmRule_MultiSelect_Comp_Contr() { LeftSelectedList = new list<String>(); RightSelectedList = new list<String>(); } /**** * ClickRight - Right pointing arrow was clicked. Move selected options to the right box. ****/ public PageReference ClickRight(){ RightSelectedList.clear(); for(String s : LeftSelectedList){ if (LeftOptionMap.containsKey(s)) { RightOptionMap.put(s, LeftOptionMap.get(s)); } LeftOptionMap.remove(s); } return null; } /**** * ClickLeft - Left pointing arrow was clicked. Move selected options to the left box. ****/ public PageReference ClickLeft(){ LeftSelectedList.clear(); for(String s : RightSelectedList){ if (RightOptionMap.containsKey(s)) { LeftOptionMap.put(s, RightOptionMap.get(s)); } RightOptionMap.remove(s); } return null; } /**** * getLeftOptionList - return SelectOptions for the left/unselected box ****/ public list<SelectOption> getLeftOptionList(){ list<SelectOption> TempOptionList = new list<SelectOption>(); list<Asset__c> TempValueList; TempValueList = LeftOptionMap.values(); TempValueList.sort(); // sort by name for (Asset__c u : TempValueList) { TempOptionList.add(new SelectOption(u.Id, u.Name)); } return TempOptionList; } /**** * getRightOptionList - return SelectOptions for the right/selected box ****/ public list<SelectOption> getRightOptionList(){ list<SelectOption> TempOptionList = new list<SelectOption>(); list<Asset__c> TempValueList; list<Asset__c> AssetList; //clear is used instead of new list, so the list maintains the pointer to the ExamplePageController list CurrentRightList.clear(); // load initially selected records into the right box if (InitialRightList != null && InitialRightList.size() > 0) { AssetList = [SELECT id,name,Description__c,Asset_Code__c,Make__r.Name,Model__r.Name,Model_Year__c FROM Asset__c WHERE name = :USERTYPE_STD and id IN :InitialRightList limit 500]; //[Select Name, Id, IsActive, UserType From User where IsActive=true and UserType = :USERTYPE_STD and Id IN :InitialRightList limit 500]; for (Asset__c u : AssetList) { RightOptionMap.put(u.Id, u); } InitialRightList.clear(); } TempValueList = RightOptionMap.values(); TempValueList.sort(); // sort by name for (Asset__c u : TempValueList) { TempOptionList.add(new SelectOption(u.Id, U.Name)); CurrentRightList.add(u.Id); } return TempOptionList; } /**** * Find - Search for Asset records by name, and add them to the left box ****/ public PageReference Find(){ String TempSearchText; list<Asset__c> AssetList; TempSearchText = '%' + SearchText + '%'; //AssetList = [Select Name, Id, IsActive, UserType From User where IsActive=true and UserType = :USERTYPE_STD and Name like :TempSearchText limit 500]; AssetList = [SELECT id,name,Description__c,Asset_Code__c,Make__r.Name,Model__r.Name,Model_Year__c FROM Asset__c WHERE name like :TempSearchText OR Description__c like :TempSearchText limit 500]; LeftOptionMap.clear(); for (Asset__c u : AssetList) { if (!RightOptionMap.containsKey(u.Id)) { LeftOptionMap.put(u.Id, u); } } return null; } public PageReference clearMe() { LeftSelectedList.clear(); RightSelectedList.clear(); RightOptionMap.clear(); LeftOptionMap.clear(); return null; } } |
Favorite
Dreamforce Visualforce Demo with Ralph
GitHub source code Dreamforce Visualforce Demo with Ralph Favorite