上一篇Lightning内容描述的是LDS,通过LDS可以很方便的实例化一个对象的数据信息。当我们通过列表展示数据需要编辑时,我们常使用两种方式去处理编辑页面:Pop Up Window弹出修改详情以及在本页面隐藏详情页面显示编辑页面。

实现这个功能以前主要需要先了解几个标签:

lightning:recordForm: 此标签允许用户快速的创建一个form去查看,添加以及修改一条记录。集合了 lightning:recordEditForm 以及 lightning:recordViewForm 两个标签功能。

下面例举一些此标签常用的属性:

objectName: 想要展示的object的API Name,此属性为必填的属性;

recordId:想要展示记录的ID;

mode:指定Form的交互方式以及样式。值有三个:

  • view – 记录在View模式下展示,默认显示编辑(铅笔)图标,点击编辑图标后进入编辑模式;
  • edit – 记录展示包含Save和Cancel的Edit模式;
  • readonly – 记录在View模式下展示,不允许编辑。

更多属性请查看:https://developer.salesforce.com/docs/component-library/bundle/lightning:recordForm/specification

上面也说到了lightning:recordForm集合了lightning:recordViewForm以及lightning:recordEditForm.下面的demo是通过这两个元素进行展示,所以简单的说一下这两个标签:

lightning:recordViewForm:此标签封装了一个wrapper,通过recordId, 使用lightning:outputField用来展示记录相关的字段值以及Label名称。正常我们想要展示一条记录,按照之前的学习有两种实现的方式,第一种是后台搜索出来,init handler实例化,第二种是使用LDS,通过此标签,我们只需要传递记录ID,便可以使用记录中所有可以访问字段的信息。

此元素有两个必填的属性:

objectApiName:想要展示的object的API Name;

recordId: 想要展示数据的ID。

更多属性请查看:https://developer.salesforce.com/docs/component-library/bundle/lightning:recordViewForm/specification

lightning:recordEditForm:此标签用途和lightning:recordViewForm大部分相同,区别为此标签用于Form配合lightning:inputField实现编辑一个form功能。如果recordId为空,则进行创建一条数据的功能,如果recordId有值,则进行更新记录功能。

官方提供的简单的demo如下:https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/documentation

常用属性:

objectApiName:想要编辑的object的API Name;

recordId:想要编辑的记录的Id,如果此属性为空,则认为是新建记录;

recordTypeId:想要编辑的记录的record type id,用于指定新建/编辑记录的record type

onload:Form数据加载后触发的回调函数;

onsubmit:Form数据submit后触发的回调函数;

onsuccess:数据操作成功后的回调函数;

onerror: 数据操作失败后的回调函数;

更多属性请参看:https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/specification

lightning:overlayLibrary:此标签包含两个功能:弹出popup modal以及展示Popover(弹出框)。此标签包含了两个主要的方法:

showCustomModal:此方法用于弹出一个popup modal,样式和使用标准界面修改一条记录弹出的modal类似。此方法包含以下常用参数:

  • header:传入类型为object,用于展示在modal header部分;
  • body:传入类型为object,用于展示在modal body部分;
  • footer:传入类型为object,用于展示在modal的footer部分;
  • showCloseButton:指定是否在modal中展示关闭按钮,默认为true;
  • cssClass:逗号分隔的一个list的css class应用于此modal;
  • closeCallback:modal关闭时的回调函数。

此方法的语法格式如下:

component.find('overlayLib').showCustomModal({
//modal attributes
}).then(function (overlay) {
//closes the modal immediately
overlay.close();
});

modal效果展示图如下:

showCustomPopover:此方法用于弹出一个弹出框,类似标签中title样式,hover后在旁边展示的描述信息的效果。此方法包含以下常用参数:

  • body:传入类型为object,用于展示popover中的body部分;
  • referenceSelector:指定popover要展示在哪个元素后面;
  • cssClass:逗号分隔的一个list的css class应用于此modal。

popover显示效果如下:

这两个方法的demo可以访问:https://developer.salesforce.com/docs/component-library/bundle/lightning:overlayLibrary/documentation

注意:演示这两个功能时,一定注意不要使用single app,single app展示这种方式会出现报错:

测试这两种样式可以考虑demo中component implements="flexipage:availableForAllPageTypes",然后放在一个object的detail页面看效果。下面为demo部分。

一. 列表使用Card样式展示,点击Edit图标覆盖原View布局显示Edit布局,点击Save以后显示View布局的List样式。

