Visualforce页面调用后台Controller中的方法,常用的有以下几种:
(1)Action
public void search_product(){
...
}
<apex:commandButton style="position: relative;left:5px;bottom:2px;" id="searchId" value="Search" action="{!search_product}" reRender="entryListTableParent,labelSearch,ConsoleInfo" status="loading"/>
(2)ActionFunction
调用声明需要写在apex:form里面,方法的返回为PageReference ,
<apex:form>
.....
<apex:actionFunction action="{!search_product2}" name="search_product2"
reRender="entryListTableParen,ConsoleInfo" status="loading">
</apex:actionFunction>
......
</apex:form>
其中action对应的是Controller中的方法,name可以再js里面直接调用,
public PageReference search_product2(){
search_product();
return null;
}
//页面加载完之后自动查询结果
$(function(){
search_product2(); //这里是actionfunction的name
});
(3)RemoteAction
方法必须是全局静态,
//获取楼栋下拉
@RemoteAction
global static List<sObject> getBuildings(String houseInspectionId) {
try {
List<Building__c> resultObjects = [select Id, Name from Building__c where Id in:bldSet order by Name];
return resultObjects;
}catch(Exception e) {
System.debug('\n\n--------- '+e.getStackTraceString()+':'+e.getMessage()+' ----------\n');
return null;
}
}
在js里调用的代码,填充的bootstrappack的下拉组件,
window.onload = function(){
$("#vkBlds").select2({placeholder: "请选择楼栋"});
var buildingNameVal = '';
xxxController.getBuildings('{!houseInspectionId}',function(result, event) {
if (event.type == 'exception') {
console.log(event.message);
}else{
console.log(result);
console.log(result[0].Name);
if(result != null && result.length>0){
if(buildingNameVal == null || buildingNameVal.length ==0){
for(var i=0; i<result.length; i++)
{
var param = result[i];
var opt = document.createElement('option');
opt.innerText = param.Name;
opt.value = param.Id;
vkBlds.appendChild(opt);
}
}else{
for(var i=0; i<result.length; i++)
{
var param = result[i];
var index = $.inArray(param.Name,buildingNameVal);
if(index >= 0){
var opt = document.createElement('option');
opt.innerText = param.Name;
opt.value = param.Id;
vkBlds.appendChild(opt);
}
}
}
}
}
});
}