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
VinuVinu 

How to Update selected records using inputCheckbox on a visualforce page

Hi,

 

I have created a visualforce page where i am displaying all the records together to update simultaneously,

 

But Instead of  updating all the records in a grid, I need to update only the selected fields by using the checkbox..

Please suggest the possible solution to do this task..

 

Thank you!!

Ritesh AswaneyRitesh Aswaney

Iterate through the collection of records in your controller, and add the records with the checkbox checked into another collection.

Then process the collection created as a result of above.

Teach_me_howTeach_me_how

can you post sample code that determine if checkbox  is equal to true.. thanks

VinuVinu

This is my Attendance update page, And currently it updates all the records together.

But my task is to update only the selected values through the checkbox, and i just created a UI checkbox only to select

and deselect.. Is it posssible to do update with the UI check boxes????

 

Here my code is, just have a look and give your suggestion,

 

This is what i used as a controller:

 

public class Checkbox_Class
{
public PageReference save()
{
update a;
return null;
}

public PageReference cancel()
{
return null;
}

List<accountwrapper> accountList = new List<accountwrapper>();
List<Attendance__c> selectedAccounts = new List<Attendance__c>();
List<Attendance__c> a = new List<Attendance__c>();

public List<accountwrapper> getAccounts()
{
for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c, Excuse_Details__c,
Present__c, Rubric__c,Session__c, Youth__r.LastName FROM Attendance__c
ORDER BY Youth__r.LastName asc limit 5])

accountList.add(new accountwrapper(a));
return accountList;
}

public PageReference getSelected()
{
selectedAccounts.clear();
for(accountwrapper accwrapper : accountList)
if(accwrapper.selected == true)
selectedAccounts.add(accwrapper.acc);
return null;
}

public List<Attendance__c> GetSelectedAccounts()
{
if(selectedAccounts.size()>0)
return selectedAccounts;
else
return null;

}

public class accountwrapper
{
public Attendance__c acc{get; set;}
public Boolean selected {get; set;}
public accountwrapper(Attendance__c a)
{
acc = a;
selected = false;
}
}
}

 

And the visualforce code is:

 

<apex:page controller="Checkbox_Class" Tabstyle="Contact" sidebar="false" showHeader="false">
<apex:form >

<apex:pageBlock Title="Selected value with CheckBoxes">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" OnClick="show_alert()" />
<apex:commandButton value="Cancel" action="{!cancel}" onclick="confirmcancel()"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!accounts}" var="a" >
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:facet>

<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:column>

<apex:column headervalue="Name" value="{!a.acc.Name}" />
<apex:column headervalue="Date" value="{!a.acc.date__c}" />

<apex:column headervalue="Excused Absence">
<apex:inputfield value="{!a.acc.Excused_Absence__c}" />
</apex:column>

<apex:column headervalue="Present">
<apex:inputfield value="{!a.acc.Present__c}" />
</apex:column>


<apex:column headervalue="Excuse Details">
<apex:inputField value="{!a.acc.Excuse_Details__c}" />
</apex:column>

<apex:column headervalue="Rubric(#)">
<apex:inputfield value="{!a.acc.Rubric__c}" />
</apex:column>

<apex:column headervalue="Session" value="{!a.acc.Session__c}" />
<apex:column headervalue="Youth Last Name" value="{!a.acc.Youth__r.LastName}" />
</apex:pageBlockTable>


</apex:pageBlock>
</apex:form>

<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>

</apex:page>