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
Rishab Goyal 8Rishab Goyal 8 

How to delete a row(of PageBlockTable ) from vf page

I am creating a VF page to mass issue books in a library management system. I have a custom junction object (Book_Issue__c) which is a child of Book__c object and Contact object.
 
I am using the following code for the VF Page
<apex:page Controller="MultipleBookIssue" >
    
    
    <apex:form >
        <apex:pageBlock title="Mass Issue Books">
            
            <apex:pageBlockTable Value="{!listBookIssue}" var="issue" id="table" >
                <apex:column headerValue="Select">
                    
                    <apex:inputCheckbox value="{!issue.selected}"/>
                    
                </apex:column>        
                <apex:column headerValue="Book" >
                    
                    <apex:inputField value="{!issue.bi.Book__c}"/>
                </apex:column>
                <apex:column headerValue="Issued To" >
                    <apex:inputField value="{!issue.bi.Issued_To__c}"/>
                </apex:column>
                
                <apex:column headerValue="Issue Date" >
                    <apex:inputField value="{!issue.bi.Book_Issue_Date__c}"/>
                </apex:column>        
                
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="Bottom" >
                <apex:commandButton value="Add Book Issue Row" action="{!addBookIssueRow}" immediate="true"/>
           
                <apex:commandButton value="Delete Book Issue Row" action="{!deleteBookIssueRow}" rendered="true" />
          
                <apex:commandButton action="{!saveBookIssue}" value="Save Book Issue"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

User-added imageUser-added image
 
public class MultipleBookIssue {
    
    public list<wBookIssue> listBookIssue{get;set;}
    
    public void deleteBookIssueRow() {
        
        System.debug('before: ' + listBookIssue);
        
        Integer j=0;
        while(j<listBookIssue.size())
        {
            if(listBookIssue.get(j).selected==true){
                listBookIssue.remove(j);
                j--;
            }
            j++;
        }
/*
        List<Integer> deleteList = new List<Integer>();
        Integer i =0;
        for(wBookIssue wbi : listBookIssue) {
            if(wbi.selected == true) {
                deleteList.add(i);
                listBookIssue.remove(i);
            }
            i++;
        }
        */
        
        System.debug('after: ' + listBookIssue);
        
    }
    
    
    
    public MultipleBookIssue(){
        listBookIssue= new list<wBookIssue>();
        listBookIssue.add(new wBookIssue());
    }
    
    public void addBookIssueRow(){
        listBookIssue.add(new wBookIssue());
    }
    
    public PageReference saveBookIssue()
    {
        List<Book_Issue__c> biList = new List<Book_Issue__c>();
        for(wBookIssue wbi : listBookIssue) {
            biList.add(wbi.bi);
        }
        
        insert biList;
        
        return Page.AllBooksIssued;
    }
    
    public class wBookIssue {
        public Book_Issue__c bi {get; set;}
        public boolean selected {get; set;}
        
        public wBookIssue() {
            bi = new Book_Issue__c();
            selected = false;
        }
    }
}
 
But I am not able to delete the row.. I am attaching the screenshot of the vf page.
 
mohdAnasmohdAnas


Hi Rishab,
Its because your operation is not moving further since the error being shown on the page is due to the fields being required.
Therefore to over come this use
 
<apex:commandButton value="Delete Book Issue Row" action="{!deleteBookIssueRow}"rendered="true"  immediate ="true" />

I hope this helps but if it doesn't let me know i will go through your process as well and will try to help you.
 
Rishab Goyal 8Rishab Goyal 8
Hi @mohdAnas, thanks for the reply,  I have tried this before, but when I am using immediate attribute it is skipping validation rule and also not doing anything but returning me the same page again. It actually skips the changed value of checkbox(to be true) and use false as the default value and hence my row is not deleted
Rishab Goyal 8Rishab Goyal 8
after using immediate attribute
Before Pressing Delete Row Button
User-added image
after 
User-added image
mohdAnasmohdAnas

Hi @rishab,

I looked at the code your fields seems to be required at field level therefore to by pass operation you need immediate =.true, but as soon as we put immediate true all other operations stops as well there fore the checkbox which are checked couldnt be set true for the controller. Therefore the codes need to be twiked a little i have tried to done that for you

Visualforce Page
 

<apex:page docType="html-5.0" Controller="MultipleBookIssue"  >
    <apex:form >
        <apex:pageBlock title="Mass Issue Books" id="pb">
        
            <apex:pageBlockTable Value="{!listBookIssue}" var="issue" id="table" >
            
                <apex:column headerValue="Select">
                    <apex:inputCheckbox value="{!issue.isselected}"/>
                </apex:column>        
            
                <apex:column headerValue="Book" >
                    <div class="requiredInput">
                        <div class="requiredBlock"></div> 
                        <apex:inputText value="{!issue.bi.Book__c}" rendered="{!((issue.bi.Book__c==NULL && !errorEncountered) || issue.bi.Book__c!=NULL)}"/>
                        <apex:outputPanel rendered="{!(LEN(issue.bi.Book__c)<=0 && errorEncountered)}">
                            <apex:inputText styleClass="error" value="{!issue.bi.Book__c}" />
                            <div class="errorMsg"><strong>Error:</strong>&nbsp;You must enter a value</div>
                        </apex:outputPanel>
                    </div>
                </apex:column>
            
                <apex:column headerValue="Issued To" >
                    <div class="requiredInput">
                        <div class="requiredBlock"></div>
                        <apex:inputText value="{!issue.bi.Issued_To__c}" rendered="{!((issue.bi.Issued_To__c ==NULL && !errorEncountered) || issue.bi.Issued_To__c!=NULL)}"/>
                        <apex:outputPanel rendered="{!(LEN(issue.bi.Issued_To__c)<=0 && errorEncountered)}">
                            <apex:inputText styleClass="error" value="{!issue.bi.Issued_To__c}" />
                            <div class="errorMsg"><strong>Error:</strong>&nbsp;You must enter a value</div>
                        </apex:outputPanel>
                    </div>
                </apex:column>
                
                <apex:column headerValue="Issue Date" >
                    <div class="requiredInput">
                        <div class="requiredBlock"></div>
                        <apex:inputField value="{!issue.bi.Book_Issue_Date__c}" rendered="{!((issue.bi.Book_Issue_Date__c ==NULL && !errorEncountered) || issue.bi.Book_Issue_Date__c!=NULL)}"/>
                        <apex:outputPanel rendered="{!(issue.bi.Book_Issue_Date__c ==NULL && errorEncountered)}">
                            <apex:inputField type="date" value="{! issue.bi.Book_Issue_Date__c}" styleClass="error" />
                            <div class="errorMsg"><strong>Error:</strong>&nbsp;You must enter a value</div>
                        </apex:outputPanel>
                    </div>
                </apex:column>        
                
                
            </apex:pageBlockTable>
            
            <apex:pageBlockButtons location="Bottom" >
                <apex:commandButton value="Add Book Issue Row" action="{!addBookIssueRow}" rerender="pb" />
                <apex:commandButton value="Delete Book Issue Row" action="{!deleteBookIssueRow}" rerender="pb"/>
                <apex:commandButton action="{!saveBookIssue}" value="Save Book Issue" rerender="pb"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
 
</apex:page>
Controller
public class MultipleBookIssue {
    public list<wBookIssue> listBookIssue{get;set;}
    public boolean errorEncountered {get;set;}
    
    public MultipleBookIssue(){
        listBookIssue= new list<wBookIssue>();
        listBookIssue.add(new wBookIssue());
        errorEncountered = False;
    }
    
    public void deleteBookIssueRow(){
        Integer j=0;
        
        while(j<listBookIssue.size())
        {
            if(listBookIssue.get(j).isselected)
                listBookIssue.remove(j);
            else
                j++;
        }
    }
    
    public void addBookIssueRow(){
        listBookIssue.add(new wBookIssue());
    }
    
    public PageReference saveBookIssue()
    {
        List<Book_Issue__c> biList = new List<Book_Issue__c>();
        
        for(wBookIssue wbi : listBookIssue) {
            if(wbi.bi.Book__c==Null || wbi.bi.Book_Issue_Date__c ==Null || wbi.bi.Issued_To__c ==Null)
                errorEncountered = TRUE;
            else
                biList.add(wbi.bi);
            
        }
        if(!errorEncountered)
            insert biList;
        
        return Page.AllBooksIssued;
    }
    
    public class wBookIssue {
        public Book_Issue__c bi {get; set;}
        public boolean isselected {get; set;}
        public wBookIssue() {
            bi = new Book_Issue__c();
            isselected = false;
        }
    }
}


I hope it help.

NOTE: for this to work correctly 'Book_Issue_Date__c' field checkbox is to be unchecked at field level, because if we move by this it wont show datepicker; but if you could change that with the javascript it will for fine.

hope it helps !!!