Weird behavior (bug) of inputCheckbox in the visualforce - visualforce

inputCheckbox and commandLink/commandButton can't work together !!
Case 1:
Try the codes.
The visualforce action in this one can't work.
<apex:commandLink action="{!SelectTicket}" reRender="outputPanel">
<apex:inputCheckbox value="{!ordsOptions[o]}"/>
<apex:param name="selected" value="{!o.id}" assignTo="{!selected}"/>
</apex:commandLink>
Without value="{!ordsOptions[o]}" in the inputCheckbox. The almost same code just works fine.
<apex:commandLink action="{!SelectTicket}" reRender="outputPanel">
<apex:inputCheckbox/>
<apex:param name="selected" value="{!o.id}" assignTo="{!selected}"/>
</apex:commandLink>
Case 2:
It doesn't fire the action at all when add the inputCheckbox in another column.
<apex:dataTable border="4" value="{!ords}" var="o">
<apex:column >
<apex:commandLink action="{!SelectTicket}" reRender="outputPanel">
<apex:inputCheckbox />
<apex:param name="selected" value="{!o.id}" assignTo="{!selected}"/>
</apex:commandLink>
</apex:column>
<apex:column ><apex:inputCheckbox value="{!ordsOptions[o]}"/></apex:column>
<apex:column ><apex:outputText value="{!ordsOptions[o]}"/></apex:column>
<apex:column value="{!o.Id}"/>
<apex:column value="{!o.Name}"/>
</apex:dataTable>
It just works fine without inputCheckbox along with commandLink:
<apex:dataTable border="4" value="{!ords}" var="o">
<apex:column >
<apex:commandLink action="{!SelectTicket}" reRender="outputPanel">
<apex:inputCheckbox />
<apex:param name="selected" value="{!o.id}" assignTo="{!selected}"/>
</apex:commandLink>
</apex:column>
<-- <apex:column ><apex:inputCheckbox value="{!ordsOptions[o]}"/></apex:column> -->
<apex:column ><apex:outputText value="{!ordsOptions[o]}"/></apex:column>
<apex:column value="{!o.Id}"/>
<apex:column value="{!o.Name}"/>
</apex:dataTable>
The whole codes
vf:
<apex:page controller="SelectRadio" sidebar="false">
<apex:outputPanel id="outputPanel">
<apex:form >
<apex:dataTable border="4" value="{!ords}" var="o">
<apex:column >
<apex:commandLink action="{!SelectTicket}" reRender="outputPanel">
<apex:inputCheckbox />
<apex:param name="selected" value="{!o.id}" assignTo="{!selected}"/>
</apex:commandLink>
</apex:column>
<apex:column ><apex:inputCheckbox value="{!ordsOptions[o]}"/></apex:column>
<apex:column ><apex:outputText value="{!ordsOptions[o]}"/></apex:column>
<apex:column value="{!o.Id}"/>
<apex:column value="{!o.Name}"/>
</apex:dataTable>
<apex:commandButton action="{!rSfdc}" value="按鍵"/>
</apex:form>
<hr/>
<h1>Debug</h1>
<p>ordsOptions = {!ordsOptions}</p>
<p>ordsOptionName = {!ordsOptionName}</p>
<p>selected = {!selected}</p>
</apex:outputPanel>
</apex:page>
class:
public with sharing class SelectRadio {
public List<String> vars {get;set;}
public List<Custom_Object__c> ords {get; set;}
public Map<Id,Boolean> ordsOptions {get; set;}
public Map<Id,String> ordsOptionName {get; set;}
public Id selected {get;set;}
public SelectRadio(){
ordsOptions = new Map<Id,Boolean>();
ordsOptionName = new Map<Id,String>();
ords = [ SELECT Id, Name FROM Custom_Object__c ORDER BY Name ];
for (Custom_Object__c tmp : ords) {
ordsOptions.put(tmp.Id, false);
ordsOptionName.put(tmp.Id, tmp.Name);
}
}
public pageReference SelectTicket() {
for(Custom_Object__c tmp : ords) if(tmp.Id == selected) ordsOptions.put(tmp.Id, true); else ordsOptions.put(tmp.Id, false);
return null;
}
public PageReference rSfdc() {
PageReference rp = new PageReference('http://www.salesforce.com');
rp.setRedirect(true);
return rp;
}
}