1.SimpleAccountList:此类用于默认初始化搜索出来10条Account信息;

 public class SimpleAccountListController {
@AuraEnabled
public static List<Account> getAccountList() {
return [SELECT Id,Name,AccountNumber,Site
FROM Account
LIMIT 10];
}
}

2.SimpleAccount.cmp:用于默认初始化展示view视图,点击edit的icon后显示edit部分视图;

 <aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="acc" type="Account"/>
<lightning:recordViewForm aura:id="viewForm" recordId="{!v.acc.Id}" objectApiName="Account">
<div class="slds-media">
<div class="slds-media__body">
<lightning:layout class="slds-hint-parent">
<a onclick="{!c.navToRecord}">
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{!v.acc.Name}</h3>
</a>
<lightning:buttonIcon iconName="utility:edit" class="slds-col_bump-left" iconClass="slds-button__icon_hint" variant="bare" alternativeText="Edit Record" onclick="{!c.editRecord}"/>
</lightning:layout>
<lightning:layout multipleRows="true">
<lightning:layoutItem size="6">
<lightning:outputField fieldName="AccountNumber"/>
</lightning:layoutItem>
<lightning:layoutItem size="6">
<lightning:outputField fieldName="Site"/>
</lightning:layoutItem>
</lightning:layout>
</div>
</div>
</lightning:recordViewForm>
<lightning:recordEditForm aura:id="editForm" recordId="{!v.acc.Id}" objectApiName="Account" class="slds-hide" onsuccess="{!c.handleSuccess}">
<div class="slds-media"> <div class="slds-media__body">
<lightning:layout>
<a onclick="{!c.navToRecord}">
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{!v.acc.Name}</h3>
</a>
</lightning:layout>
<lightning:layout multipleRows="true">
<lightning:layoutItem size="6">
<lightning:inputField fieldName="AccountNumber"/>
</lightning:layoutItem>
<lightning:layoutItem size="6">
<lightning:inputField fieldName="Site"/>
</lightning:layoutItem>
</lightning:layout>
<lightning:layout horizontalAlign="center" class="slds-m-top_large">
<lightning:button variant="neutral" label="Cancel" title="Cancel" type="text" onclick="{!c.handleCancel}"/>
<lightning:button variant="brand" label="Submit" title="Submit" type="submit"/>
</lightning:layout>
</div>
</div>
</lightning:recordEditForm>
</aura:component>

3.SimpleAccountController.js: 处理跳转到account页面以及点击edit,save以及cancel操作处理;

 ({
navToRecord : function (component, event, helper) {
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": component.get("v.acc.Id")
});
navEvt.fire();
},
editRecord : function(component, event, helper) {
helper.showHide(component);
},
handleSuccess : function(component, event, helper) {
helper.showHide(component);
var recUpdate = $A.get("e.c:recordUpdated");
recUpdate.fire();
},
handleCancel : function(component, event, helper) {
helper.showHide(component);
event.preventDefault();
}
})

4.SimpleSimpleAccountHelper.js:用于切换view/edit视图的显示;

 ({
showHide : function(component) {
var editForm = component.find("editForm");
$A.util.toggleClass(editForm, "slds-hide");
var viewForm = component.find("viewForm");
$A.util.toggleClass(viewForm, "slds-hide");
}
})

5.SimpleAccountList.cmp:用于展示account的list;

 <aura:component  controller="SimpleAccountListController" implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="simpleAccounts" type="Object[]"/>
<aura:attribute name="acc" type="Account"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:card iconName="standard:account" title="simple accounts" class="slds-is-relative">
<div class="slds-p-left_medium slds-p-right_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<aura:if isTrue="{!v.simpleAccounts.length > 0}">
<aura:iteration items="{!v.simpleAccounts}" var="item">
<li class="slds-list__item">
<c:SimpleAccount acc="{!item}"/>
</li>
</aura:iteration>
<aura:set attribute="else">
<li class="slds-list__item">
<h3 class="slds-text-small slds-text-color_error">No accounts found.</h3>
</li>
</aura:set>
</aura:if>
</ul>
</div>
</lightning:card>
</aura:component>

6. SimpleAccountListController.js:用于初始化数据

 ({
doInit : function(component, event, helper) { var action = component.get("c.getAccountList");
action.setCallback(this, function(response){
var simpleAccounts = response.getReturnValue();
component.set("v.simpleAccounts", simpleAccounts); });
$A.enqueueAction(action);
}
})

