Salesforce零基礎(三)簡單的資料增刪改查頁面的構建(Ajax樣式)

來源:互聯網
上載者:User

標籤:static   作用   sde   lex   command   結束   stat   ble   表單   

VisualForce封裝了很多的標籤用來進行頁面設計

下面以一個單一的表進行資料增刪改查。表結構1所示。通過圖可以看出GOODS表自己定義的參數主要包括以下:

GoodsName__c,GoodsType__c,GoodsBrand__c,GoodsDescribe__c,GoodsPrice__c。

                      圖1

VF每個頁面都是以<apex:page>標籤起始</apex:page>結束,每個VF頁面都有一個Controller用來控制其商務邏輯。本篇例子中主要用到的控制項包括如下:

<apex:inputText>:輸入框,類似於HTML中的<input type="text"/>,綁定的value類型可以為任意類型;

<apex:inputFile>:輸入框,類似於HTML中的<input type="text"/>,區別上者為value類型必須是sObject類型;

<apex:commandButton>:按鈕,類似於<input type="button"/>;

<apex:selectList>:下拉框,類似於html中的<select>;

<apex:selectOptions>:下拉框下元素,類似於html中的<option>;

<apex:pageBlockTable>:表格元素,類似於html中的table。用法與jstl類似,可以指定items屬性繫結資料行表,var屬性指定變數;

<apex:column>:表格的列元素,用於顯示表格每一列的值;

<apex:commandLink>:連結,類似於html中的<a>標籤;

<apex:param>:參數傳遞使用,用於給Controller層傳遞參數,傳遞的參數通過索引值對傳遞;

<apex:form>:表單元素,類似於html中的form表單。

背景值在前台可以通過{!object}形式(是不是類似EL運算式)來擷取後台object的變數。

eg:後台聲明Integer i = 1;   前台通過{!i}便可以擷取到i的值。

如果想要擷取系統的變數,比如想要擷取某個當前元素的ID屬性,則可以通過{!$Compontent.currentElementId}

在<apex:commandLink>或者<apex:commandButton>標籤綁定事件時,action綁定的格式為{!function}

eg:在<apex:commandButton>標籤綁定的action為{!query},則當點擊按鈕後,會調用Controller層的query方法。

注釋: OK,以下為代碼部分以及顯示的樣式,通過add按鈕可以添加一行資料,輸入內容後點擊save即可儲存資料,上方為搜尋地區。如果需要一次性插入多條資料,可以多操作幾次add按鈕,每個都輸入內容後執行save操作。

VF頁面代碼如下

