I encounter this weird problem in visualforce page wherein for every checkbox click, there is an extra table row added, whether I tick/untick it. The table row is supposed to display only if I tick the checkbox, and hide if I untick it.
There's nothing wrong with displaying the table row whenever I tick the checkbox. This feature works. The only problem is that every time I untick it, it adds another table row, instead of hiding the said table row.
I use two objects: the standard Account object and a custom object named CustomObj__c.
Controller: AccountController
public class AccountController {
private ApexPages.StandardController controller {get;set;}
public Boolean b1 {get;set;}
public List<Account> accList {get;set;}
public List<CustomObj__c> customList {get;set;}
public List<MyWrapper> wrapCls {get;set;}
public AccountController(ApexPages.StandardController con) {
controller = con;
b1 = true;
wrapCls = new List<MyWrapper>():
customList = new List<CustomObj__c>();
}
public void clickAction() {
customList.add(new CustomObj__c());
}
public class MyWrapper {
public Account listA {get;set;}
public Boolean IsSelected {get;set;}
public MyWrapper(Boolean sel, Account a) {
IsSelected = sel;
listA = a;
}
}
}
Visualforce Page: AccountPage
<apex:page standardController="Account" extensions="AccountController">
<style type="text/css">
#blkToRender {
display: none;
}
</style>
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID) {
var inputCheckbox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckbox.length; i++) {
if(inputCheckbox[i].id.indexOf(receivedInputID)!=-1) {
inputCheckbox[i].checked = obj.checked;
}
}
}
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var $ = jQuery.noConflict();
$(document).ready(function() {
$('[id$=chk]').onclick(function() {
$('[id$=blkToRender]').css('display','block');
});
});
</script>
<apex:pageBlock id="pb1" rendered="{!b1}">
<apex:pageBlockTable value="{!wrapCls}" var="acc">
<apex:column>
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'chk')"/>
</apex:facet>
<apex:inputCheckbox value="{!acc.IsSelected}" id="chk">
<apex:actionSupport event="onclick" reRender="blkToRender" action={!clickAction}/>
</apex:inputCheckbox>
</apex:column>
<apex:column>
<apex:inputField value="{!acc.listA.Type}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlockTable id="blkToRender" value="{!customList}" var="cl">
<apex:column>
<apex:inputField value="{!cl.Type__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Updated Controller:
public class AccountController {
private ApexPages.StandardController controller {get;set;}
public Account returnAcc {get;set;}
public Boolean b1 {get;set;}
public List<Account> accList {get;set;}
public List<CustomObj__c> customList {get;set;}
public List<MyWrapper> wrapCls {get;set;}
public AccountController(ApexPages.StandardController con) {
controller = con;
b1 = true;
wrapCls = new List<MyWrapper>():
customList = new List<CustomObj__c>();
returnAcc = new Account();
}
public void accSearch() {
List<Account> accounts = new List<Account>();
List<MyWrapper> wraptemp = new List<MyWrapper>();
accounts = [SELECT Id, Name, CustomObj__c, Type FROM Account WHERE CustomObj__c =: returnAcc.CustomObj__c];
for(Account accloop : accounts) {
wraptemp.add(new MyWrapper(false,accloop));
}
if(wraptemp != null) {
wrapCls = wraptemp;
}
}
public void clickAction() {
customList.add(new CustomObj__c());
}
public class MyWrapper {
public Account listA {get;set;}
public Boolean IsSelected {get;set;}
public MyWrapper(Boolean sel, Account a) {
IsSelected = sel;
listA = a;
}
}
}
Updated VF Page:
<apex:page standardController="Account" extensions="AccountController">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID) {
var inputCheckbox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckbox.length; i++) {
if(inputCheckbox[i].id.indexOf(receivedInputID)!=-1) {
inputCheckbox[i].checked = obj.checked;
}
}
}
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function displayTable(obj) {
if(obj.checked) {
$('[id$=blkToRender]').css('display','block');
CallApexMethod();
}
else {
$('[id$=blkToRender]').css('display','none');
}
}
</script>
<apex:actionFunction name="CallApexMethod" action="{!clickAction}" rerender="blkToRender" oncomplete="alert(1);"/>
<apex:pageBlock id="pb1" rendered="{!b1}">
<apex:inputField value="{!returnAcc.CustomObj__c}">
<apex:actionSupport action="{!accSearch}" event="onchange" />
</apex:inputField>
<apex:pageBlockTable value="{!wrapCls}" var="acc">
<apex:column>
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'chk')"/>
</apex:facet>
<apex:inputCheckbox value="{!acc.IsSelected}" id="chk" onclick="displayTable(this)">
</apex:column>
<apex:column>
<apex:inputField value="{!acc.listA.Type}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlockTable id="blkToRender" value="{!customList}" var="cl">
<apex:column>
<apex:inputField value="{!cl.Type__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>


