+ Start a Discussion
Vijay sidaraddiVijay sidaraddi 

I have Visual force page which is having 10 records how to enable button when i selected one records (by check box im selecting)

Hi,

I have Vf page which is having 10 records when i selected one record which is having check box than it should be enable the  button  than im going to share selected record with some user ( for sharing i can use apex sharing, let me know), let me know how to do this.

<apex:page Controller="Democlass" extensions="">
<apex:form>
<apex:inputCheckbox id="isCheckBox" style="opacity:{!w2.isPartOrderabilityStatusCode};" value="{!w2.isCheckBox}" >
<apex:actionSupport event="onclick" action="{!checkBoxStaus}" reRender="pb2"/>
</apex:inputCheckBox>
<apex:commandButton value="Add Selected"   rendered="{!(if(isButtonValid==true, true, false))}" action="{!selectRecords}" id="sparebtn" reRender="frm">
<apex:actionSupport event="onclick" rerender="resultsBlock" status="statusSaveTrip"/>
</apex:commandButton>
</apex:form>
</apex:page>

Thanks
Vijay S
 
Best Answer chosen by Vijay sidaraddi
hitesh90hitesh90

Hello Vijay,

Try to use following sample code for your requirement.

Visualforce Page:
<apex:page id="myPage" controller="listwithSelectionController">
    <script>
        var count = 0;
        function enableSharebutton(chk){
            if(chk.checked == true){  
                count = count + 1;             
                document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShareDisplay').style.display = 'block';
                document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShare').style.display = 'none';
            }else{
                count = count - 1;
                if(count == 0){
                     document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShareDisplay').style.display = 'none';
                    document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShare').style.display = 'block';
                }
            }
        }
    </script>
    <apex:form id="myFrm">
        <apex:pageblock id="pbMain">
            <apex:pageblockButtons id="pbButtons" location="top">
                <apex:commandButton id="btnShare" value="Share" disabled="true"/>
                <apex:commandButton id="btnShareDisplay" style="display:none;" value="Share" />
            </apex:pageblockButtons>
            <apex:pageblockTable id="contList" value="{!lstwrapperContact}" var="con">
                <apex:column headerValue="Select" width="3%">                    
                    <apex:inputcheckbox id="conSel" value="{!con.isConSelected}" onclick="enableSharebutton(this)"/>                                                      
                </apex:column>
                <apex:column headerValue="Name" width="20%">
                    <apex:outputField value="{!con.objContact.Name}"/>
                </apex:column>
                <apex:column headerValue="Email" width="20%">
                    <apex:outputField value="{!con.objContact.Email}"/>
                </apex:column>
                <apex:column headerValue="Phone" width="20%">
                    <apex:outputField value="{!con.objContact.Phone}"/>
                </apex:column>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex Class:
public class listwithSelectionController{
    public List<wrapperContact> lstwrapperContact {get; set;}
    public listwithSelectionController(){
        lstwrapperContact = new List<wrapperContact>();
        List<Contact> lstContact = [select id, Name, Email, Phone from contact LIMIT 10];
        wrapperContact objwrapperContact;
        for(Contact con: lstContact){
            objwrapperContact = new wrapperContact();
            objwrapperContact.objContact = con;
            objwrapperContact.isConSelected = false;
            lstwrapperContact.add(objwrapperContact);
        }
    }
    public void shareSelRecords(){
        // Your logic will be here
    }
    public class wrapperContact{
        public boolean isConSelected {get; set;}
        public Contact objContact {get; set;}
    }
}

Let me know if you have any question on this.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

Hello Vijay,

Try to use following sample code for your requirement.

Visualforce Page:
<apex:page id="myPage" controller="listwithSelectionController">
    <script>
        var count = 0;
        function enableSharebutton(chk){
            if(chk.checked == true){  
                count = count + 1;             
                document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShareDisplay').style.display = 'block';
                document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShare').style.display = 'none';
            }else{
                count = count - 1;
                if(count == 0){
                     document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShareDisplay').style.display = 'none';
                    document.getElementById('myPage:myFrm:pbMain:pbButtons:btnShare').style.display = 'block';
                }
            }
        }
    </script>
    <apex:form id="myFrm">
        <apex:pageblock id="pbMain">
            <apex:pageblockButtons id="pbButtons" location="top">
                <apex:commandButton id="btnShare" value="Share" disabled="true"/>
                <apex:commandButton id="btnShareDisplay" style="display:none;" value="Share" />
            </apex:pageblockButtons>
            <apex:pageblockTable id="contList" value="{!lstwrapperContact}" var="con">
                <apex:column headerValue="Select" width="3%">                    
                    <apex:inputcheckbox id="conSel" value="{!con.isConSelected}" onclick="enableSharebutton(this)"/>                                                      
                </apex:column>
                <apex:column headerValue="Name" width="20%">
                    <apex:outputField value="{!con.objContact.Name}"/>
                </apex:column>
                <apex:column headerValue="Email" width="20%">
                    <apex:outputField value="{!con.objContact.Email}"/>
                </apex:column>
                <apex:column headerValue="Phone" width="20%">
                    <apex:outputField value="{!con.objContact.Phone}"/>
                </apex:column>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex Class:
public class listwithSelectionController{
    public List<wrapperContact> lstwrapperContact {get; set;}
    public listwithSelectionController(){
        lstwrapperContact = new List<wrapperContact>();
        List<Contact> lstContact = [select id, Name, Email, Phone from contact LIMIT 10];
        wrapperContact objwrapperContact;
        for(Contact con: lstContact){
            objwrapperContact = new wrapperContact();
            objwrapperContact.objContact = con;
            objwrapperContact.isConSelected = false;
            lstwrapperContact.add(objwrapperContact);
        }
    }
    public void shareSelRecords(){
        // Your logic will be here
    }
    public class wrapperContact{
        public boolean isConSelected {get; set;}
        public Contact objContact {get; set;}
    }
}

Let me know if you have any question on this.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
This was selected as the best answer
Vijay sidaraddiVijay sidaraddi
Hi Hitesh,

Its Spledind Great ..its working as expected thanks lot.

Thanks
Vijay S