You need to sign in to do that
Don't have an account?
Subramani_SFDC
Need help for checkboxes selection
i have the wrapeer class for checkbox....without that my pagination,sorting,searching functionality is working fine.....After adding checkbox is pageblock table.....all the above functions are not working.....anybody can help this??? i am using standardsetcontroller......
i got the error like this
- Value 'false' cannot be converted from Boolean to lib.id.ID
Checkbox can only be used to bind BOOLEAN variables.
Can you post your code here?
Here's an example that uses checkbox on a pageblock table using wrapper class and it works fine:
Checkbox in a wrapper class
thanks,
controller class
public with sharing class SearchProduct {
public list<Product2> searchResults { get; set;}
public string searchText {get;set;}
// Variables required for Sorting.
public String soql {get;set;}
public string recordid {get; set;}
public List <Product2> ProductList1 = New List <Product2>();
//public List<pProduct2> ProductList {get; set;}
public String soqlsort {get;set;}
public List <Product2> ProductList2 = New List <Product2>();
public String allchecked { get; set; }
private Set<Id> selectedproductIds;
// item in context from the page
public String contextItem{get;set;}
//private ApexPages.StandardSetController Con;
public SearchProduct()
{
}
public void doSelectItem(){
this.selectedproductIds.add(this.contextItem);
}
// handle item deselected
public void SelectAllItems()
{
//this.selectedproductIds.clear();
//List<productlistdisplay> LstProd=getproducts();
/*
for(LstProd)
{
this.selectedproductIds.add(this.ProductId);
}
*/
}
public void doDeselectItem(){
this.selectedproductIds.remove(this.contextItem);
}
public Integer getSelectedCount(){
return this.selectedproductIds.size();
}
// fired when the search button is clicked
public PageReference search()
{
//String qry = new List<Product2>(); // init the list if it is null
if(SearchText!= null)
{
String qry ='Select id,Name,ProductCode,List_Price__c,Family,Description from Product2 WHERE Name LIKE \'%'+searchText+'%\'OR ProductCode LIKE \'%'+searchText+'%\' order by Name';
this.con = new ApexPages.StandardSetController(Database.query(qry));
this.con.setPageSize(10);
this.con.setPageNumber(1);
//this.selectedproductIds= new Set<Id>();
}
else
{
this.con=null;
}
return null;
}
public PageReference show()
{
product2 products=new product2();
System.debug('-----------'+products.id);
pagereference ref= new pagereference('/'+recordid);
ref.setredirect(false);
return ref;
}
public PageReference save() {
// id oppid;
opportunity opp= [select id,Name from opportunity where id = :System.currentPageReference().getParameters().get('id')];
List<OppLineItem__c> Recordstosave = new List<OppLineItem__c>();
for(productlistdisplay Lstselected :Selectedprodlist){
OppLineItem__c OLI = new OppLineItem__c();
OLI.LineDescription__c = Lstselected.LineDes;
OLI.Pro_Quantity__c = Lstselected.Quantity;
OLI.List_Price__c = Lstselected.prod.List_Price__c;
OLI.Sales_Price__c= Lstselected.prod.List_Price__c;
//OLI.Date__c=Date.ValueOf(Opp_Date);
OLI.Product__c=Lstselected.prod.Name;
OLI.Opportunity__c=opp.id;
OLI.Product_Code__c=Lstselected.prod.ProductCode;
Recordstosave.add(OLI);
}
insert Recordstosave;
PageReference pageRef= new PageReference('https://ap1.salesforce.com/00690000009q8RY');
pageRef.setredirect(true);
return pageRef;
}
// List used in to display the table in VF page.
//public List<Product2> getProductList() {
// Passing the values of list to VF page.
//return con.getRecords();
//}
// instantiate the StandardSetController from a query locator
public ApexPages.StandardSetController con {
get {
if(con == null) {
this.selectedproductIds= new Set<Id>();
// String Query to have a list of Products for a respective End-user.
soql = 'SELECT id,Name,ProductCode,Family,List_Price__c,Description FROM Product2';
// Passing the String array to a list with Selected field sorting.
ProductList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
this.con = new ApexPages.StandardSetController(ProductList1);
// sets the number of records in each page set
this.con.setPageSize(10);
this.con.setPageNumber(1);
}
return con;
}
set;
}
// indicates whether there are more records after the current page set.
public Boolean hasNext {
get {
return con.getHasNext();
}
set;
}
// indicates whether there are more records before the current page set.
public Boolean hasPrevious {
get {
return con.getHasPrevious();
}
set;
}
// returns the page number of the current page set
public Integer pageNumber {
get {
return con.getPageNumber();
}
set;
}
Public Integer getTotalPages(){
Decimal totalSize = this.con.getResultSize();
Decimal pageSize = this.con.getPageSize();
Decimal pages = totalSize/pageSize;
return (Integer)pages.round(System.RoundingMode.CEILING);
}
// returns the first page of records
public void first() {
con.first();
}
// returns the last page of records
public void last() {
con.last();
}
// returns the previous page of records
public void previous() {
con.previous();
}
// returns the next page of records
public void next() {
con.next();
}
// returns the PageReference of the original page, if known, or the home page.
public void cancel() {
con.cancel();
}
public PageReference AddProductToOpp()
{
PageReference pageRef= new PageReference('/apex/AddProductToOpp');
pageRef.setredirect(false);
return pageRef;
}
//Toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again for sorting other columns
soqlsort ='SELECT id,Name,ProductCode,Family,List_Price__c,Description FROM Product2';
// Adding String array to a List array
ProductList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir );
// Adding Caselist to Standard Pagination controller variable
con = new ApexPages.StandardSetController(ProductList2 );
// Set Page Size to 5
con.setPageSize(5);
}
// the current sort direction. defaults to asc
public String sortDir {
// To set a Direction either in ascending order or descending order.
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir;}
set;
}
// the current field to sort by. defaults to last name
public String sortField {
// To set a Field for sorting.
get { if (sortField == null) {sortField = 'Name'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
//Wrapper Class for checkbox selection
public list<productlistdisplay> listproduct{get;set;}
public List<productlistdisplay> getproducts(){
listproduct= new List<productlistdisplay>();
for(product2 p : (List<product2>)this.Con.getRecords()){
//product2 p = (product2)pr;
productlistdisplay pd = new productlistdisplay(p);
if(this.selectedproductIds.contains(p.Id)){
pd.check=true;
}
listproduct.add(pd);
//else{
//pd.check=false;}
}
return listproduct;
}
public list<productlistdisplay> SelectedprodList{get;set;}
public list<productlistdisplay> getSelectedproducts()
{
selectedprodList = new list<productlistdisplay >();
for(productlistdisplay pCon:getproducts())
{
if(pCon.Check == true)
{
selectedprodList.add(pCon);
}
}
system.debug('#### SELETCED'+selectedprodList.size());
return selectedprodList;
}
public class productlistdisplay{
public product2 prod{get;set;}
public decimal Quantity{get;set;}
public date Opp_Date{get;set;}
public string LineDes{get;set;}
public double Sales_Price{get;set;}
public boolean check{get;set;}
public productlistdisplay( product2 p){
this.prod=p;
check=false;
}
}
}
Visual Force
<apex:page controller="SearchProduct" tabStyle="Opportunity" Sidebar="false">
<apex:sectionHeader title="Product Slection for" subtitle="Opportunity" />
<div>
Add products to this opportunity from Standard price book.
</div>
<style> .aDiff { color: red; } </style>
<!-- style class To make a button as center -->
<style>
.wrapper {
text-align: center;
}
.button {
position: absolute:
top: 50%;
}
</style>
<apex:form id="form" >
<apex:messages style="color:red"/>
<!-- function to handle checkbox selection -->
<script type="text/javascript">
function doCheckboxChange(cb,itemId){
if(cb.checked==true){
//alert('checed'+itemId);
aSelectItem(itemId);
}
else{
aDeselectItem(itemId);
}
}
function checkAll(cb,cbid)
{
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:pageBlock id="Select" tabStyle="Opportunity">
<div class="wrapper">
<apex:commandButton action="{!AddProductToOpp}" value="Select"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</div>
</apex:pageBlock>
<apex:pageBlock Title="Find Products" mode="edit" id="block" >
<p> Enter your Product Code or Product Name, then click Search to begin your search. Search results include all records based on your keyword.</p><br> </br>
<apex:outputPanel >
<apex:outputLabel for="searchText"> <h3>By Keyword</h3></apex:outputLabel><br></br>
<apex:inputText id="searchText" value="{!searchText}"/><br></br>
<apex:commandButton value="Search" action="{!search}" rerender="ProductTable" status="status"/>
<apex:actionStatus id="status" startText="Searching... please wait..."/>
</apex:outputPanel>
</apex:pageBlock>
<apex:pageBlock >
<apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="refresh">
<apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
</apex:actionFunction>
<apex:actionFunction name="aSelectAllItems" action="{!SelectAllItems}" rerender="refresh"/>
<!-- handle deselected item -->
<apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}">
<apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
</apex:actionFunction>
<apex:pageBlockTable value="{!products}" var="ProList" align="centre" id="ProductTable" columns="10">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox value="{!ProList.check}" onclick="checkAll(this,'checkedone')" style="background-color:lightblue;" />
</apex:facet>
<apex:inputCheckbox value="{!ProList.check}" id="checkedone" onchange="doCheckboxChange(this,'{!ProList.prod.Id}')" />
</apex:column>
<apex:column >
<apex:facet name="header" >
<apex:commandLink action="{!toggleSort}" rerender="ProductTable">
<apex:outputText value="Product Name" />
<apex:outputText escape="false" rendered="{!sortField =='Name'&&sortDir=='asc'}">
<img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
</apex:outputText>
<apex:outputText escape="false" rendered="{!sortField =='Name'&&sortDir=='desc'}">
<img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
</apex:outputText>
<apex:param value="Name" name="column" assignTo="{!sortField }" />
<apex:param value="{!IF(sortDir=='asc'&&sortField =='Name', 'asc', 'desc')}" name="direction" assignTo="{!sortDir}" />
</apex:commandLink>
</apex:facet>
<apex:commandlink value="{!ProList.prod.Name}" action="{!show}" rerender="ProductTable" >
<apex:param value="{!ProList.prod.id}" assignTo="{!recordid}" name="recordid"/>
</apex:commandLink>
</apex:column>
<apex:column >
<apex:facet name="header" >
<apex:commandLink action="{!toggleSort}" rerender="ProductTable">
<apex:outputText value="Product Code" />
<apex:outputText escape="false" rendered="{!sortField =='ProductCode'&&sortDir=='asc'}">
<img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
</apex:outputText>
<apex:outputText escape="false" rendered="{!sortField =='ProductCode'&&sortDir=='desc'}">
<img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
</apex:outputText>
<apex:param value="ProductCode" name="column" assignTo="{!sortField }" />
<apex:param value="{!IF(sortDir=='asc'&&sortField =='ProductCode', 'asc', 'desc')}" name="direction" assignTo="{!sortDir}" />
</apex:commandLink>
</apex:facet>
<apex:commandlink value="{!ProList.prod.ProductCode}" action="{!show}" rerender="ProductTable">
<apex:param value="{!ProList.prod.id}" assignTo="{!recordid}" name="recordid"/>
</apex:commandLink>
</apex:column>
<apex:column >
<apex:facet name="header" >
<apex:commandLink action="{!toggleSort}" rerender="ProductTable">
<apex:outputText value="List Price" />
<apex:outputText escape="false" rendered="{!sortField =='List_Price__c '&&sortDir=='asc'}">
<img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
</apex:outputText>
<apex:outputText escape="false" rendered="{!sortField =='List_Price__c '&&sortDir=='desc'}">
<img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
</apex:outputText>
<apex:param value="List_Price__c " name="column" assignTo="{!sortField }" />
<apex:param value="{!IF(sortDir=='asc'&&sortField =='List_Price__c ', 'asc', 'desc')}" name="direction" assignTo="{!sortDir}" />
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!ProList.prod.List_Price__c }"/>
</apex:column>
<apex:column >
<apex:facet name="header" >
<apex:commandLink action="{!toggleSort}" rerender="ProductTable">
<apex:outputText value="Product Family" />
<apex:outputText escape="false" rendered="{!sortField =='Family'&&sortDir=='asc'}">
<img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
</apex:outputText>
<apex:outputText escape="false" rendered="{!sortField =='Family'&&sortDir=='desc'}">
<img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
</apex:outputText>
<apex:param value="Family" name="column" assignTo="{!sortField }" />
<apex:param value="{!IF(sortDir=='asc'&&sortField =='Family', 'asc', 'desc')}" name="direction" assignTo="{!sortDir}" />
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!ProList.prod.Family}"/>
</apex:column>
<apex:column >
<apex:facet name="header" >
<apex:commandLink action="{!toggleSort}" rerender="ProductTable">
<apex:outputText value="Product Description" />
<apex:outputText escape="false" rendered="{!sortField =='Description'&&sortDir=='asc'}">
<img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
</apex:outputText>
<apex:outputText escape="false" rendered="{!sortField =='Description'&&sortDir=='desc'}">
<img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
</apex:outputText>
<apex:param value="Description" name="column" assignTo="{!sortField }" />
<apex:param value="{!IF(sortDir=='asc'&&sortField =='Description', 'asc', 'desc')}" name="direction" assignTo="{!sortDir}" />
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!ProList.prod.Description}"/>
</apex:column>
</apex:pageBlockTable>
<apex:panelGrid columns="6" id="refresh">
<apex:outputLabel value="[{!selectedCount} records selected]" />
<apex:commandLink action="{!first}">First</apex:commandlink>
<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
<apex:commandLink action="{!last}">Last</apex:commandlink>
<apex:outputLabel value="(page {!pageNumber} of {!totalPages})" styleClass="aDiff"/>
</apex:panelGrid>
</apex:pageBlock>
<apex:pageBlock title="Debug - SOQL" id="debug">
<apex:outputText value="{!debugSoql}" />
</apex:pageBlock>
</apex:form>
</apex:page>