It's not a bug at all. And, it has to be so.
It is all about the logic of OO programming.
Finally, I found myself the answer.
Check the answer here: http://boards.developerforce.com/t5/Visualforce-Development/Strange-Bug-of-the-apex-inputCheckbox/m-p/631629/highlight/false#M65163
It can be used to replace the selectRadio, and pattern code is more flexible than apex:selectRadio. The sample pattern is easily to use with apex:dataTable and apex:repeat. That's the reason to write the code. Enjoy it with me if you like.

Related

Invalid case exception

In report page i have dropdown and datepicker fromdate and todate and button
so when i select values from dropdown and datepicker then this show error
on this line
dt=report(Convert.ToDateTime(fromdate), Convert.ToDateTime(todate), Convert.ToString(DropDownList1.SelectedValue));
error
An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code
Additional information: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.IConvertible'
.
code
protected void Button1_Click(object sender, EventArgs e)
{
dt=report(Convert.ToDateTime(fromdate), Convert.ToDateTime(todate), Convert.ToString(DropDownList1.SelectedValue));
GridView1.DataSource = dt;
GridView1.DataBind();
}
DataTable dt = new DataTable();
public DataTable report(DateTime fromdate,DateTime todate,string IMEI)
{
DateTime fromdatee = Convert.ToDateTime(Request.Form["fromdate"]);
DateTime todatee = Convert.ToDateTime(Request.Form["todate"]);
Entities track = new Entities();
DateTime fr_date = new DateTime(fromdatee.Year, fromdatee.Month, fromdatee.Day, 0, 0, 0);
DateTime t_date = new DateTime(todatee.Year, todatee.Month, todatee.Day, 23, 59, 59);
List<spGetReport_Result> report = track.spGetReport(IMEI,fr_date,t_date).ToList();
dt.Columns.Add("Time",typeof(DateTime));
dt.Columns.Add("X",typeof(float));
dt.Columns.Add("valuenumber",typeof(int));
foreach(var c in report)
{
dt.Rows.Add(c.Time, c.X, c.valuenumber);
}
return dt;
}
HTML
<form id="form1" runat="server">
<div>
<span>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</span>
<span>
<input id="fromdate" runat="server" clientidmode="static" />
</span>
<span>
<input id="todate" runat="server" clientidmode="static" />
</span>
<span>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</span><br />
<asp:Label ID="Label1" style="margin-left: 220px;" runat="server" Text="Export to"></asp:Label>
<asp:GridView ID="GridView1" runat="server" class="display nowrap"
Width="100%" CellPadding="0"
Font-Names="Verdana" BackColor ="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" Font-Size="9pt">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
</form>
Because you are sending Convert.ToDateTime the HTML control instead of the string to be converted.
You should be doing this:
dt = report(Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), DropDownList1.SelectedValue);
A very simple debugging process would have quickly showed you where the problem was.
DropDownList1.SelectedValue is already a string, so no point in converting it.
Anyway, you should check first that what's in those inputs are really valid DateTime representations by using a validator.

Merging rows in dynamically binded apex:pageblockTable in visualforce page

