Adding rows in visualforce page in PageBlocktable dynamically - salesforce

i am creating a dummy visualforce page whose code is
<apex:page controller="sampleCon">
<apex:form>
<apex:pageMessages id="message" />
<apex:pageBlock>
<apex:pageBlockTable value="{!wrap1}" var="item" id="hello">
<apex:column headerValue="String" value="{! item.x}"></apex:column>
<apex:column headerValue="Action"><apex:commandButton value="{!if(item.i == size-1,'Add','Delete')}" onclick="AddOrDelete({! item.i});" rerender="message"></apex:commandButton></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionRegion >
<apex:actionFunction name="AddOrDelete" action="{!addElement}" reRender="message,hello">
<apex:param value="" name="fieldName" />
</apex:actionFunction>
</apex:actionRegion>
</apex:form>
</apex:page>
and the controller code is
public class sampleCon {
public List<Wrapper> wrap1{get;set;}
public Integer size{get;set;}
public sampleCon(){
wrap1=new List<Wrapper>();
wrap1.add(new wrapper(0));
size=wrap1.size();
}
public PageReference addElement(){
size=wrap1.size();
System.debug('Hello ji'+Apexpages.currentPage().getParameters().get('fieldName')+'size is'+size);
Integer i=Integer.valueOf(ApexPages.currentPage().getParameters().get('fieldName'));
if(i == size-1)
{
wrap1.add(new wrapper(size));
size++;
}
return null;
}
public class wrapper{
public integer i{get;set;}
public String x{get;set;}
public wrapper(Integer p){
i=p;
}}
}
what i am trying is the last row button label should be Add and other button label should be Delete when i press add button a new row is added and the label should be changed acordingly for this i am adding row but it is not working as expected
initial page screenshot is
when i press add button screen look like this.
when i again press the Add button screen is looking like first screenshot.i was expecting that one more row will be added when i click add button in above screenshot.can any one please tell why only one row is showing when i click add button in second screen shot and how to correct it that when i again click on add button on above screen one more row will be added instead of showing initial screen.

I would suggest you take a list at below post:
http://boards.developerforce.com/t5/Visualforce-Development/Add-Row-functionality-in-VisualForce/m-p/358921
Hope this helps.

Related

In Visualforce, Does "rendered" reexecutes after "reRender" refreshes a section?

I am trying to hide the section 'searchCriteria' when 'Edit' link is clicked. The hiding is done in Controller 'editSearchResult()' method using 'pageLoad' variable.
The 'pageLoad' is used in 'searchCriteria' section with rendered attribute. But, the section is not refreshing with updated values from Controller when Edit link is clicked.
But if I remove 'rendered' attribute on the section 'searchCriteria', system refreshes the section with updated values from Controller. Can anyone explain how 'rendered' handles display?
Visualforce Code:
<apex:page controller="RerenderDemoController">
<apex:form id="thisForm">
<apex:outputPanel id="searchCriteria" rendered="{!pageLoad}">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Criteria Section. Page Load: {!pageLoad},
User: <apex:outputText value="{!userName}">
</apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="searchResults">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Results Section. {!accts}
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="EditResult">
<apex:pageBlock>
<apex:pageBlockSection>
This is Edit Result Section.
<apex:commandLink action="{!editSearchResult}" reRender="searchCriteria">Edit</apex:commandLink>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
Apex Controller:
public class RerenderDemoController {
public List<Account> accts {get; set;}
public Boolean pageLoad {get; set;}
public String userName {get; set;}
public RerenderDemoController() {
pageLoad = true;
userName = 'My First Name';
accts = [select id, name from Account limit 10];
}
public void editSearchResult() {
pageLoad = false;
accts = [select id, name from Account limit 20];
userName = 'My Last Name';
}
}
What you can do is, wrap your outputPanel with another outputPanel
<apex:outputPanel id="searchCriteria">
<apex:outputPanel rendered="{!pageLoad}">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Criteria Section. Page Load: {!pageLoad},
User: <apex:outputText value="{!userName}">
</apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>
Hope, this will solve your issue.
You can use JQuery to Hide the content just use html class and use [jquery]:https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
and use $('.classname').hide();
hope it helps you.

Passing values to nested apex page for editing

I have an apex page with custom extension. I am using pageblocktable to show all available records. Table has modify button, on click of button I am showing another nested apex page in a javascript dialog. Question is how to pass pageblocktable row values to nested page for editing purpose?
You can share same controller for both pages.
Page1:
<apex:page standardcontroller="Object__c" extensions="MyController">
<apex:column headerValue="Action">
<apex:commandLink action="{!goToPage2}" value="Edit" >
<apex:param name="rowId" assignTo="{!rowId}" value="{!item.Component.Id}" />
</apex:commandLink>
</apex:column>
</apex:page>
Page2:
<apex:page standardcontroller="Object__c" extensions="MyController">
{!objForPage2.Name}
</apex:page>
Controller:
public with sharing class MyController{
public string rowId {get;set;}
Object__c objForPage2;
public Pagereference goToPage2(){
for(Object__c obj : objectList){
if(obj.Id==rowId){
objForPage2= obj;
}
}
return Page.Page2;
}
}

