You need to sign in to do that
Don't have an account?
Rishab 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>
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.
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
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.
Before Pressing Delete Row Button
after
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
ControllerVisualforce Page
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 !!!