I am new to Salesforce VF pages. I have a dynamically binded pageBlockTable in my Visualforce page. I wanted to merge certain dynamic rows based on a condition. Could someone please suggest, if there is any possible way to merge rows(or subrows) in apex:pageblocktable based on some conditions?
I have tried googling this, but could find no clues.
Any help is really appreciated.
Visualforce page code:
<apex:pageblock id="listBlock">
<apex:pageblockTable value="{!lstcomp}" var="oComp" styleclass="table table-striped table-hover" id="sysTable" rowClasses="even,odd">
<apex:column headerClass="TableTitle" style="width: 20%;height:4%" ><apex:facet name="header"><font class="headerfontstyle" >Name</font></apex:facet>
<font class="rowdatastyle"> {!oComp.Name} </font>
</apex:column>
<apex:column headerClass="TableTitle" style="width: 10%;height:4%" ><apex:facet name="header"><font class="headerfontstyle" >Mfg</font></apex:facet>
<font class="rowdatastyle" > {!oComp.Manufacturer}</font>
</apex:column>
<apex:column headerClass="TableTitle" style="width: 6%;height:4%"><apex:facet name="header"><font class="headerfontstyle" >Type</font></apex:facet>
<font class="rowdatastyle" > {!oComp.ComponentType} </font>
</apex:column>
<apex:column headerClass="TableTitle" style="width: 12%;height:4%"><apex:facet name="header"><font class="headerfontstyle" >Allocated</font></apex:facet>
<font class="rowdatastyle"> {!oComp.Allocated} </font>
</apex:column>
<apex:column headerClass="TableTitle" style="width: 10%;height:4%"><apex:facet name="header"><font class="headerfontstyle" >Used</font></apex:facet>
<font class="rowdatastyle"> {!oComp.Used} </font>
</apex:column>
<apex:column headerClass="TableTitle" style="width: 30%;height:4%"><apex:facet name="header"><font class="headerfontstyle" >Version</font></apex:facet>
<font class="rowdatastyle">{!oComp.OS} </font>
</apex:column>
</apex:pageblockTable>
You could group your records base on certain conditions and show the rest of fields as from child records.
<apex:pageblockTable>
<apex:column value="{!acc.name}">
<apex:column>
<apex:facet name="header"> Team Members </apex:facet>
<apex:pageblocktable value="{!acc.AccountTeamMembers}" var="tm">
<apex:column headerValue="Team Member">
<apex:outputfield value="{!tm.User.Name}"/>
</apex:column>
<apex:column headerValue="Role">
<apex:outputfield value="{!tm.TeamMemberRole}"/>
</apex:column>
</apex:pageblocktable>
</apex:column>
</apex:pageblockTable>

Display custom object and fields with Visualforce

I have a custom object called Conference. I need to create a simple visualforce page that displays all records of Conference including the following details per Conference: Name, City, State, Start Date, End Date, Expected Attendess, and Actual Enrolled. I built this with standard Salesforce.com admin point-and-click, and they all display perfectly in a report I created with point-and-click.
With the following code I'm getting this error:
Error: Unknown property 'Conference__cStandardController.conference'
<apex:page standardStylesheets="false" showHeader="false" sidebar="false"
standardController="Conference__c" recordsetVar="conf">
<apex:stylesheet value="{!URLFOR($Resource.styles, 'styles.css')}"/>
<h1>Conference Details</h1>
<apex:form>
<apex:dataTable value="{!Conference__c}" var="confItem" rowClasses="odd,even">
<apex:column headerValue="Conference Name">
<apex:outputField value="{!confItem.Name}"/>
</apex:column>
<apex:column headerValue="City">
<apex:outputText value="{!confItem.City__c}"/>
</apex:column>
<apex:column headerValue="State">
<apex:outputText value="{!confItem.Location_State__c}"/>
</apex:column>
<apex:column headerValue="Start Date">
<apex:outputText value="{!conference.Start_Date__c}"/>
</apex:column>
<apex:column headerValue="End Date">
<apex:outputText value="{!conference.End_Date__c}"/>
</apex:column>
<apex:column headerValue="Technologies">
<apex:outputText value="{!conference.Technologies__c}"/>
</apex:column>
<apex:column headerValue="Expected">
<apex:outputText value="{!conference.Number_of_Attendees_Expected__c}"/>
</apex:column>
<apex:column headerValue="Currently Enrolled">
<apex:outputText value="{!conference.Enrolled_Attendees__c}"/>
</apex:column>
</apex:dataTable>
</apex:form>
You already refered sObject Conference__c to conf.
Therefore in your dataTable you should write your code like this:
<apex:dataTable value="{!conf}" var="confItem" rowClasses="odd,even">
<apex:column headerValue="Conference Name">
<apex:outputField value="{!confItem.Name}"/>
</apex:column>
and so on
<apex:enhancedList> might be your new best friend.
But if you want to keep the code you have so far you'd need to loop (meaning reference it in dataTable/pageBlockTable/repeat) over the variable name you've chosen as the "recordsetvar" attribute. Check out this link for more info.
Something like that should work:
<apex:page standardStylesheets="false" showHeader="false" sidebar="false"
standardController="Conference__c" recordSetVar="conferences">
<apex:pageBlock>
<apex:pageBlockTable value="{!conferences}" var="c">
<apex:column value="{!c.Name}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

Visualforce Toolbar Rerender Firefox bug

