function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SushupsiSushupsi 

Component Development in Apex

Hi All,

 

Long time since i have posted on the forums. Here i am with a request for suggestion.

 

I have developed the following component:

<apex:component controller="PickText">

    <script>
        function ShowText(list,text)
        {
            document.getElementById(list).style.visibility = "hidden";
            document.getElementById(list).style.display = "none";
            document.getElementById(text).style.visibility = "visible";
            document.getElementById(text).style.display = "inline";
            document.getElementById(text).focus();
        }
    </script>

    <span style="color:Blue;font-weight:bold"><apex:OutputLabel >Please Press a key to enable Text Entry</apex:OutputLabel></span><br/><br/>
    <apex:inputText id="inputone" style="visibility:hidden; display:none;" size="4" maxlength="2" value="{!SelectProduct}">
    <apex:actionSupport action="{!SaveText}" event="onblur" reRender="inputone"/>
    </apex:inputText>
    <apex:selectList id="sel" size="1" value="{!SelectProduct}" onkeypress="ShowText('{!$Component.sel}','{!$Component.inputone}');">
        <apex:selectOptions id="prodlist" value="{!ListValues}">
        </apex:selectOptions>
    </apex:selectList>
</apex:component>

 

 

The Controller for the component goes like this:

 

Public class PickText
{
    public PageReference SaveText() {
        return null;
    }
    Public String SelectProduct;
    Public List<SelectOption> getListValues()
    {
        List<SelectOption> ProdList = new List<SelectOption>();
        ProdList.add(new SelectOption('None','None'));
        ProdList.add(new SelectOption('MT','MT'));      
        ProdList.add(new SelectOption('MO','MO'));
        ProdList.add(new SelectOption('PPC','PPC'));
        ProdList.add(new SelectOption('UBP','UBP'));
        ProdList.add(new SelectOption('EPS','EP'));
        return ProdList;
    }

    Public String getSelectProduct(){
    return SelectProduct;
    }

    Public void setSelectProduct(String Product){
    this.SelectProduct = Product;
    system.debug('******'+SelectProduct);
    }
}

 

My query is when i have developed this component, the intention is to store the value from a text box or pick list into a back end field.

I have stored a value in the controller in a local variable.

 

When this component is used in a VF page, how do i access the value in the field. (The problem is the value is Stored in component's controller)

 

Any pointers are appreciated.

TIA

- Sushupsi

_Prasu__Prasu_

i think you will need to use <apex:attribute> tag which may provide you two binding of data.

You can find more information here: Visualforce Developer's Guide

SushupsiSushupsi

Thank you Prasanna, i am currently working on the same and will try posting the component. :)

 

Thanks

-Sushupsi

SushupsiSushupsi

Hi there, I struggled hard to conclude and still fail to reach a solution.:smileysad::smileysad:

 

Please find my component and help me with the same:

 

<apex:component controller="PickTextCombo">

    <apex:attribute name="listValues" required="true" type="String[]" description="This is the picklist value that we display." assignTo="{!}"/>
    
    <apex:selectList multiselect="false" size="1" value="{!selectValues}">
    <apex:selectOptions id="prodlist" value="{!listValues}">
    </apex:selectOptions>
    </apex:selectList>
</apex:component>

 

 The controller for my component is aas follows:

 

public class PickTextCombo
{
    Public String selectedValue;

    public String getSelectValues(){
        return selectedValue;
    }
    Public void setSelectValues(String pickval){
        selectedValue = pickval;
        system.debug('*****'+selectedValue);
    }

}

 

My intention is to save the value in the picklist and send it to a field in the page in which the Component is used.

The main purpose is to Use the same component in multiple locations in the same VF page and yet reuse the code.

 

My problem is that i am unable to find a way to return the value chosen in the "Component Controller" to my controller in current page which uses the custom component.

 

My page that uses the component is as follows: 

<apex:page controller="testcon">
<apex:form >

<apex:outputLabel value="{!$Component.one.selectValues}"></apex:outputLabel>
    <c:PickTextCombo listValues="{!ListValues}"/>
    <apex:outputText id="one1" value="{!retval}"/><br/><br/>

    <c:PickTextCombo listValues = "{!ListValues1}" id="two"/>
    <apex:outputText id = "two1" value="{!retval1}"/>
<br/><br/><apex:commandButton value="test"/>
</apex:form>
</apex:page>

 The Controller for current page is as:

 

public class testcon
{
    Public PickTextCombo test = new PickTextCombo();
    Public String retval;
    Public String retval1;

    Public String getretval(){
        if(test.selectedValue != null){
            retval = test.getSelectValues();
        }
        system.debug('-----'+retval);
        return retval;
    }

    Public String getretval1(){
        if(test.selectedValue != null){
            retval1 = test.getSelectValues();
        }
        system.debug('-----'+retval1);
        return retval1;
    }

    Public List<SelectOption> getListValues()
    {
        List<SelectOption> ProdList = new List<SelectOption>();
        ProdList.add(new SelectOption('None','None'));
        ProdList.add(new SelectOption('MT','MT'));      
        ProdList.add(new SelectOption('MO','MO'));
        ProdList.add(new SelectOption('PPC','PPC'));
        ProdList.add(new SelectOption('UBP','UBP'));
        ProdList.add(new SelectOption('EPS','EP'));
        return ProdList;
    }

    Public List<SelectOption> getListValues1()
    {
        List<SelectOption> ProdList = new List<SelectOption>();
        ProdList.add(new SelectOption('None','None'));
        ProdList.add(new SelectOption('A','A'));      
        ProdList.add(new SelectOption('B','B'));
        ProdList.add(new SelectOption('C','C'));
        ProdList.add(new SelectOption('D','D'));
        ProdList.add(new SelectOption('E','E'));
        return ProdList;
    }
}

 

 I tried using the Assign to attribute, but it seems of not much help. Please educate me how to resolve the issue and help me code better.

 

Hoping to find a solution soon.:smileysad:

 

TIA

-Sushupsi.