这里用的组合是:apex:commandLink  + apex:actionFunction + apex:outputPanel

这里的 apex:commandLink 和 apex:actionFunction 都可以通过 action 来指定Ajax所回调的后台方法,

方案一:

其中 apex:commandLink 充当直接回调后台方法的控件,  用 oncomplete 指定执行完后台方法之后所调用的 actionFunction

其中 apex:actionFunction 的 name 标识actionFunction的名字,实质上是一个JavaScript方法,其他地方可以直接进行调用,这里用apex:commandLink的oncomplete方法调用, rerender 指定所要刷新的 apex:outputPanel, oncomplete 指定刷新完outputPanel之后所调用的JavaScript方法

1):后台class的变量和类定义

public List<SalesPersonSales> personSalesTop10_MTD { get; set; }

public class SalesPersonSales implements Comparable{
public string OwnerName {get;set;}
public string OwnerId {get;set;}
public decimal TotalSales {get;set;}
public decimal TotalGP {get;set;} public SalesPersonSales(string name, string id, decimal sales, decimal gp){
OwnerName = name;
OwnerId = id;
TotalSales = sales;
TotalGP = gp;
} public Integer compareTo(Object objToCompare){
return (((SalesPersonSales)objToCompare).TotalSales - TotalSales).intValue();
}
}

2):前台page的控件布局与JavaScript方法

<apex:form id="theform">
<apex:outputPanel id="SalespeopleReportPanel">
<apex:outputPanel id="SalespeopleReportMTDPanel">
<table>
<thead>
<tr>
<th>Top 10 Salespeople MTD</th>
<th>Sales</th>
<th>GP$</th>
</tr>
</thead>
<tbody>
<apex:repeat value="{!personSalesTop10_MTD}" var="Item_Obj">
<tr>
<td>
<apex:outputLink value="../apex/DashboardReportDetail">
{!Item_Obj.OwnerName}
<apex:param name="oid" value="{!Item_Obj.OwnerId}"/>
<apex:param name="timetype" value="MTD"/>
</apex:outputLink>
</td>
<td>
<apex:outputText value="{0,number,$#,###.##}" >
<apex:param value="{!Item_Obj.TotalSales}" />
</apex:outputText>
</td>
<td>
<apex:outputText value="{0,number,$#,###.##}" >
<apex:param value="{!Item_Obj.TotalGP}" />
</apex:outputText>
</td>
</tr>
</apex:repeat>
<tr>
<td>
<apex:commandLink action="{!LoadAllSalespeople}" oncomplete="rerenderSalespeopleReportMTDPanel();" status="sstatus">
Show All
<apex:param value="All" name="topType"/>
<apex:param value="MTD" name="timeType"/>
</apex:commandLink>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</apex:outputPanel>
<apex:actionFunction name="rerenderSalespeopleReportMTDPanel" rerender="SalespeopleReportMTDPanel" oncomplete="refreshSalespeopleDashboardChart();"/>
</apex:outputPanel>
</apex:form> <script type="text/javascript"> function refreshSalespeopleDashboardChart(){
// Your JS function....
} </script>

【 注:如果Ajax回调的方法更新了后台的某个变量,前台JS方法中想要直接用{!myVariable}这种方法是获取不到更新之后的值的,这样的处理方式细想一下还是很合理的。】

【 要想获取更新之后的值的话,要将变量的值存储到更新的outputPanel中的某个控件中,然后通过JS的方法获取更新之后控件的内容就可以了。】

3):Ajax回调的后台方法

public pagereference LoadAllSalespeople(){
string topType = ApexPages.currentPage().getParameters().get('topType');
string timeType = ApexPages.currentPage().getParameters().get('timeType');
if(timeType == 'MTD'){
// to do
}
else{
// to do
}
return null;
}

上面的这种方式有一个很大的弊端,就是不能够动态的给parameter赋值,如果想满足这点请看如下方案

方案二:

其中 apex:commandLink 仅当做一个触发器,通过前台JS方法去调用apex:actionFunction所对应的后台方法,这个JS方法做了一个非常重要的事情就是动态的传递Parameter的值。形式为:actionFunction的名字(param1, param2, ...)

