Checkbox in DataTable (salesforce)   Leave a comment

Page Code as follows:

view sourceprint?
01 <apex:page controller="Checkbox_Class" Tabstyle="Account">
02 <apex:form>
03
04 <!--
05 * This source code was written by SrinivasaRao Pendala.
06 * I have made every effort and taken great care in making sure that the source
07 * code and other content included on my wiki is technically accurate.
08 * As with any code, ensure to test this code in a development environment
09 * before attempting to run it in production.
10 -->
11
12 <apex:pageBlock Title="Accounts with CheckBoxes">
13 <apex:pageBlockSection Title="List of Available Accounts">
14 <apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
15 <apex:column >
16 <apex:facet name="header"> <apex:inputCheckbox >
17 <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
18 </apex:inputCheckbox></apex:facet>
19 <apex:inputCheckbox value="{!a.selected}" id="checkedone">
20 <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
21 </apex:inputCheckbox></apex:column>
22 <apex:column headervalue="Account Name" value="{!a.acc.Name}" />
23 <apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />
24 <apex:column headervalue="Phone" value="{!a.acc.Phone}" />
25 </apex:dataTable>
26 </apex:pageBlockSection>
27
28 <apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS">
29 <apex:dataTable value="{!SelectedAccounts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
30 <apex:column headervalue="Account Name" value="{!s.Name}" />
31 <apex:column headervalue="Account Number" value="{!s.AccountNumber}" />
32 <apex:column headervalue="Phone" value="{!s.Phone}" />
33 </apex:dataTable>
34 </apex:pageBlockSection>
35
36 </apex:pageBlock>
37 </apex:form>
38 <script>
39 function checkAll(cb)
40 {
41 var inputElem = document.getElementsByTagName("input");
42 for(var i=0; i<inputElem.length; i++)
43 {
44 if(inputElem[i].id.indexOf("checkedone")!=-1)
45 inputElem[i].checked = cb.checked;
46 }
47 }
48 </script>
49 </apex:page>And then the Controller Code is:

01 public class Checkbox_Class
02 {
03
04 /*
05 * This source code was written by SrinivasaRao Pendala.
06 * I have made every effort and taken great care in making sure that the source
07 * code and other content included on my wiki is technically accurate.
08 * As with any code, ensure to test this code in a development environment
09 * before attempting to run it in production.
10 */
11
12 List<accountwrapper> accountList = new List<accountwrapper>();
13 List<Account> selectedAccounts = new List<Account>();
14
15 public List<accountwrapper> getAccounts()
16 {
17 for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 5])
18 accountList.add(new accountwrapper(a));
19 return accountList;
20 }
21
22 public PageReference getSelected()
23 {
24 selectedAccounts.clear();
25 for(accountwrapper accwrapper : accountList)
26 if(accwrapper.selected == true)
27 selectedAccounts.add(accwrapper.acc);
28 return null;
29 }
30
31 public List<Account> GetSelectedAccounts()
32 {
33 if(selectedAccounts.size()>0)
34 return selectedAccounts;
35 else
36 return null;
37 }
38
39 public class accountwrapper
40 {
41 public Account acc{get; set;}
42 public Boolean selected {get; set;}
43 public accountwrapper(Account a)
44 {
45 acc = a;
46 selected = false;
47 }
48 }
49 }http://wiki.developerforce.com/index.php/Checkbox_in_DataTable
Advertisements

Posted 2010年12月30日 by gw8310 in salesforce

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: