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!!

raseshtcsraseshtcs

You will need to write a wrapper class to include the object that you are updating and a boolean variable which will take the value (True/False) from the checkbox of the corresponding record of the object. So while displaying the records on the VF page you will have to use the instance of the wrapper class.

VinuVinu

Thank you raseshtcs,

 

And i have a another doubt regarding to create a checkboxes.

 

At present i just created a UI checkbox and Since this is not a one of the field in my object. Is it compulsary to create a individual checkbox field to check whether the condition is TRUE or FALSE?

or we can simply do it through by the UI fields?

raseshtcsraseshtcs

No you would not need to create a field on the object, you can use a UI checkbox only. You just need to bind the check box with a wrapper class which would contain two attributes, a boolean variable tied to the checkbox on the page and obejct tied to the corresponding row. Following link although is about Pagination, but will help you for sure.

 

http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/

 

Chamil MadusankaChamil Madusanka

Here is example for wrapper class and page block table with checkBox

 

public class OuterClass
{
List<WrapperClass> wrapperList = new List<WrapperClass>();

public class WrapperClass
{
	public YourObject__c obj{get;set;}
	public Boolean checked{get;set;}
	public WrapperClass()
	{
		obj = new YourObject__c();
		checked = false;
	}
}

}
<apex:pageBlockTable value="{!wrapperList}" var="list">
    <apex:column value="{!list.obj.Field1}"/>
	<apex:column value="{!list.obj.Field1}"/>
	<apex:column value="{!list.checked}"/>
</apex:pageBlockTable>

 

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

 

 

 

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>

chinnu 5chinnu 5
Hi,
i m trying to create attendance tracking for school children can any one sujjest me for the data modeling ,and how to track 200+ students everday attendance
thanks