+ Start a Discussion
wixxeywixxey 

Getting Specific rows in Datatable using JavaScript in Salesforce

I am working with visualforce pages. I am using datatable in page and i have used a checkbox in each row in datatable, now i want to get only those rows whose checkbox values is true, the code is given below.

 

<apex:page controller="searchDuplicate">
<apex:pageBlock title="Searching for Duplicate Contacts Record">
 <center>
     <apex:form>
         <apex:dataTable id="dTable" value="{!selectedContactList}" var="cn" border="1" cellpadding="5">
            <apex:column headerValue="Name" value="{!cn.Name}" />
            <apex:column headerValue="Email" value="{!cn.Email}" />
            <apex:column headerValue="Select">
                <apex:inputCheckbox/>
            </apex:column>
         </apex:dataTable>
         <apex:commandButton value= "Delete Selected"/>
    </apex:form>
 </center>
</apex:pageBlock> 
</apex:page>

 kindly help

 

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

You have to take one string variable "strSelectedIds" in your controller and using javascript you can get the value of selected contactid to this variable in your controller.

 

Public sting strSelectedIds {get; set;}

 

<apex:page controller="searchDuplicate">
	<script>
		function checkOne(cb){                    
			var selectedIds=document.getElementById("{!$Component.pbSearchCon.frm.hdnSelectedIds}").value;       
			if(selectedIds.indexOf(cb.title)!=-1){
				selectedIds=selectedIds.replace(cb.title+",","");
			}else{
				selectedIds=selectedIds+cb.title+",";
			}        
			document.getElementById("{!$Component.pbSearchCon.frm.hdnSelectedIds}").value=selectedIds;
		}
	</script>
	<apex:pageBlock id="pbSearchCon" title="Searching for Duplicate Contacts Record">	
	 <center>
		 <apex:form id="frm">
			 <apex:inputhidden id="hdnSelectedIds" value="{!strSelectedIds}"/> 
			 <apex:dataTable id="dTable" value="{!selectedContactList}" var="cn" border="1" cellpadding="5">
				<apex:column headerValue="Name" value="{!cn.Name}" />
				<apex:column headerValue="Email" value="{!cn.Email}" />
				<apex:column headerValue="Select">
					<apex:inputCheckbox title="{!cn.id}" onClick="checkOne(this);"/>
				</apex:column>
			 </apex:dataTable>
			 <apex:commandButton value= "Delete Selected"/>
		</apex:form>
	 </center>
	</apex:pageBlock> 
</apex:page>

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

Hi,

 

You have to take one string variable "strSelectedIds" in your controller and using javascript you can get the value of selected contactid to this variable in your controller.

 

Public sting strSelectedIds {get; set;}

 

<apex:page controller="searchDuplicate">
	<script>
		function checkOne(cb){                    
			var selectedIds=document.getElementById("{!$Component.pbSearchCon.frm.hdnSelectedIds}").value;       
			if(selectedIds.indexOf(cb.title)!=-1){
				selectedIds=selectedIds.replace(cb.title+",","");
			}else{
				selectedIds=selectedIds+cb.title+",";
			}        
			document.getElementById("{!$Component.pbSearchCon.frm.hdnSelectedIds}").value=selectedIds;
		}
	</script>
	<apex:pageBlock id="pbSearchCon" title="Searching for Duplicate Contacts Record">	
	 <center>
		 <apex:form id="frm">
			 <apex:inputhidden id="hdnSelectedIds" value="{!strSelectedIds}"/> 
			 <apex:dataTable id="dTable" value="{!selectedContactList}" var="cn" border="1" cellpadding="5">
				<apex:column headerValue="Name" value="{!cn.Name}" />
				<apex:column headerValue="Email" value="{!cn.Email}" />
				<apex:column headerValue="Select">
					<apex:inputCheckbox title="{!cn.id}" onClick="checkOne(this);"/>
				</apex:column>
			 </apex:dataTable>
			 <apex:commandButton value= "Delete Selected"/>
		</apex:form>
	 </center>
	</apex:pageBlock> 
</apex:page>

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
Bhawani SharmaBhawani Sharma
You can go with the warpper approach. It's easy.
http://wiki.developerforce.com/page/Wrapper_Class