1 <apex:page controller="GoodsController" showHeader="false">  2     <!-- <script>  3         function query123(goodsName) {  4               5             var goodsName = document.getElementById(goodsName).value;   6             console.log(‘goodsName:‘+goodsName);  7             Visualforce.remoting.Manager.invokeAction(   8                 ‘{!$RemoteAction.GoodsController.queryForName}‘,   9                 goodsName,  10                 function(result, event){  11                     console.log(‘aaa‘); 12                     if (event.status) { // Get DOM IDs for HTML and Visualforce elements like this  13                     } else if (event.type === ‘exception‘) {  14                     } else {  15                     }  16                 },  17                 {escape: false}  18             );   19         }  20     </script>--> 21     <apex:messages /> 22     <apex:form > 23         <apex:pageBlock title="GOODS"> 24             <apex:pageBlockSection title="query goods"> 25                 <apex:inputText value="{!goodsName}" tabIndex="4" label="goodsName" 26                     id="goodsName" /> 27                 <apex:selectList multiselect="false" size="1" value="{!goodsType}" 28                     label="goodsType:"> 29                     <apex:selectOptions value="{!typeNames}"> 30                     </apex:selectOptions> 31                 </apex:selectList> 32                 <apex:inputText value="{!goodsPriceStart}" tabIndex="3" 33                     label="goodsPriceStart" /> 34                 <apex:inputText value="{!goodsPriceEnd}" tabIndex="5" 35                     label="goodsPriceEnd" /> 36                 <apex:inputText value="{!goodsDescribe}" tabIndex="1" 37                     label="goodsDescribe" /> 38                 <apex:commandButton value="query" action="{!query}" 39                     /> 40             </apex:pageBlockSection> 41              42             <!-- <apex:pageBlockSection title="query goods By Name Via JS"> 43                 <apex:inputText label="goodsName" id="goodsName1" /> 44                 <apex:commandButton value="query" onclick="query123(‘{!$Component.goodsName1}‘)" 45                     styleClass="centerStyle" /> 46             </apex:pageBlockSection> --> 47              48             <apex:pageBlockTable value="{!goodsList}" var="goods" id="resultGoods"> 49                 <apex:column headervalue="goodsName"> 50                     <apex:inputField value="{!goods.GoodsName__c}"/> 51                 </apex:column> 52                 <apex:column headervalue="goodsPrice"> 53                     <apex:inputField value="{!goods.GoodsPrice__c}" /> 54                 </apex:column> 55                 <apex:column headervalue="goodsType"> 56                     <apex:inputField value="{!goods.GoodsType__c}" /> 57                 </apex:column> 58                 <apex:column headervalue="goodsDescribe"> 59                     <apex:inputField value="{!goods.GoodsDescribe__c}" /> 60                 </apex:column> 61                 <apex:column headervalue="delete?"> 62                     <apex:commandLink value="delete" action="{!deleteGoods}"> 63                         <apex:param name="goodsId" value="{!goods.Id}"></apex:param> 64                     </apex:commandLink> 65                 </apex:column> 66             </apex:pageBlockTable> 67              68             <!-- <apex:pageBlockTable value="{!goodsList}" var="goods" rendered="{!goodsList == null || goods == null}"> 69                 <apex:column colspan="7"> 70                     <apex:outputText value="no records"/> 71                 </apex:column> 72             </apex:pageBlockTable> --> 73              74             <apex:pageBlockSection > 75                 <apex:commandButton value="add" action="{!add}" /> 76                 <apex:commandButton value="save" action="{!save}" /> 77             </apex:pageBlockSection> 78             <apex:pageMessages /> 79         </apex:pageBlock> 80     </apex:form> 81 </apex:page>

其中,VF代碼中注釋的內容為通過js和ajax請求後台,後台方法已省略,格式為

@RemoteAction標籤是必不可少的,聲明此方法用於js和後台互動。使用此標籤則方法必須是static類型。

Controller層代碼如下

1 public with sharing class GoodsController {  2          3        public List<GOODS__c> goodsList{get;set;}  4          5        public List<SelectOption> goodsTypes = new List<SelectOption>();  6          7      8          9        public GOODS__c goods{get;set;} 10         11        public Boolean isStatus{get;set;} 12         13        public String goodsName{get;set;} 14         15        public String goodsType{get;set;} 16         17        public Decimal goodsPriceStart{get;set;} 18      19        public Decimal goodsPriceEnd{get;set;} 20      21        public String goodsDescribe{get;set;}   22      23           24         25        public GoodsController() { 26            goodsList = new List<GOODS__c>(); 27            refreshData(); 28        } 29         30         31         32         33        public List<selectOption> getTypeNames() { 34            goodsTypes.clear(); 35         goodsTypes.add(new SelectOption(‘手機‘,‘手機‘)); 36            return goodsTypes; 37        } 38         39         40        //重新整理資料作用 41        public void refreshData() { 42            Boolean isStatus = true; 43            String goodsQueryString = ‘SELECT   GoodsBrand__c,‘+ 44                                       ‘GoodsDescribe__c,GoodsName__c, GoodsType__c, GoodsPrice__c,‘+ 45                                        ‘  Id FROM Goods__c where IsDeleted = false limit 100‘; 46            goodsList = Database.query(goodsQueryString); 47        } 48         49        public void save() { 50            try { 51                upsert goodsList; 52            } catch(DmlException e) { 53                ApexPages.addMessages(e); 54            } 55        } 56         57        public void deleteGoods() { 58            Id id = ApexPages.currentPage().getParameters().get(‘goodsId‘); 59            Database.delete(id); 60            refreshData(); 61        } 62         63        public void add() { 64            if(goodsList == null) { 65                goodsList = new List<GOODS__c>(); 66            } 67            GOODS__c goods2 = new GOODS__c(); 68            System.debug(‘-----------goodsList------------------‘+goodsList); 69            if(goodsList.size() == 0) { 70                goodsList.add(goods2); 71            } else { 72                goodsList.add(0,goods2); 73            } 74        } 75         76        public void query() { 77            String goodsSql = ‘SELECT GoodsBrand__c,‘+ 78                               ‘GoodsDescribe__c,GoodsName__c , GoodsType__c, GoodsPrice__c,‘+ 79                                ‘ Id FROM GOODS__c where IsDeleted = false ‘; 80            if(goodsName.length() >0) { 81                goodsName = ‘%‘ + goodsName + ‘%‘; 82                goodsSql += ‘ and GoodsName__c like :goodsName ‘; 83            } 84            if(goodsType.length() > 0) { 85                goodsType = ‘%‘ + goodsType + ‘%‘; 86                goodsSql += ‘ and GoodsType__c like :goodsType‘; 87            } 88            if(goodsDescribe.length() > 0) { 89                goodsDescribe = ‘%‘ + goodsDescribe + ‘%‘; 90                goodsSql += ‘ and GoodsDescribe__c like :goodsDescribe‘; 91            } 92             93            if(String.valueOf(goodsPriceStart).length()>0) { 94                goodsSql += ‘ and GoodsPrice__c >= :goodsPriceStart‘; 95            } 96            if(String.valueOf(goodsPriceEnd).length()>0) { 97                goodsSql += ‘ and GoodsPrice__c <= :goodsPriceEnd‘; 98            } 99            goodsSql += ‘ limit 100‘;100            goodsList = Database.query(goodsSql);101            goodsName = goodsName.remove(‘%‘);102            goodsDescribe = goodsDescribe.remove(‘%‘);103            goodsType = goodsType.remove(‘%‘);104        }105}

58行中Id id = ApexPages.currentPage().getParameters().get(‘goodsId‘);解釋一下:

ApexPages為VF頁面的控制類,此句的意思為擷取當前頁面的Parameter中key為goodsId的value,goodsId在頁面中通過<apex:param>封裝。

 

Salesforce零基礎(三)簡單的資料增刪改查頁面的構建(Ajax樣式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.