0

Here I'm Trying to do swapping on Pageblocktable means I want my rows to be Up and down Right now it's happening through Javascript Now I want this through APEX method.

Please look at the below picture for more Understanding:

I want apex method for that row to swap up and down

VF PAGE CODE:

<apex:pageBlockTable id="thetable" title="Invoice line item" var="acc" value="{!contactList}">
                       <apex:column headerValue="S.No" style="width:20px; text-align:center;" headerClass="centertext">
                         <apex:inputField value="{!acc.Row_Num__c}" id="getindex" style="width:25px" />
                        </apex:column>
                        <apex:column headerValue="Nicomatic Part No"><apex:inputField value="{!acc.Name}" required="true" /></apex:column>
                        <apex:column headerValue="Description"><apex:inputField value="{!acc.Description__c}" style="height:30px" /></apex:column>
                        <apex:column headervalue="Client Part No"><apex:inputField value="{!acc.Client_Part_No__c}" style="width:150px" /></apex:column>
                        <apex:column headerValue="HSN"><apex:inputField value="{!acc.HSN__c}" style="width:60px"/></apex:column>
                        <apex:column headervalue="Quantity">
                         <apex:inputField value="{!acc.Quantity__c}" style="width:60px">
                         <apex:param name="cal" value="{!op2}"/>
                         <apex:actionSupport event="onchange" reRender="tot"/> 
                         </apex:inputField>
                         <apex:variable var="op1" value="{!acc.Quantity__c}"/>
                        </apex:column>
                        <apex:column headerValue="Rate(Per Item)">
                          <apex:inputField value="{!acc.Rate_Per_Item__c}" style="width:80px">
                          <apex:param name="cal" value="{!op2}"/>
                           <apex:actionSupport event="onchange" reRender="tot"/>
                          </apex:inputField>
                          <apex:variable var="op2" value="{!acc.Rate_Per_Item__c}"/>
                        </apex:column>
                        <apex:column headerValue="total">
                        <apex:outputpanel id="tot">
                        <apex:outputText value="{!(acc.Quantity__c*acc.Rate_Per_Item__c)}">
                        <apex:actionSupport event="onchange" rerender="to"/>
                         </apex:outputText>
                         </apex:outputPanel>
                        </apex:column>
                        <apex:column headerValue="Discount"><apex:inputField value="{!acc.Discount__c}" style="width:60px">
                        <apex:actionSupport event="onchange" rerender="to"/>
                         </apex:inputField>
                        </apex:column>
                        <apex:column headerValue="Taxable Value">
                        <apex:outputText id="to" value="{!((acc.Quantity__c*acc.Rate_Per_Item__c)-acc.Discount__c)}" />
                       </apex:column>                       

                        <apex:column headerValue="Action" >
                         <apex:commandButton value="Delete" onmouseout="insert_numbers()" action="{!deleteRow}" reRender="pb">
                          <apex:param name="rowIndex" value="{!rowNumber}"/>
                         </apex:commandButton>
                         <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
                       </apex:column>                                                
                        <apex:column HeaderValue="Row Up">
                        <apex:commandButton onClick="insert_numbers()"
                          rerender="mytable" action="{!swap}"  value="Up" >&#8679;
                           <apex:param name="RowIndex" assignTo="{!rowNumber}" value="{!rowNumber}" />

                        </apex:commandbutton>
                    </apex:column>
                    <apex:column HeaderValue="Row Down">
                        <apex:commandButton onClick="insert_numbers()"
                            rerender="mytable" action="{!swap}" value="Down" >&#8681;
                            <apex:param name="RowIndex" assignTo="{!rowNumber}" value="{!rowNumber}" />

                        </apex:commandbutton>
                    </apex:column>
         </apex:pageBlockTable>   

APEX CODE:

Public Void Swap(){
    //Here this **"contactList"** is normal list that I'm passing information to //VF page to pageblock table 
//I don't know what to do now for Swapping
                    For(Invoice__c li:contactList){

                        If(li.name=='656555'){

                            li=li[0];
                        }
                    }

            }
1

Your controller method needs to reference the rowNumber property that is already set by your Visualforce when the up or down buttons are clicked. You also need to communicate whether it is up or down by e.g. using separate methods. So something like:

Public Void SwapUp() {
     Swap(-1);
}

Public Void SwapDown() {
     Swap(1);
}

private Void Swap(Integer offset) {
     Invoice__c temp = contactList[rowNumber + offset];
     contactList[rowNumber + offset] = contactList[rowNumber];
     contactList[rowNumber] = temp;
}

Take care in the Visualforce to disable the up button in the first row and the down button in the last row.

Best you rename contactList as that name is misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.