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
PzyrenPzyren 

Delete all checked elements from table

<apex:page controller="controller">
<apex:form >
 <apex:pageBlock title="foo"> 
<apex:pageBlockButtons location="top">       
         <apex:commandButton id="DeleteBtn" value="{!$Label.Delete}" />
   </apex:pageBlockButtons> 
  <apex:pageBlockTable value="{!list}" var="list" title="foo">
      <apex:column >
        <input type="checkbox" value="{!list.id}" id = "checkbox"/> 
   </apex:column>
   <apex:column>
      <apex:facet name="header"> <apex:outputText value="Name" /> </apex:facet>
        <apex:outputField value="{!foo.Name__c}" />
    </apex:column>
  
    <apex:column > 
      <apex:facet name="header"> <apex:outputText value="Type" /> </apex:facet>
        <apex:outputField value="{!foo.Type__c}" />
    </apex:column>
    <apex:column > 
      <apex:facet name="header"> <apex:outputText value="Comments" /> </apex:facet>
      <apex:outputField value="{!foo.Comment__c}" />
    </apex:column>
    
  </apex:pageBlockTable> 
 </apex:pageBlock>
</apex:form>
</apex:page>

 CONTROLLER. 

 

I want to be able to delete multiple rows out of the table. So delete all checked rows with he delete button. How do I do this? 

public with sharing class controller {

public List<sObjects> myList 	{get; set;}
public String pId		{get; set;}							
	
    //CONSTRUCTOR
    public controller(){
    	 pId = ApexPages.CurrentPage().getparameters().get('id');
    	 myList = loadlist(); 
    }
    
    public list<People_Skill__c> loadList(){
    	List<sObjects> myList= [SELECT Id, name__c, type__c, comment__c,    FROM object__c WHERE id=:pId];
    	return mylist;
    }
}

 

Puja_mfsiPuja_mfsi

Hi,

You need to use wrapper class to acheive uor purpose.

 

Controller:

public class DeleteElementTesting {
       List<wrapperClass> accList;
       public List<wrapperClass> getAccounts() {
               if( accList == null ) {
                    accList = new List<wrapperClass>();
                    for( Account acc : [select id,Name,type from Account]) {
                           accList.add( new wrapperClass( acc,false));
                   }
            }
            return accList;
       }

 

       public void doDelete() {
                List<Account> selectedAcc = new List<Account>();
                for( wrapperClass cs: getAccounts()) {
                      if(cs.isSelect) {
                           selectedAcc.add( cs.accObj ); 
                      }
                }
               delete selectedAcc;
                accList = null;
        }

       public class wrapperClass {
              public Account accObj{get;set;}
              public Boolean isSelect{get;set;}
              public wrapperClass( Account accObj, boolean isSelect ) {
                     this.accObj = accObj;
                     this.isSelect = isSelect;
              }
        }
}

 

Visual Force Page:

<apex:page controller="DeleteElementTesting">
       <apex:form>
             <apex:pageblock>
                       <apex:pageBlockButtons location="top">
                                 <apex:commandButton id="DeleteBtn" value="Delete" action="{!doDelete}" />
                        </apex:pageBlockButtons>
                        <apex:pageBlockTable value="{!Accounts}" var="acc">
                                  <apex:column >
                                          <apex:inputCheckbox value="{!acc.isSelect}"/>
                                  </apex:column>
                                  <apex:column>
                                        <apex:facet name="header"> <apex:outputText value="Name" /> </apex:facet>
                                        <apex:outputField value="{!acc.accObj.Name}" />
                                   </apex:column>
                                  <apex:column >
                                             <apex:facet name="header"> <apex:outputText value="Type" /> </apex:facet>
                                             <apex:outputField value="{!acc.accObj.Type}" />
                                  </apex:column>
                            </apex:pageblockTable>
                      </apex:pageblock>
               </apex:form>
 </apex:page>

 

for more information please go through the below link:

http://wiki.developerforce.com/page/Wrapper_Class

 

 

Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.

 

PzyrenPzyren

This work when the object is an account but when I am using a custom object, what goes into the value of the table? Because the list is a wrapper class list and so what should the table display? 

 

  <apex:pageBlockTable value="{!Accounts}" var="acc">

 

Sneha PatilSneha Patil

The value of the PageBlockTable should contain the wrapper list

 

In the above example it should be

 

  <apex:pageBlockTable value="{!accList}" var="acc">

 

Hope this answer hepls!!