Salesforce Aura開發 Component表單初解


初步了解了Lightning的組件開發流程后,我們來認識下lightning的表單

點擊對象管理器,選擇對象:電影(Movie__c),創建字段

標簽       API         數據類型        
 票價  Number__c  數字(16,2)
 是否上映  Flag__c  復選框

 

 

 

關於對象電影的相關內容及相關組件請參考之前的博客內容:http://www.cnblogs.com/luqinghua/p/8979893.html

1.創建組件:MyTest_CreateMovie

 1 <aura:component Controller="MyTestMovieController">
 2     <aura:attribute name="movieList" type="Movie__c[]"/>
 3     <aura:attribute name="movie" type="Movie__c" 
 4                     default="{'sobjectType':'Movie__c',
 5                              'Name':'',
 6                              'Director__c':'',
 7                              'ToStar__c':'',
 8                              'ShowTime__c':'',
 9                              'Number__c':0,
10                              'Flag__c':false}"/>    
11     <!-- 表頭 -->
12      <lightning:layout class="slds-page-header slds-page-header--object-home">
13         <lightning:layoutItem >
14             <lightning:icon iconName="standard:scan_card" alternativeText="新電影"/>
15         </lightning:layoutItem>
16         <lightning:layoutItem padding="horizontal-small">
17             <div class="page-section page-header">
18                 <h1 class="slds-text-heading--label">電影</h1>
19                 <h1 class="slds-text-heading--medium">新電影</h1>
20             </div>
21         </lightning:layoutItem>
22     </lightning:layout>
23     <!-- 電影字段列表    -->
24     <lightning:layout >
25         <lightning:layoutItem padding="around-small" size="6">
26             <div aria-labelledby="newMovieForm">
27                 <fieldset class="slds-box slds-theme--default slds-container--small">
28                     <legend id="newMovieForm" class="slds-text-heading--small slds-p-vertical--medium">
29                         新建電影
30                     </legend>
31                     <form class="slds-form--stacked">
32                         <lightning:input aura:id="MovieForm" label="名稱"
33                                          name="movieName"
34                                          value="{!v.movie.Name}"
35                                          required="true"/>                        
36                         <lightning:input aura:id="MovieForm" label="導演"
37                                          name="movieDirector"
38                                          value="{!v.movie.Director__c}"
39                                          placeholder="請輸入導演名稱"/>
40                          <lightning:input aura:id="MovieForm" label="主演"
41                                          name="movieToStar"
42                                          value="{!v.movie.ToStar__c}"
43                                          placeholder="請輸入主演名稱"/>       
44                         <lightning:input type="number" aura:id="MovieForm" label="票價"
45                                          name="movieNumber"
46                                          formatter="currency"
47                                          min="1"
48                                          step="0.5"
49                                          value="{!v.movie.Number__c}"
50                                          messageWhenRangeUnderflow="票價最低1元"/>                        
51                         <lightning:input type="date" aura:id="MovieForm" label="上映時間"
52                                          name="movieShowTime"
53                                          value="{!v.movie.ShowTime__c}"/>                        
54                         <lightning:input type="checkbox" aura:id="MovieForm" label="是否上映"
55                                          name="movieFlag"
56                                          checked="{!v.movie.Flag__c}"/>
57                         <lightning:button label="加入電影列表"
58                                           class="slds-m-top--medium"
59                                           variant="brand"
60                                           onclick="{!c.AddToList}"/>
61                     </form>
62                 </fieldset>
63             </div>           
64         </lightning:layoutItem>
65     </lightning:layout>
66 </aura:component>

將該組件放在 My_Test.app中並預覽

可以看到如上圖所示的一個表單,包含了常用的復選框,日期,數字,文本等類型,然后是完成后面的創建方法

2.補充MyTest_CreateMovieController.js

 1 ({
 2     AddToList : function(component, event, helper) {
 3         //系統提供的校驗錯誤信息的標准方法可校驗必填項以及最小值等等
 4         var validExpense = component.find('MovieForm').reduce(function (validSoFar, inputCmp) {
 5             // 顯示填寫錯誤的字段消息
 6             inputCmp.showHelpMessageIfInvalid();
 7             return validSoFar && inputCmp.get('v.validity').valid;
 8         }, true);
 9          // 通過字段校驗后繼續創建的邏輯
10         if(validExpense){
11             // 創建一條記錄
12             var movie = component.get("v.movie");
13             console.log("傳入的電影信息: " + JSON.stringify(movie));
14             helper.createMovie(component, movie);
15             //將表單重置
16              component.set("v.movie",{'sobjectType':'Movie__c',
17                              'Name':'',
18                              'Director__c':'',
19                              'ToStar__c':'',
20                              'ShowTime__c':'',
21                              'Number__c':0,
22                              'Flag__c':false});
23         }
24     }
25 })

在MyTest_CreateMovieController.js中完成對表單數據的基本校驗,比如表單的必填項,以及設置的票價不小於1元等等

3.補充MyTest_CreateMovieHelper.js

({
    createMovie : function(component, movie) {
        //調用Apex類中的方法
        var action = component.get("c.saveMovie");
        //傳遞參數
        action.setParams({
            "movie": movie
        });
        //方法調用
        action.setCallback(this, function(response){
            //方法調用狀態
            var state = response.getState();
            if (state === "SUCCESS") {
                var movieList = component.get("v.movieList");
                movieList.push(response.getReturnValue());
                component.set("v.movieList", movie);
            }
        });
        var movie = component.get("v.movie");        
        $A.enqueueAction(action);
    }
})

MyTest_CreateMovieHelper.js中主要是與后台APEX控制類中的方法進行交互,將數據存入數據庫中保存起來

4.更新MyTestMovieController類,在其中加入saveMovie方法

 1 /*********
 2      *  Author:ricardo
 3      *  Time:2018-03-21
 4      *  Function:MyTest_Movie 后台控制類
 5      *  Test:
 6      */
 7 public class MyTestMovieController{
 8     //初始化
 9     @AuraEnabled
10     public static List<Movie__c> GetAll(){
11         List<Movie__c> movieList = new List<Movie__c>();
12         movieList = [select ShowTime__c,ToStar__c,Theme__c,Director__c,Name from Movie__c limit 50];
13         return movieList;
14     }
15     //創建記錄
16     @AuraEnabled
17     public static Movie__c saveMovie(Movie__c movie) {
18         // Perform isUpdatable() checking first, then
19         upsert movie;
20         return movie;
21     }
22 }

綜上所示,一個簡單的創建電影條目的表單就完成了,打開組件MyTest_Movie就能看到我們新創建的電影記錄位列其中,如有遺漏歡迎指正,有問題可在評論區留言

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM