display fields based on selection on a picklist selection - salesforce

I have a VF page where i am using a apex:pageblocktable to display bunch of records.
One of the columns is a picklist and i need to display/not display fields according to selection on the picklist.
<apex:pageBlockTable value="{!showRecord}" var="item">
<apex:column headerValue="Delivery">
<apex:inputField value="{!item.delivery__c}"/>
</apex:column>
<apex:column headerValue="Roadway">
<apex:inputField value="{!item.road__c}"/>
</apex:column>
<apex:column headerValue="Rail">
<apex:inputField value="{!item.rail__c}"/>
</apex:column>
</apex:pageBlockTable>
in the above code delivery_c is picklist with values roadways and railways. if the user selects roadways then i need to display road--c and if user selects railways then i need to display rail_c
How can i go about doing that?
Thanks

One way to do this would be to use partial-page refreshes in Visualforce.
Put both fields in the same column and use the "rendered" attribute to dynamically show/hide the field using an if-statement. Then you set up an AJAX onchange event handler for the delivery__c field using the actionSupport tag. This will basically listen for that field to change then refresh the table on the page. Each time this is refreshed, your if statements will be re-evaluated and result in showing one of the two fields in that column.
I didn't get a chance to try this but I think it should work.
<apex:pageBlockTable id="mytable" value="{!showRecord}" var="item">
<apex:column headerValue="Delivery">
<apex:actionRegion>
<apex:inputField value="{!item.delivery__c}">
<apex:actionSupport event="onchange" reRender="mytable">
</apex:inputField>
</apex:actionRegion>
</apex:column>
<apex:column headerValue="Delivery Type">
<apex:inputField rendered="{!item.delivery__c='Road'}" value="{!item.road__c}"/>
<apex:inputField rendered="{!item.delivery__c='Rail'}" value="{!item.rail__c}"/>
</apex:column>
</apex:pageBlockTable>

Related

Salesforce show all cases based on criteria in visualforce

All,
I am attempting to show all cases that meet certain criteria in salesforce using visual force page, then I would like to use that data in a gantt chart. I do know much about coding, but trying based on user manuals. This visualforce page comes back without any case information.
<apex:page standardController="Case" >
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:PageBlockTable value="{!Case}" var="c">
<apex:column value="{!c.Account}"/>
<apex:column value="{!c.Number}"/>
<apex:column value="{!c.Owner}"/>
<apex:column headerValue="Install Date">
<apex:inputField value="{!a.Planned_Install_Date__c}"/>
</apex:column>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
You need an apex controller class to get data from Salesforce to the visual force page. Something like this:
Visual Page:
<apex:page standardController="Case" controller="MyCaseController">
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:PageBlockTable value="{!Cases}" var="c">
<apex:column value="{!c.Accountid}"/>
<apex:column value="{!c.CaseNumber}"/>
<apex:column value="{!c.OwnerId }"/>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
Controller:
public class MyCaseController {
public list<Case> cases {get;set;}
public MyCaseController(){
cases = [select id, accountid, CaseNumber, OwnerId from case];
}
}
You should be able to accommodate the code to your needs.

How do I get a "New" button on my custom apex:pageBlockTable for Cases?

I have a custom Cases apex page where I use a apex:pageBlockTable to display my list of Cases. Here is the code for it:
<apex:page standardController="Case" recordSetVar="Case" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="Cases">
<apex:outputLabel value="View:"/>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" rerender="cases_table"/>
<apex:selectOptions value="{!listviewoptions}"/>
</apex:selectList>
<apex:pageBlock >
<apex:pageBlockButtons >
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
<apex:column >
<a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
<apex:facet name="header">Case Number</apex:facet>
</apex:column>
<apex:column value="{!c.ContactId}" />
<apex:column value="{!c.Subject}" />
<apex:column value="{!c.Status}" />
<apex:column value="{!c.Priority}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
<base target="_parent"/>
</apex:page>
I would like to get a button inside my apex:pageBlockButtons like in the default Cases page. When the user clicks the button, I would like it to take the user to the new Cases page. I tried this:
<apex:commandButton action="{!new}" value="New"/>
but that gave me an error. How do I make a button that will take me to the new Cases page?
Try this:
<apex:commandButton onclick="window.location.href='{!URLFOR($Action.Case.NewCase)}'" value="New" immediate="true" rerender="blank"/>
Important to note that the rerender tag is needed otherwise the page will do a postback. By using a dummy rerender tag the redirect will work. Immediate is there to avoid running any validation rules on click.
I have a custom Cases apex page where I use a apex:pageBlockTable to display my list of Cases. Here is the code for it(custom object):
<apex:commandButton action="{!URLFOR($Action.Your_Custom_Object__c.New)}" ...>

How do I customize the columns in a apex:listView?

I have a apex:listView for my Cases:
<apex:ListViews type="Case">
</apex:ListViews>
When I display this page, I get the columns Action, Case Number, Contact Name, Subject, Status, Priority, Date/Time Opened, and Case Owner Alias.
How would I customize which columns show up and what order the columns are in?
Thanks.
This is what I ended up doing:
<apex:page standardController="Case" recordSetVar="Case" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="Cases">
<apex:outputLabel value="View:"/>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" rerender="cases_table"/>
<apex:selectOptions value="{!listviewoptions}"/>
</apex:selectList>
<apex:pageBlock >
<apex:pageBlockButtons >
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
<apex:column >
<a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
<apex:facet name="header">Case Number</apex:facet>
</apex:column>
<apex:column value="{!c.ContactId}" />
<apex:column value="{!c.Subject}" />
<apex:column value="{!c.Status}" />
<apex:column value="{!c.Priority}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
<base target="_parent"/>
</apex:page>
Apex List Views, similar to standard list views, contain a picklist at the top followed by an Edit button and a Create New View button. You can modify the view by clicking on the Edit button. There is no way, that I am aware of, to modify the columns programmatically. If you need more control, you could use another tag, such as <apex:pageblocktable>, as JCD mentioned in the question comments.

how to use colspan in VF pages

i have a VF page code
<apex:pageBlockSection collapsible="false" columns="2" >
<apex:inputField value="{!Opp.field1__c}"/>
<apex:inputField value="{!Opp.field2__c}"/>
<apex:outputField value="{!Opp.field3__c}"/>
<apex:outputField value="{!Opp.field4__c}"/>
</apex:pageBlockSection>
I want to have a command button inside the blockSection. Can we have something like a colspan on table to merge the first line to a single column to hold the command button?
Thanks
Prady
pageBlockSection renders as a table inside a div, so once you are "inside" you can just piggyback on that schema (at least until they change how they render sections). You need two columns per section column (in your case 2x2=>4). Use the following
<apex:pageBlockSection collapsible="false" columns="2" >
<tr>
<td colspan="4">
<apex:commandButton ...>
</td>
</tr>
<apex:inputField value="{!Opp.field1__c}"/>
<apex:inputField value="{!Opp.field2__c}"/>
<apex:outputField value="{!Opp.field3__c}"/>
<apex:outputField value="{!Opp.field4__c}"/>
</apex:pageBlockSection>

Salesforce-Not able to update records after being changed in a pageblock table having inline editing

I have created a search query which returns the records in a table. I have used command in the records returned so that i can edit them and save them in the table only. But after changing the records in the table and clicking on the SAVE button, I am not able to update the the records in the table. How can I use 'rerender' to show the updated data? The page code and controller action I'm using are below:
<!-- Page -->
<apex:pageBlock id="pb1">
<apex:outputPanel id="pan">
<apex:pageBlockTable value="{!l1}" var="k" rendered="{!flag}" id="pb">
<apex:column value="{!k.First_Name__c}"/>
<apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
<apex:column value="{!k.Last_Name__c}"/>
<apex:inlineEditSupport event="ondblclick" showOnEdit="save"/>
<apex:column value="{!k.E_mail__c}"/>
<apex:inlineEditSupport event="ondblclick" showOnEdit="save"/>
<apex:column value="{!k.Employee_ID__c}"/>
<apex:commandButton action="{!save}" id="saveButton" value="Save" />
</apex:pageBlockTable>
</apex:outputPanel>
<apex:commandButton action="{!edit}" id="editButton" value="Edit"/>
<apex:commandButton action="{!save}" id="saveButton" value="Save"/>
<apex:actionSupport event="onclick" rerender="pan" status="refreshstatus"/>
<apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
<apex:actionStatus id="refreshstatus" startstyle="color:green;" startText="Saving....">
</apex:actionStatus>
</apex:pageBlock>
// controller action
public pagereference save(){update l1;return null;}}
Posting some of your code would go a long way here, but the long and the short of it is this:
<!-- put your table in a panel with an ID -->
<apex:outputPanel id="thePanel:>
<!-- put your table here -->
</apex:outputPanel>
<!-- specify the panel's ID as the rerender target for the action -->
<apex:commandButton value="Save" action="{!TheSaveAction}" rerender="thePanel"/>
And then make sure your controller returns a Pagereference with a value of null:
public Pagereference TheSaveAction()
{
// save
return null;
}
If you're still struggling after doing this, put in the page code (or the relevant parts) so I can see what's going on.

Resources