2

I'm trying to add a button Delete in every row inside a pageblock table. When a User clicks that button the row has to be deleted and table rerendered. I am not able to to pass the id of the row that has to be deleted. I tried with this code:

<apex:outputPanel id="pinserted">
    <apex:pageBlock >
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!saveContract}" value="Salva Contratto"  />
            <apex:commandButton action="{!deleteContract}" value="Elimina Contratto e prodotti selezionati" immediate="true" rerender="tabella" />
        </apex:pageBlockButtons>          
        <apex:pageBlockSection columns="1">
            <apex:pageBlockTable value="{!shoppingCart}" var="sc" id="tabella">
                <apex:column value="{!sc.Id}"/>
                <apex:column value="{!sc.Scelta__c}"/>
                <apex:column value="{!sc.Giorni_durata_noleggio__c}"/> 
                <apex:column value="{!sc.Quantit_prodotto__c}"/>
                <apex:column value="{!sc.Sconto__c}"/>
                <apex:column headerValue="Azioni">
                <apex:commandButton action="{!deleteRow}" immediate="true" reRender="pinserted" value="CANC">
                <apex:param name="rowid" value="{!sc.Id}" assignTo="{!SelectedRowId}"/>
                </apex:commandButton>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>             
    </apex:pageBlock>
 </apex:outputPanel>

and inside the controller I put the function:

public Pagereference deleteRow(){
        System.debug('fatto');
        String SelectedRowId=ApexPages.CurrentPage().getParameters().get('rowid');
        System.debug(SelectedRowId);
       Prodotto_contratto__c tobeDeleted = null;
        for(Prodotto_contratto__c a : shoppingCart)
        if (a.Id == SelectedRowId) {
            tobeDeleted = a;
            break;
        } 
        if (tobeDeleted != null) {
           Delete tobeDeleted;
            }
        return null;
    }

But it's not working. As you can see the SelectedRowId variable is null checking the Logs. How can I do that? every help is really appreciated thank you

6
  • 1
    The docs say apex:param is "A parameter for the parent component" so move it to within i.e. <apex:commandButton> in here </apex:commandButton>. Also I have only used the assignTo mechanism of apex:param so am unsure about getting the value from the current page parameters.
    – Keith C
    Commented Jul 16, 2016 at 9:24
  • thank you now seems to delete teh right record inside the table. Commented Jul 16, 2016 at 9:28
  • But the table isn't rerender after, why? Commented Jul 16, 2016 at 9:29
  • I don't see the "pinserted" ID present in the markup you have posted - is it present on any parent tag of what you have posted? Needs to be to identify the part of the page to re-render.
    – Keith C
    Commented Jul 16, 2016 at 9:31
  • I'mso sorry I missed part of the vf page, now I edit it Commented Jul 16, 2016 at 9:33

1 Answer 1

2

The docs say apex:param is "A parameter for the parent component" so move it to within i.e. <apex:commandButton> in here </apex:commandButton>.

If the ID is set correctly the table may be being re-rendered, but your deleteRow method is not removing the row from the in-memory collection so you are still seeing it. You instead need code more like this:

for (Integer i = 0; i < shoppingCart.size(); i++) {
    Prodotto_contratto__c a = shoppingCart[i];
    if (a.Id == SelectedRowId) {
        delete a;
        shoppingCart.remove(i);
        break;
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .