How to add from PageBlockTable to another PageBlockTable in Visualforce page?

How to add from PageBlockTable to another PageBlockTable in Visualforce page?

Sample Code:


Visualforce page:

<apex:page id=”pg” controller=”Sample”>
<apex:actionstatus id=”counterStatus”>
    <apex:facet name=”start”>
        <div class=”waitingSearchDiv” id=”el_loading” style=”background-color: #fbfbfb; height:100%;opacity:0.65;width:100%;”>
            <div class=”waitingHolder” style=”top: 100px; width: 91px;”>
            <img class=”waitingImage” src=”/img/loading.gif” title=”Please Wait…” />
            <span class=”waitingDescription”>Loading…</span>
            </div>
        </div>
    </apex:facet>
</apex:actionstatus>
<apex:form >
<apex:variable var=”rowNum” value=”{!0}”  />
    <apex:pageBlock id=”addList”>
        <apex:variable var=”rowNum” value=”{!0}”  />
        <apex:pageBlockTable value=”{!listAccount}” var=”acct”>
            <apex:column value=”{!acct.Name}”/>
            <apex:column value=”{!acct.Industry}”/>
            <apex:column >
                <apex:commandLink value=”Add” action=”{!addToList}” reRender=”addList, removeList” status=”counterStatus”>
                    <apex:param value=”{!rowNum}” name=”index” />
                </apex:commandLink>
                <apex:variable var=”rowNum” value=”{!rowNum+1}”/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
<apex:outputPanel id=”removeList”>
<apex:variable var=”rowNum1″ value=”{!0}”  />
    <apex:pageBlock rendered=”{! IF(listAccountAdded.size > 0, true, false)}”>
        <apex:variable var=”rowNum1″ value=”{!0}”  />
        <apex:pageBlockTable value=”{!listAccountAdded}” var=”acc”>
            <apex:column value=”{!acc.Name}”/>
            <apex:column value=”{!acc.Industry}”/>
            <apex:column >
                <apex:commandLink value=”Remove” action=”{!removeFromList}” reRender=”addList, removeList” status=”counterStatus”>
                    <apex:param value=”{!rowNum1}” name=”index1″ />
                </apex:commandLink>
                <apex:variable var=”rowNum1″ value=”{!rowNum1+1}”/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:outputPanel>    
</apex:form>
</apex:page>


Apex Controller:


public class Sample {
    public List<Account> listAccount {get;set;}    
    public List<Account> listAccountAdded {get;set;}
    public Integer rowNum {get;set;}    
    public Integer rowNum1 {get;set;}
    
    public Sample() {
        listAccount = [SELECT Name, Industry FROM Account];
        listAccountAdded = new List<Account>();
    }
    
    public void addToList() {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get(‘index’));
        listAccountAdded.add(listAccount.get(rowNum));
        listAccount.remove(rowNum);   
    }
    
    public void removeFromList() {
        rowNum1 = Integer.valueOf(apexpages.currentpage().getparameters().get(‘index1’));
        listAccount.add(listAccountAdded.get(rowNum1));
        listAccountAdded.remove(rowNum1);   
    }

}


Output:


Cheers!!!

Leave a Reply