7.将SimpleAccountList.cmp放在account的detail page中。

效果展示:

1.默认通过card布局展示列表

2.点击其中的一个edit,会切换成edit模式,其他的不变化;

3.点击save后正常显示save以后的列表效果。

二.显示View列表,点击Edit后展示PopUp Modal,修改后,隐藏Modal,然后继续展示列表。

1.SimpleAccountEdit.cmp:用来展示编辑Account的UI

 <aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="recordId" type="String" />
<lightning:overlayLibrary aura:id="popuplib"/>
<lightning:recordEditForm aura:id="editForm" recordId="{!v.recordId}" objectApiName="Account" onsuccess="{!c.handleSuccess}" onsubmit="{!c.handleSubmit}">
<div class="slds-media">
<div class="slds-media__body">
<lightning:layout multipleRows="true">
<lightning:layoutItem size="6">
<lightning:inputField fieldName="AccountNumber"/>
</lightning:layoutItem>
<lightning:layoutItem size="6">
<lightning:inputField fieldName="Site"/>
</lightning:layoutItem>
</lightning:layout>
<lightning:layout horizontalAlign="center" class="slds-m-top_large">
<lightning:button variant="brand" label="Submit" title="Submit" type="submit"/>
</lightning:layout>
</div>
</div>
</lightning:recordEditForm> </aura:component>

2. SimpleAccountEditController.js:Edit操作 submit以及操作success的handler操作;

 ({
handleSubmit : function(component,event,helper) {
component.find("editForm").submit();
},
handleSuccess : function(component,event,helper) {
console.log('save success');
component.find("popuplib").notifyClose();
},
})

3.SimpleAccountUsingModal.cmp:用来显示Account的View UI

 <aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="acc" type="Account"/>
<lightning:overlayLibrary aura:id="popuplib"/>
<lightning:recordViewForm aura:id="viewForm" recordId="{!v.acc.Id}" objectApiName="Account">
<div class="slds-media"> <div class="slds-media__body">
<lightning:layout class="slds-hint-parent">
<a onclick="{!c.navToRecord}">
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{!v.acc.Name}</h3>
</a>
<lightning:buttonIcon iconName="utility:edit" class="slds-col_bump-left" iconClass="slds-button__icon_hint" variant="bare" alternativeText="Edit Record" onclick="{!c.handleClick}"/>
</lightning:layout>
<lightning:layout multipleRows="true">
<lightning:layoutItem size="6">
<lightning:outputField fieldName="AccountNumber"/>
</lightning:layoutItem>
<lightning:layoutItem size="6">
<lightning:outputField fieldName="Site"/>
</lightning:layoutItem>
</lightning:layout>
</div>
</div>
</lightning:recordViewForm>
</aura:component>

4.SimpleAccountUsingModalController.js:用于点击编辑按钮,显示SimpleAccountEdit组件;

 ({
handleClick : function(component, event, helper) {
var modalBody;
$A.createComponent("c:SimpleAccountEdit", { recordId : component.get('v.acc.Id')},
function(content, status) {
modalBody = content;
if (status === "SUCCESS") {
component.find('popuplib').showCustomModal({
header: "Account Edit",
body: modalBody,
showCloseButton: true,
cssClass: "mymodal",
});
}
});
}
})

5.SimpleAccountListUsingModal.cmp: 用于列表循环simpleAccountUsingModal组件,列表显示card布局的account数据;

 <aura:component  controller="SimpleAccountListController" implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="simpleAccounts" type="Object[]"/>
<aura:attribute name="acc" type="Account"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <lightning:card iconName="standard:account" title="simple accounts" class="slds-is-relative"> <div class="slds-p-left_medium slds-p-right_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<aura:if isTrue="{!v.simpleAccounts.length > 0}">
<aura:iteration items="{!v.simpleAccounts}" var="item">
<li class="slds-list__item">
<c:SimpleAccountUsingModal acc="{!item}"/>
</li>
</aura:iteration>
<aura:set attribute="else">
<li class="slds-list__item">
<h3 class="slds-text-small slds-text-color_error">No accounts found.</h3>
</li>
</aura:set>
</aura:if>
</ul> </div>
</lightning:card>
</aura:component>

6.SimpleAccountListUsingModalController.js:用于初始化list数据

 ({
doInit : function(component, event, helper) { var action = component.get("c.getAccountList");
action.setCallback(this, function(response){
var simpleAccounts = response.getReturnValue();
component.set("v.simpleAccounts", simpleAccounts); });
$A.enqueueAction(action);
}
})

