067_VFPage中Js与controller交互方式(二) RemoteAction
生活随笔
收集整理的這篇文章主要介紹了
067_VFPage中Js与controller交互方式(二) RemoteAction
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
上篇文章介紹了Toolkit API,是一種js的前臺寫法
同步調(diào)用格式:
sforce.connection.method("argument1","argument2",...);
異步調(diào)用格式:
sforce.connection.method("argument1","argument2",...,"callback_function");
此次介紹的內(nèi)容仍為JS前臺的寫法,不過是和controller交互的,他不同與action對應的method,而是一種在js代碼中調(diào)用的controller 方法;
結構如下:
?
- Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
?callbackFunction接收方法調(diào)用的狀態(tài)和結果作為參數(shù)。
global with sharing class AccountRemoter {public String accountName { get; set; }public static Account cc{ get; set; }public AccountRemoter() { } // empty constructor@RemoteActionpublic static Account getAccount(String accountName) {cc = [SELECT Id, name,NumberOfEmployees FROM Account WHERE Name = :accountName];return cc;}}<apex:page controller="AccountRemoter"> <script type="text/javascript">function getRemoteAccount() {var accountName = document.getElementById('acctSearch').value;Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.AccountRemoter.getAccount}',accountName,function(result, event){if (event.status) {//alert(result); //console.log(result); console.log(event);// Get DOM IDs for HTML and Visualforce elements like thisdocument.getElementById('remoteAcctId').innerHTML = result.Iddocument.getElementById("{!$Component.block.blockSection.secondItem.acctNumEmployees}").innerHTML = result.NumberOfEmployees;} else if (event.type === 'exception') {document.getElementById("responseErrors").innerHTML =event.message + "<br/>\n<pre>" + event.where + "</pre>";} else {document.getElementById("responseErrors").innerHTML = event.message;}},{escape: true});} </script> <input id="acctSearch" type="text"/> <button οnclick="getRemoteAccount()">Get Account</button> <div id="responseErrors"></div><apex:pageBlock id="block"><apex:pageBlockSection id="blockSection" columns="2"><apex:pageBlockSectionItem id="firstItem"><span id="remoteAcctId"/></apex:pageBlockSectionItem><apex:pageBlockSectionItem id="secondItem"><apex:outputText id="acctNumEmployees"/></apex:pageBlockSectionItem></apex:pageBlockSection> </apex:pageBlock> </apex:page>
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/bandariFang/p/9682154.html
總結
以上是生活随笔為你收集整理的067_VFPage中Js与controller交互方式(二) RemoteAction的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【随笔】express中间件系统的基本实
- 下一篇: day10 局部变量 全局变量 作用域前