I have a Visualforce toolbar and when I rerender the pageblock only in Firefox the whole bar gets extended vertically to a large toolbar block.
See code below
<apex:toolbar id="theToolbar" style="background-color:#8d8d8d;background-image:none">
<apex:toolbarGroup itemSeparator="line" location="left" id="toobarGroupForm">
<apex:outputText style="color:#f8f8ff;font-weight:bold" value="Amount of Records"/>
<apex:selectList label="Record Amount" value="{!ShowAmountOfRecords}" size="1" required="false" >
<apex:actionSupport event="onchange" action="{!AmountOfRecordsAction}" reRender="innerblock" status="recordamountchange" />
<apex:outputPanel style="color:#f8f8ff;font-weight:bold">
<apex:actionStatus id="recordamountchange" startText="Showing more records..." stopText=""/>
</apex:outputPanel>
<apex:selectOptions value="{!AmountOfRecordsList}"/>
</apex:selectList>
<apex:outputText style="color:#f8f8ff;font-weight:bold" value="Filter By Document Type"/>
<apex:selectList label="Filter by Record Type" value="{!FilterByRecordType}" size="1" required="false" >
<apex:actionSupport event="onchange" action="{!FilterByRecordTypeAction}" reRender="innerblock" status="filterByRecordType" />
<apex:outputPanel style="color:#f8f8ff;font-weight:bold">
<apex:actionStatus id="filterByRecordType" startText="Filtering your records..." stopText=""/>
</apex:outputPanel>
<apex:selectOptions value="{!FilterByRecordTypeList}"/>
</apex:selectList>
</apex:toolbarGroup>
</apex:toolbar>
Is this a know bug in Firefox?
The problem was that I had two picklist part of the same Toolbar Group. Make sure to add your components to different toolbar groups.
<apex:toolbar id="theToolbar" style="background-color:#8d8d8d;background-image:none" rendered="true">
<apex:outputText style="color:#f8f8ff;font-weight:bold" value="Amount of Records"/>
<apex:toolbarGroup itemSeparator="line" location="left" id="toobarGroupForm" rendered="true">
<apex:selectList label="Record Amount" value="{!ShowAmountOfRecords}" size="1" required="false" >
<apex:actionSupport event="onchange" action="{!AmountOfRecordsAction}" status="recordamountchange" reRender="innerblock" />
<apex:outputPanel style="color:#f8f8ff;font-weight:bold">
<apex:actionStatus id="recordamountchange" startText="Showing more records..." stopText=""/>
</apex:outputPanel>
<apex:selectOptions value="{!AmountOfRecordsList}"/>
</apex:selectList>
</apex:toolbarGroup>
<apex:outputText style="color:#f8f8ff;font-weight:bold" value="Filter By Document Type"/>
<apex:toolbarGroup itemSeparator="line" location="left" id="toobarGroupForm2" rendered="true">
<apex:selectList label="Filter by Record Type" value="{!FilterByRecordType}" size="1" required="false" >
<apex:actionSupport event="onchange" action="{!FilterByRecordTypeAction}" status="filterByRecordType" reRender="innerblock"/>
<apex:outputPanel style="color:#f8f8ff;font-weight:bold">
<apex:actionStatus id="filterByRecordType" startText="Filtering your records..." stopText=""/>
</apex:outputPanel>
<apex:selectOptions value="{!FilterByRecordTypeList}"/>
</apex:selectList>
</apex:toolbarGroup>
</apex:toolbar>

Zk how to reach included .zul page component by id?