其中 apex:actionFunction 的 name 标识actionFunction的名字,实质上是一个JavaScript方法,其他地方可以直接进行调用,rerender 指定所要刷新的 apex:outputPanel, oncomplete 指定刷新完outputPanel之后所调用的JavaScript方法

那么所对应的Apex Page代码如下所示:

<apex:form id="theform">
<apex:outputPanel id="SalespeopleReportPanel">
<apex:outputPanel id="SalespeopleReportMTDPanel">
<table>
<thead>
<tr>
<th>Top 10 Salespeople MTD</th>
<th>Sales</th>
<th>GP$</th>
</tr>
</thead>
<tbody>
<apex:repeat value="{!personSalesTop10_MTD}" var="Item_Obj">
<tr>
<td>
<apex:outputLink value="../apex/DashboardReportDetail">
{!Item_Obj.OwnerName}
<apex:param name="oid" value="{!Item_Obj.OwnerId}"/>
<apex:param name="timetype" value="MTD"/>
</apex:outputLink>
</td>
<td>
<apex:outputText value="{0,number,$#,###.##}" >
<apex:param value="{!Item_Obj.TotalSales}" />
</apex:outputText>
</td>
<td>
<apex:outputText value="{0,number,$#,###.##}" >
<apex:param value="{!Item_Obj.TotalGP}" />
</apex:outputText>
</td>
</tr>
</apex:repeat>
<tr>
<td>
<apex:commandLink onclick="CallActionFunction();">Show All</apex:commandLink>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</apex:outputPanel>
<apex:actionFunction name="rerenderSalespeopleReportMTDPanel" action="{!LoadAllSalespeople}" rerender="SalespeopleReportMTDPanel" oncomplete="refreshSalespeopleDashboardChart();"/>
<apex:param value="All" name=""/>
<apex:param value="MTD" name=""/>
</apex:actionFunction>
</apex:outputPanel>
</apex:form> <script type="text/javascript"> function CallActionFunction(){
rerenderSalespeopleReportMTDPanel('topType', 'timeType'); // these two parameters can get automatically by js
} function refreshSalespeopleDashboardChart(){
// Your JS function....
} </script>

对比方案一和方案二:

方案一的基本代码结构(轻actionFunction,重commandLink)

<apex:outputPanel id="SalespeopleReportMTDPanel">
<apex:commandLink action="{!LoadAllSalespeople}" oncomplete="rerenderSalespeopleReportMTDPanel();">
Show All
<apex:param value="All" name="topType"/>
<apex:param value="MTD" name="timeType"/>
</apex:commandLink>
<apex:actionFunction name="rerenderSalespeopleReportMTDPanel" rerender="SalespeopleReportMTDPanel" oncomplete="refreshSalespeopleDashboardChart();"/>
</apex:outputPanel> <script type="text/javascript"> function refreshSalespeopleDashboardChart(){
// Your JS function....
} </script>

方案二的基本代码结构(轻commandLink,重actionFunction)

<apex:outputPanel id="SalespeopleReportMTDPanel">
<apex:commandLink onclick="CallActionFunction();">Show All</apex:commandLink>
<apex:actionFunction name="rerenderSalespeopleReportMTDPanel" action="{!LoadAllSalespeople}" rerender="SalespeopleReportMTDPanel" oncomplete="refreshSalespeopleDashboardChart();"/>
<apex:param value="All" name=""/>
<apex:param value="MTD" name=""/>
</apex:actionFunction>
</apex:outputPanel> <script type="text/javascript"> function CallActionFunction(){
rerenderSalespeopleReportMTDPanel('topType', 'timeType'); // these two parameters can get automatically by js
} function refreshSalespeopleDashboardChart(){
// Your JS function....
} </script>

进一步引申:

我们发现可以完全用其他控件来代替commandLink的触发器作用,来调用actionFunction所指定的后台方法,具体的代码结构如下所示:

<apex:outputPanel id="SalespeopleReportMTDPanel">
<input type="button" value="Show All" title="ShowAll" name="ShowAll" onclick="CallActionFunction();"/>
<apex:actionFunction name="rerenderSalespeopleReportMTDPanel" action="{!LoadAllSalespeople}" rerender="SalespeopleReportMTDPanel" oncomplete="refreshSalespeopleDashboardChart();"/>
<apex:param value="All" name=""/>
<apex:param value="MTD" name=""/>
</apex:actionFunction>
</apex:outputPanel> <script type="text/javascript"> function CallActionFunction(){
// other custom logic
rerenderSalespeopleReportMTDPanel('topType', 'timeType'); // these two parameters can get automatically by js
} function refreshSalespeopleDashboardChart(){
// Your JS function....
} </script>

这里附上另一种Ajax回调的方式,请看这个链接:http://www.cnblogs.com/mingmingruyuedlut/p/3385532.html

在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)的更多相关文章

  1. 在Asp.Net MVC中用Ajax回调后台方法

    在Asp.Net MVC中用Ajax回调后台方法基本格式: var operData = ...; //传递的参数(action中定义的) var type = ...; //传递的参数(action ...

  2. silverlight visifire控件图表制作——silverlight 后台方法页面事件

    1.返回事件 (1.返回silverlight页面,2.返回web页面) private void button_ClickBack(object sender, RoutedEventArgs e) ...

  3. silverlight visifire控件图表制作——silverlight 后台方法ControlChart.xaml.cs

    一.构造方法ControlChart 1.前台页面控件赋值 //时间下拉框赋值,下拉框赋选定值                for (int ii = DateTime.Today.Year; ii ...

  4. silverlight visifire控件图表制作——silverlight 后台方法画图

    1.调用wcf 获取信息 private void svc_GetSingleChartDataCompleted(object sender, GetSingleChartDataCompleted ...

  5. silverlight visifire控件图表制作——silverlight 后台方法打印

    一.后台方法 1.添加引用:using System.Windows.Printing; 2.全局变量://定义图片和文本打印变量  PrintDocument printImage; 3.构造方法体 ...

  6. 【转】【完全开源】百度地图Web service API C#.NET版,带地图显示控件、导航控件、POI查找控件

    [转][完全开源]百度地图Web service API C#.NET版,带地图显示控件.导航控件.POI查找控件 目录 概述 功能 如何使用 参考帮助 概述 源代码主要包含三个项目,BMap.NET ...

  7. HighChats图表控件显示精度小数点的方法

    相信大家对highchats这个图表控件并不陌生,最近在项目中用到它,但是某些字段需要显示为小数点,顾查找资料文档发现下面两个方式可以实现: 初始化时候添加如下两个参数 tooltip:{    fo ...

  8. C#自动实现Dll(OCX)控件注册的两种方法

    尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...

  9. C#中DEV控件,XtraTabPage得小方法

    DEV控件设计窗体程序,XtraTabPage用到的小方法,欢迎大家评论,分享技术! //DEV中的选项卡 private bool TabCtlPageExist(string pageName) ...

随机推荐

  1. centos7最小版本安装nginx+tomcat+java+mysql运行环境

    最近项目从windows搬到linux,由于项目组成员有限并且有其它紧急的任务需要处理,因而这个任务就落到我的头上了.下面记录下centos最小版本安装nginx+tomcat+mysql+java的 ...

  2. poj1155

    题意:给定一个树形图,节点数量3000.叶子节点是用户,每个用户如果能看上电视会交一定的电视费.看上电视的条件是从根到该用户的路径全部被修好,修每条边有一个费用.在不亏损(用户交钱总额>=修路总 ...

  3. linux学习之-vsftp

    一.简介 vsftp是一个基于GPL发布的类unix系统上使用的ftp服务器软件,它的全称是very secure FTP ,软件的编写初衷是为了代码的安全,另外高速与高稳定性也是vsftp的两个重要 ...

  4. Divide and conquer:Sumsets(POJ 2549)

    数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...

  5. (转)JAVA AJAX教程第二章-JAVASCRIPT基础知识

    开篇:JAVASCRIPT是AJAX技术中不可或缺的一部分,所以想学好AJAX以及现在流行的AJAX框架,学好JAVASCRIPT是最重要的.这章我给大家整理了一些JAVASCRIPT的基础知识.常用 ...

  6. 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)

    1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 < ...

  7. 【leetcode】Binary Tree Postorder Traversal (hard) ☆

    二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> an ...

  8. 【python】getopt使用

    来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...

  9. 网站配置好了,在本地能登录系统,但是挂在IIS上就无法登录了,提示数据库连接错误

    我用的VS2010开发的网站,但是挂在本机的IIS上的时候就无法登录了,提示错误如下:

  10. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...