public with sharing virtual class PageBaseController
{
protected string lookUpSelectedValue{get;set;}
public LookUpListUtil component{get;set;}
public virtual void onValuechange(string SelectedLookUp)
{
/******
On change event of pick list.
If you want to create component picklist change event on page.
Then overight this method on page (onValuechange)
************/
}
public void SetLookUpSelectedValue(string val)
{
lookUpSelectedValue=component.lookUpSelectedValue;
}
}
// Custom controller for lookup control.
public with sharing class LookUpListUtil
{
public PageBaseController pageController{get;
set
{
if(value!=null)
pageController=value;
pageController.component=this;
}
}
public string ObjectType{get;set;}
public string objectField{get;set;}
public string groupBy{get;set;}
public List<selectOption> LookUpDataList{get; private set;}
public string lookUpSelectedValue{get;set;}
public sobject objAssignTo{get;set ;}
public string AssignToField{get;set;}
public LookUpListUtil()
{
LookUpDataList=new List<selectOption>();
//TODO : testing use only need to remove line no 22 to 29
if(ObjectType==null)
{
ObjectType='Account';
objectField='name';
groupBy='Type';
}
try
{
string soqlQuery;
if(groupBy=='')
soqlQuery='SELECT ID,'+objectField+' FROM '+ type.forname(ObjectType).getName();
else
soqlQuery='SELECT ID,'+objectField+','+ groupBy+' FROM '+ type.forname(ObjectType).getName()+' ORDER BY '+groupBy;
string itemVal;
for(sobject obj:DataBase.query(soqlQuery))
{
if(groupBy=='')
LookUpDataList.Add(new selectOption(string.ValueOf(obj.get('id')),string.Valueof(obj.get(objectField))));
else
{
itemVal=string.Valueof(obj.get(groupBy))+'_'+string.Valueof(obj.get(objectField));
LookUpDataList.Add(new selectOption(string.ValueOf(obj.get('id')),itemVal));
}
}
}
catch(exception ex)
{
apexPages.AddMessage(new apexPages.Message(ApexPages.severity.Error,'Can\'t able to fetch data in PickList. There is an issue. '+ex.getmessage()));
}
//return ReturnList;
}
public virtual void onValuechange()
{
//objAssignTo=lookUpSelectedValue;
objAssignTo.put(AssignToField,lookUpSelectedValue);
pageController.onValuechange(lookUpSelectedValue);
pageController.SetLookUpSelectedValue(lookUpSelectedValue);
}
public string GetSelectedValue()
{
return this.lookUpSelectedValue;
}
}
<apex:component controller="LookUpListUtil">
<!-- Begin Default Content REMOVE THIS -->
<apex:includeScript value="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
<apex:attribute type="string" assignTo="{!ObjectType}" name="LookUpObject" description="type of sobject." required="true"/>
<apex:attribute type="string" assignTo="{!objectField}" default="name" name="FieldName" description="Which field value you want to show on ui"/>
<apex:attribute type="PageBaseController" assignTo="{!pageController}" name="Controller" description="Which field value you want to show on ui" required="true"/>
<apex:attribute type="sobject" assignTo="{!objAssignTo}" name="AssignTo" description="Assign selected value" required="true" />
<apex:attribute type="string" assignTo="{!AssignToField}" name="AssignToFieldname" description="Assign selected value" required="true" />
<apex:selectList id="LookUpControl" value="{!lookUpSelectedValue}" size="1" multiselect="false" >
<apex:selectOption itemValue="" itemLabel="--Please Select--"/>
<apex:selectoptions value="{!LookUpDataList}" >
</apex:selectoptions>
<apex:actionSupport event="onchange" action="{!onValuechange}" rerender="sdf" />
</apex:selectList>
<script>
jQuery.noConflict();
(function (componentName) {
// get a reference to the select component in the page
var selectList = jQuery('[id="' + componentName + '"]');
// a javascript object to collect the elements in each region
var regions = {};
// iterate all of the options and collect them by region
jQuery('option', selectList).each(function (i) {
if (i === 0) {
return; // skip the first element in the list
}
var oElement = jQuery(this);
var regionCountryArray = oElement.text().split('_');
// create an array entry for the region name if there is not one
if (!regions[regionCountryArray[0]]) {
regions[regionCountryArray[0]] = [];
}
// add the item to the array for this region
regions[regionCountryArray[0]].push(oElement);
});
// iterate all of the names in the regions object
for (var region in regions) {
// make sure the name did not come from the prototype
if (regions.hasOwnProperty(region)) {
// turn the array of items into a single jQuery collection
var groupElements = jQuery(regions[region]).map(function () {
return this.toArray();
});
// create the group and set the label
var optgroup = jQuery('<optgroup/>');
optgroup.attr('label', region);
// wrap the option elements in an optgroup
groupElements.wrapAll(optgroup);
// remove the region text from the label
groupElements.each(function () {
jQuery(this).text(function () {
return jQuery(this).text().replace(region + '_', '');
});
});
}
}
})('{!$Component.LookUpControl}'); // immediate function execution - pass in the id of the select list
</script>
</apex:component>
public with sharing class LookUpListUtilTestPagecontroller extends PageBaseController
{
/****** lookUpSelectedValue containing current selected value of pickList ***/
public LookUpListUtilTestPagecontroller ()
{
acc=new account();
}
public string selectedValue{get;set;}
public account acc{get;set;}
public pagereference Button_click()
{
system.assert(false,' value on page'+lookUpSelectedValue + ' acc='+acc.name);
return null;
}
public override void onValuechange(string SelectedLookUp)
{
//val=get('cntr');
//insert
// system.assert(false,' value on page'+lookUpSelectedValue);
}
public LookUpListUtilTestPagecontroller GetThis(){ return this;}
}
<apex:page controller="LookUpListUtilTestPagecontroller" >
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
<br/> <br/>
<apex:form >
<c:LookUp LookUpObject="Account" Controller="{!this}" AssignTo="{!acc}" AssignToFieldname="name"/>
<br/>
<apex:commandButton action="{!Button_click}" title="Button" style="width:50px;" value="Page Btn"> </apex:commandButton>
</apex:form>
This is your new Page
<!-- End Default Content REMOVE THIS -->
</apex:page>