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
r1985r1985 

Choosing a default value from a list of records in a table

Hi,

 

I have a requirement to display list of records from a object in tabular format. Every values will have a checkbox so that user can select a default value. The problem i am facing here is, the user can select only one value as default value. Once the default value is selected, other check boxes need to be unchecked automatically. Kindly let me know how this req can be done since in pageblocktable there is a possiblity of choosing multiple values.

 

Thanks in advance,

MVP

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference

<apex:page controller="RelatedChildContact" tabStyle="Account">

<script>
function CloseWindow()
{
alert('client');
// window.parent.close();
// OR

var winMain=window.parent.opener;
// alert("window.parent.opener ="+window.parent.opener);
// alert("window.parent.opener.location ="+window.parent.opener.location);
winMain.HelloFromChild();
window.top.close();


}

function CloseWindow2(st)
{
// alert(st);
if(st!='no record selected')
{
// alert('IDs with separator'+st);
window.parent.opener.document.getElementById("str1").value=st;
window.parent.opener.displayContact(st);
window.top.close();

}
else
{

alert('Please select atleast one contact to be displayed');
window.reload();
}

}
function CloseWindow3(st)
{
// alert(st);
if(st!='no record selected')
{
// alert('IDs with separator'+st);
window.parent.opener.displayContact2(st);
window.top.close();

}
else
{

alert('Please select atleast one contact to be displayed');
window.reload();
}

}

function SelectedID(checkedIdd,i)
{

alert('hi'+checkedIdd+'__i'+i);

return false;
}

</script>
<apex:form >

<apex:pageBlock Title="Contacts with CheckBoxes">


<apex:pageBlockSection Title="List of Available Contacts">

<!--<apex:dataTable value="{!Contacts}" var="ct1" columnswidth="50px,50px" cellpadding="4" border="1">-->

<apex:pageBlockTable value="{!Contacts}" var="ct1" columnswidth="50px,50px" cellpadding="4" border="1">
<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="{!ct1.selected}" id="checkedone">

<!-- <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="SelectedID('{!ct1.con.id}','{!count}');" rerender="Selected_PBS"/> -->
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox></apex:column>

<apex:column headervalue="Contact Firstname" value="{!ct1.con.firstname}" />

<apex:column headervalue="Contact Lastname" value="{!ct1.con.lastname}" />

<apex:column headervalue="Phone" value="{!ct1.con.Phone}" />


<!--</apex:dataTable>-->

</apex:pageBlockTable>
</apex:pageBlockSection>

<!-- /* New Section */ -->

<apex:pageBlockSection Title="Selected Contacts" id="Selected_PBS">

<apex:dataTable value="{!SelectedContacts}" var="c" columnswidth="50px,50px" cellpadding="4" border="1">

<apex:column headervalue="Contact Name" value="{!c.Firstname}" />

<apex:column headervalue="Contact LastName" value="{!c.lastname}" />

<apex:column headervalue="Phone" value="{!c.Phone}" />

<apex:column headervalue="Email" value="{!c.Email}" />

</apex:dataTable>

</apex:pageBlockSection>
</apex:pageBlock>

</apex:form>

<script>

function checkAll(cb)

{
//alert('__cb'+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>
====================Controller================================

 

public with sharing class RelatedChildContact {


public String param1 { get; set; }

public String str{get;set;}

public RelatedChildContact ()
{
param1=ApexPages.currentPage().getParameters().get('q');
system.debug('____param1'+param1);
}

List<contactwrapper> contactList = new List<contactwrapper>();

public List<contactwrapper> getContacts()

{
contactList.clear();
for(Contact c: [SELECT id,firstname,lastname,email,Phone from contact Limit 10])

contactList.add(new contactwrapper(c));

return contactList;

}



List<Contact> SelectedContacts= new List<Contact>();


public PageReference getSelected()
{


SelectedContacts.clear();

for(contactwrapper conwrapper : contactList )

if(conwrapper.selected == true)
{
SelectedContacts.add(conwrapper.con);

}

return null;

}

 

public List<Contact> GetSelectedContacts()

{
System.debug('______SelectedContacts.size()'+SelectedContacts.size());
if(SelectedContacts.size()>0)

return SelectedContacts;

else

return null;

}

public void Retrieve()
{
str=null;
System.debug('___R___SelectedContacts.size()'+SelectedContacts.size());
if(SelectedContacts.size()>0)
{
for(Integer i=0;i<SelectedContacts.size();i++)
{
System.debug('__'+i);
if(str==null)
str=SelectedContacts[i].id;
else
{
// if(!(str.contains(SelectedContacts[i].id)))
str=str+'####'+SelectedContacts[i].id;

}
}
}
else
{
str='no record selected';
}
system.debug('______str'+str);
}

public class contactwrapper

{

public Contact con{get; set;}

public Boolean selected {get; set;}

public contactwrapper(Contact ctt)

{

con= ctt;

selected = false;

}

}

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.