This post is just to expose the one of the way to sending a records/datas displayed in the visualforce page in to our apex controller, The data displaying in vf page is queried in the databased using the same apex controller, the point is to send the back the selected records from the overall queried records displayed in the visualforce page.
Generally this can be achieved by using wrapper class functionality of Apex, its a great functionality,as well as bit hard to understand and implement though, so below code is just some simple workaround to achieve this by the help of simple javascript.
The Below code just sending the selected records recordids to the apex controller seperated by commas, we can treat it as a set in apex class,the ids going to send can be displayed as alert message in visualforce page for the reference.
This is just the sample code to give you the idea, soon will post the complete workaround code.
VisualForce Code:
Apex Controller Code:
Generally this can be achieved by using wrapper class functionality of Apex, its a great functionality,as well as bit hard to understand and implement though, so below code is just some simple workaround to achieve this by the help of simple javascript.
The Below code just sending the selected records recordids to the apex controller seperated by commas, we can treat it as a set in apex class,the ids going to send can be displayed as alert message in visualforce page for the reference.
This is just the sample code to give you the idea, soon will post the complete workaround code.
VisualForce Code:
- <apex:page tabStyle="Task" sidebar="false" showHeader="false" id="page1" controller="clsSearchContact">
- <apex:form >
- <script>
- var flag=0;
- var SelectConId1='';
- function checkAll(cb)
- {
- flag=0;
- SelectConId1='';
- var inputElem = document.getElementsByTagName("input");
- for(var i=1; i<inputElem.length; i++)
- {
- if(inputElem[i].id.indexOf("checkedone")!=-1)
- {
- inputElem[i].checked = cb.checked;
- flag=flag+1;
- SelectConId1=SelectConId1+inputElem[i].name+',';
- }
- }
- if(cb.checked!=true)
- {
- SelectConId1="";
- flag=0;
- }
- alert(SelectConId1);
- }
- function checkone(cb)
- {
- var countchbox=0;
- if(cb.checked)
- {
- flag=flag+1;
- if((SelectConId1.length)<=1)
- {
- SelectConId1=cb.name+',';
- }
- else
- {
- SelectConId1=SelectConId1+cb.name+',';
- }
- }
- else if((cb.checked)!=true)
- {
- flag=flag-1;
- if((SelectConId1.length)<=1)
- {
- SelectConId1=cb.name+',';
- }
- else
- {
- var allids=SelectConId1.split(',');
- var idarray='';
- for(var i=0;i<allids.length;i++)
- {
- if(allids[i]!=cb.name && allids[i]!='')
- {
- idarray=idarray+allids[i]+',';
- }
- }
- SelectConId1=idarray;
- }
- }
- var inputElem = document.getElementsByTagName("input");
- for(var i=0; i<inputElem.length; i++)
- {
- if(inputElem[i].id.indexOf("checkedone"))
- {
- countchbox=countchbox+1;
- }
- }
- if(flag==countchbox-1)
- {
- document.getElementById("main").checked=true;
- }
- else
- {
- document.getElementById("main").checked=false;
- }
- alert(SelectConId1);
- }
- function search_element()
- {
- //alert('hello');
- var element=document.getElementById("searchtext").value;
- // alert(element);
- searchelement(element);
- return false;
- }
- function addtolist()
- {
- if((SelectConId1.length)<=1)
- {
- alert('Please select atleast one contact');
- return false;
- }
- else
- {
- addtolistcontact();
- }
- }
- </script>
- <apex:pageblock tabstyle="account">
- <apex:pageBlockSection title="Select Multiple Contact" collapsible="false" columns="1">
- <Apex:pageBlock >
- <apex:pageBlockButtons location="both" >
- <apex:outputLabel > </apex:outputLabel>
- <apex:commandButton onclick="return addtolist();" value="Add to List"/>
- <Apex:commandButton value="Done" onclick="return closethis()"/>
- </apex:pageBlockButtons>
- <apex:outputPanel >
- <input type="text" id="searchtext"/>
- <apex:actionFunction immediate="true" action="{!searchcontact}" reRender="pbtable"name="searchelement">
- <apex:param name="assignserach" value="" assignTo="{!searchfiled}"/>
- </apex:actionFunction>
- <apex:commandButton value="search" onclick="return search_element();" />
- </apex:outputPanel>
- <apex:pageBlockTable value="{!getconlist}" var="cc" id="pbtable">
- <apex:column >
- <apex:facet name="header">
- <input type="checkbox" id="main" onclick="return checkAll(this)" />
- </apex:facet>
- <input type="checkbox" name="{!cc.id}" id="checkedone" onclick="return checkone(this)" />
- </apex:column>
- <div id="{!cc.id}">
- <apex:column >
- <apex:facet name="header" >Name</apex:facet>
- {!cc.name}
- </apex:column>
- <apex:column >
- <apex:facet name="header" >Title</apex:facet>
- {!cc.Title}
- </apex:column>
- <apex:column >
- <apex:facet name="header" >Phone</apex:facet>
- {!cc.phone}
- </apex:column>
- <apex:column >
- <apex:facet name="header" >Email</apex:facet>
- {!cc.email}
- </apex:column>
- <apex:column >
- <apex:facet name="header" >Account/Company</apex:facet>
- {!cc.account.name}
- </apex:column>
- <apex:column >
- <apex:facet name="header" >Owner</apex:facet>
- {!cc.owner.name}
- </apex:column>
- </div>
- </apex:pageBlockTable>
- </Apex:pageBlock>
- </apex:pageBlockSection>
- </apex:pageblock>
- </apex:form>
- </apex:page>
Apex Controller Code:
- public class clsSearchContact
- {
- public string searchfiled{get;set;}
- public list<contact> getconlist{get;set;}
- public clsSearchContact()
- {
- getconlist =new list<contact>();
- getconlist=database.query('Select name,phone,email,account.name,title,owner.name from contact limit 10');
- }
- public void searchcontact()
- {
- try
- {
- searchfiled= '%'+searchfiled+ '%';
- getconlist =new list<contact>();
- getconlist=database.query('Select name,phone,email,account.name,title,owner.name from contact WHERE Name like :searchfiled limit 100');
- }
- catch(Exception e)
- {
- system.debug('Error'+e);
- }
- }
- }
No comments:
Post a Comment