效果展示:

1.正常显示account的list

2.点击Edit按钮以后会弹出popup modal用来显示编辑Form

3.点击Submit后继续展示account list,modal消失。

总结:篇中使用两种方式实现list 模式下两种方式Edit数据方式,demo比较粗糙,其中有很多地方是可以优化的,比如edit没有处理异常的操作等等。感兴趣的同学可以考虑优化,篇中有问题的地方欢迎指出。不懂得欢迎留言。

salesforce lightning零基础学习(七) 列表展示数据时两种自定义编辑页面的更多相关文章

  1. salesforce lightning零基础学习(十五) 公用组件之 获取表字段的Picklist(多语言)

    此篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) 我们在lightning中在前台会经常碰到获取pi ...

  2. salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容

    本篇参考: https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader https://github.com/SheetJS/sheetjs ...

  3. salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型

    看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...

  4. salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)

    在salesforce的classic中,我们使用{!expresion}在前台页面展示信息,在lightning中,上一篇我们也提及了,如果展示attribute的值,可以使用{!v.expresi ...

  5. salesforce lightning零基础学习(十三) 自定义Lookup组件(Single & Multiple)

    上一篇简单的介绍了自定义的Lookup单选的组件,功能为通过引用组件Attribute传递相关的sObject Name,捕捉用户输入的信息,从而实现搜索的功能. 我们做项目的时候,可能要从多个表中获 ...

  6. salesforce lightning零基础学习(十一) Aura框架下APP构造实现

    前面的一些lightning文章讲述了aura的基础知识,aura封装的常用js以及aura下的事件处理.本篇通过官方的一个superbadge来实现一个single APP的实现. superbad ...

  7. salesforce lightning零基础学习(八) Aura Js 浅谈一: Component篇

    我们在开发lightning的时候,常常会在controller.js中写 component.get('v.label'), component.set('v.label','xxValue'); ...

  8. salesforce lightning零基础学习(六)Lightning Data Service(LDS)

    本篇可参看:https://trailhead.salesforce.com/modules/lightning_data_service Lightning中针对object的detail页面,一个 ...

  9. salesforce lightning零基础学习(四) 事件(component events)简单介绍

    lightning component基于事件驱动模型来处理用户界面的交互.这种事件驱动模型和js的事件驱动模型也很相似,可以简单的理解成四部分: 1.事件源:产生事件的地方,可以是页面中的输入框,按 ...

随机推荐

  1. Java 后台验证的工具类

    Java 后台验证的工具类 public class ValidationUtil {         //手机号     public static String mobile = "^( ...

  2. 【转载】通过sql server 连接mysql

    http://www.cnblogs.com/goole/p/5291286.html 1.在SQL SERVER服务器上安装MYSQL ODBC驱动; 驱动下载地址:http://dev.mysql ...

  3. PHP开发——数据类型

    概述 l  变量就是一个容器,变量本身并没有类型,变量的类型解决值的类型. l  PHP和JS都属于弱类型语言,变量在运行过程中,类型是可以变的.但是,Java不可以. l  标量(基本)数据类型:字 ...

  4. 201621123002《Java程序设计》第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  5. Java内存泄露监控工具:JVM监控工具介绍

    本文将对JVM监控工具jstack, jconsole, jinfo, jmap, jdb, jstat进行详细的介绍,具体内容请看下文 Sun JDK监控和故障处理工具 名称 主要作用 jps JV ...

  6. String 常用函数

    判断字符串是否包含指定字符str.contains("string"); 查找指定字符索引str.indexOf("s"'); 查找最后出现的字符索引str.i ...

  7. 【project】【Maven】dynamic web module 3.1 requires 1.7

    Maven导入和新建java web 项目时可能报的错. 解决方案: 1.保证 在eclipse 构建 web中关于java版本有三处需要修改统一:  右击项目,选择“propertie”===> ...

  8. JS基础-运算符-函数

    1.运算符  1.赋值运算符和扩展运算符    1.赋值运算符 =    2.扩展运算符      +=,-=,*=,/=,%=,^=....      ex:        a=a+b;--> ...

  9. python发送邮箱

    一.Python发送邮件 import smtplib from email.mime.text import MIMEText def send(email_to,title,content): m ...

  10. 13. The Impact of New Technology on Business 新科技对商务的影响

    13. The Impact of New Technology on Business 新科技对商务的影响 (1) New technology links the world as never b ...