I can't reach component by id in the included .zul page. I have one main.zul with a controller and I need to get a component in included zul page through the java controller class, but it returns null.
I know the included method creates new id space but is there any way to get this component?
UPDATE
Here is my code:
the main zul page
<?page title="DealerVizard.zul"?>
<?page id="main" ?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./Dealer" ?>
<zk>
<style src="/resources/css/default.css" />
<window id="Dealer" class="index"
apply="com.i2i.prm.controller.IndexController">
<div class="content" width="100%">
<tabbox id="tb" forward="onSelect=onSelect">
<tabs id="tabs">
<tab id="info" label="INFO" />
<tab id="create" label="CREATE" />
<tab id="edit" label="EDIT" />
<tab id="test" label="TEST PANEL(LIST BOX)" />
</tabs>
<tabpanels>
<tabpanel id="DealerInfo">
<include id="DealerInfoContent"
src="View/Dealer/DealerInfo.zul" />
</tabpanel>
<tabpanel id="DealerCreate">
<include id="DealerCreateContent"
src="View/Dealer/DealerCreate.zul" />
</tabpanel>
<tabpanel id="DealerEdit">
<include id="DealerEditContent"
src="View/Dealer/DealerEdit.zul" />
</tabpanel>
<tabpanel id="PagingListBox">
<include id="PagingListBoxContent" // Included here
src="View/TEST/PagingListBox.zul" />
</tabpanel>
</tabpanels>
</tabbox>
</div>
</window>
</zk>
PagingListBox.zul (Included page)
<?page id="list" ?>
<zk>
<grid width="100%">
<columns>
<column label="" />
</columns>
<rows>
<row>
<listbox id="listModel" width="100%" height="100%"
visible="true" span="true" pagingPosition="top" rows="20"
selectedItem="#{DealerController.selected}"
model="#{DealerController.userList}"
forward="onSelect=//main/Dealer.onSelect">
<auxhead>
<auxheader colspan="1">
<textbox
value="#{DealerController.searchUser.name}" maxlength="9"
id="searchCO_ID" forward="onChanging=//main/Dealer.onSearch"
width="100%">
</textbox>
</auxheader>
<auxheader colspan="1">
<textbox
value="#{DealerController.searchUser.surname}" maxlength="21"
id="searchMSISDN" forward="onChanging=//main/Dealer.onSearch"
width="100%">
</textbox>
</auxheader>
<auxheader colspan="1">
</auxheader>
</auxhead>
<listhead>
<listheader label="Name"
sort="auto(UPPER(name))" />
<listheader label="Surname"
sort="auto(UPPER(surname))" />
<listheader label="Delete ?" />
</listhead>
<listitem self="#{each=USERLIST}">
<listcell>
<label value="#{USERLIST.user.name}" />
<textbox
value="#{DealerController.tmpUser.name}" visible="false" />
</listcell>
<listcell>
<label value="#{USERLIST.user.surname}" />
<textbox
value="#{DealerController.tmpUser.surname}" visible="false" />
</listcell>
<listcell>
<button label="Update"
forward="onClick=//main/Dealer.onUpdate" visible="false" />
<button image="icons/edit-delete.png"
label="Delete" forward="onClick=//main/Dealer.onDelete"
width="100%" disabled="true" />
<button label="Save"
forward="onClick=//main/Dealer.onSave" visible="false" />
<button label="Cancel"
forward="onClick=//main/Dealer.onCancel" visible="false" />
</listcell>
</listitem>
</listbox>
<paging id="pagingData" pageSize="20" />
</row>
</rows>
</grid>
</zk>
IndexCOntroller.java
public class IndexController extends GenericForwardComposer {
private List<User> userList = new ArrayList<User>() ;
AnnotateDataBinder binder;
Tabbox tb;
Window Dealer;
private int pageCount=0;
#Override
public void doAfterCompose(Component comp) throws Exception {
// TODO Auto-generated method stub
super.doAfterCompose(comp);
comp.setVariable(comp.getId() + "Controller", this, true);
binder = (AnnotateDataBinder) Dealer.getVariable("binder", true);
System.out.println(Path.getComponent("//list/listModel"));
}
public IndexController() {
// TODO Auto-generated constructor stub
}
}
Normally I wouldn't recommend using Path.getComponent() way to access other components as your application code becomes tightly coupled with your component structure in your view page.
In your case you simplest way is to use AbstractComponent#getFellow(String compId) method so for eg.
Include inc = (Include) Dealer.getFellow("PagingListBoxContent");
Listbox listModel = (Listbox) inc.getFellow("listModel");
System.out.println(listModel);
So in future even if you insert any other component in your ZUML page before your listbox your code will still work.
UPDATE: BTW there was an interesting blogpost on this very topic on ZK blog recently
if your include have id, you can use dollar sign to get the inner components
<zk>
<include id="inc" src="test.zul />
</zk>
test.zul
<zk>
<label id="lab1" value="test1" />
</zk>
you can use "inc$lab1" get the label in test.zul
You can access any component in any other id space using zscript or java. if it is on the same page, but different window then (component B in window A):
Path.getComponent("/A/B");
if it is on a different page then (component B in window A on page P):
Path.getComponent("//P/A/B");
You can find documentation here: http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/UI%20Composing/ID%20Space
You can add in your IndexController.java:
...
private Include DealerInfoContent;
...
this way you can access the included component within the parent composer.
(I would suggest to use camelCase ids for it, though).

Resources