reRender and rendered attribute in visualforce

i write a visualforce page with source code
<apex:page controller="MyController1">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="search">
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanel" id="theCommandLink"/>
<apex:outputPanel id="thePanelWrapper">
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">My div</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
and the MyController1 class is
public class MyController1{
public Boolean rend{get;set;}
public PageReference commandLinkAction(){
rend=true;
return null;
}
}
when i click on Advanced Search link nothing happens but i was expecting outputPanel with id "thePanel" should render.why it is not rendering please someone explain??
In moment that you click on the link the panel not on the page, SF not rendered it.
As #Shimshon said, when the HTML code is generated from Visualforce, the Visualforce components marked as rendered="false" are not displayed in the resulting HTML document.
In this case:
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">
if you are going to rerender this panel, then you must ensure that the component will be displayed in the HTML code so the rerender action can find it. Since {! rend} is initially set to false in the controller's constructor "thePanel" is never rendered in the page, thus you try to rerender a component that does not exist.
#theGreatDanton 's solution will work because <apex:outputPanel id="thePanelWrapper"> is the container panel that is always rendered:
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanelWrapper" id="theCommandLink"/>
and if this panel is pointed by rerender attribute, then "thePanelWrapper" and their child nodes ("thePanel") will be updated.
try the below code.
<apex:page controller="MyController1">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="search">
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanelWrapper" id="theCommandLink"/>
<apex:outputPanel id="thePanelWrapper">
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">My div</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class MyController1{
public Boolean rend{get;set;}
//setting the boolean to false in the constructor
public MyController1(){
rend = false;
}
public void commandLinkAction(){
rend=true;
// return null;
}
}
Hope this helps!!

parameter value null is redirected to url

Hi i am writing a simple controller class with code
public class OMTime1 {
public String xx { get; set; }
public PageReference continue1(){
String url='/apex/OneMoreTime?id='+ApexPages.currentPage().getParameters().get('id')+'&process='+xx;
PageReference page=new PageReference(url);
page.setRedirect(true);
return page;
}
}
and a simple VF Page with code
<apex:page controller="OMTime1" >
<!-- Begin Default Content REMOVE THIS -->
<!-- End Default Content REMOVE THIS -->
<apex:form >
<apex:selectList id="chooseColor" value="{!xx}" size="1">
<apex:selectOption itemValue="1" itemLabel="Terminate Resource"/>
<apex:selectOption itemValue="2"
itemLabel="Change Resource Position at Same Location"/>
<apex:selectOption itemValue="3" itemLabel="Change Resource Position with in same Location "/>
<apex:selectOption itemValue="4" itemLabel="Change Resource Position to a Different Location"/>
</apex:selectList>
</apex:form>
<apex:form >
<apex:commandButton value="Continue" action="{!continue1}" id="theButton"/>
</apex:form>
</apex:page>
my task s when user click on button name as Continue page will redirect to it a url /apex/OneMoreTime?id=record_id&process=some_value
some_value=1,2,3, or 4
but when i click on button it redirect to /apex/OneMoreTime?id=record_id&process=null
why this null is coming in parameter i want to redirect a value related to label of select option.please suggest way how to resolve it
Short answer: because <apex:commandButton> is broken ;)
Recently I've posted an explanation of 2 most common workarounds on Salesforce-dedicated stackexchange site: https://salesforce.stackexchange.com/questions/4937/command-button-vs-command-link
Use an <apex:commandLink styleClass="btn"> and it will work while looking almost the same as button.

display records based on selection of name from picklist in salesforce

I am trying to get my hands on learning Visual force.
I have an object inv_c which holds invoice records and another object item_c
I have in my VF page a picklist with the object names.
If user selects inv_c then all records of inv_c are displayed if user selects item__c all records of item are displayed
Is there any way where the list would be displayed on the completion of the selection or do we have to have button to get it.
how can i achieve this in VF? any small code snippet would be wonderful
Thanks
You can do this using a JavaScript onchange event with the help of the ActionSupport Visualforce Component. Here's an example.
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}
In your case the actionSupport component would be a child of your selectRadio component i.e.
<apex:selectRadio value="{!selection}">
<apex:selectOptions value="{!items}"/>
<apex:actionSupport event="onchange" .... />
</apex:selectRadio>

Resources