我正在尝试完成使用闪电Web组件的过程。
基本上,这里有三个主要对象:事件,访问评论和产品。
当用户想要创建访问评论时,他们进入Event页面并按下一个按钮,该按钮将打开一个弹出式模式,他们可以用必要的数据填充该模式以创建与该事件相关的访问评论。这是用闪电记录形式实现的,效果很好。
输入图片描述
之后,当用户按下"保存"按钮,记录被创建,并显示在事件页的lighting-datatable中。
当用户按下"保存"按钮,它还打开了一个新的模态(类似于前一个),用户可以添加与他们刚刚在前一个模态上创建的访问评论相关的产品。
基本上,这个过程从创建一个对象的记录开始,"Visit Comment"并以创建另一个对象"Product"的一条/多条记录结束,该对象与之前创建的访问注释相关。
我的问题是,我不知道如何指定"id"在第一个模态中初始创建的访问评论,并将其发送给第二个模态。
由于第一个模态从用户当前查看的记录页面获得了他的事件Id,我如何获得访问评论Id并将其发送到下一个模态,以便用户可以将产品添加到该访问评论中,即使他仍然在事件页面上。
下面是我的代码:
HTML:
<template>
<lightning-card title="Visit Comments related to this Event's Doctor">
<lightning-button class="slds-m-around_medium" label="New Visit Comment"
onclick={customShowModalPopup} slot="actions">
</lightning-button>
<!-- <break></break> -->
<lightning-datatable
data={data}
columns={columns}
key-field="id"
onrowaction={handleRowAction}
hide-checkbox-column="true">
</lightning-datatable>
</lightning-card>
<template if:true={customFormModal}>
<section role="dialog" tabindex="-1" aria-modal="true" aria-labelledby="modal-heading-01" class="slds-modal slds-fade-in-open">
<div class="slds-modal__container">
<button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse">
<svg class="slds-button__icon slds-button__icon_large" aria-hidden="true">
<use xlink:href="/assets/icons/utility-sprite/svg/symbols.svg#close"></use>
</svg>
<span class="slds-assistive-text">Cancel and close</span>
</button>
<div class="slds-modal__header">
<h1 id="modal-heading-01" class="slds-modal__title slds-hyphenate">New Visit Comment</h1>
</div>
<div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
<lightning-record-form
object-api-name={visitCommentObject}
fields={myFields}
record-id={visitCommentRecordId}
onsuccess={handleVisitCommentCreated}
oncancel={customHideModalPopup}>
</lightning-record-form>
</div>
</div>
</section>
<div class="slds-backdrop slds-backdrop_open" role="presentation"></div>
</template>
<template if:true={customFormModalProduct}>
<section role="dialog" tabindex="-1" aria-modal="true" aria-labelledby="modal-heading-01" class="slds-modal slds-fade-in-open">
<div class="slds-modal__container">
<button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse">
<svg class="slds-button__icon slds-button__icon_large" aria-hidden="true">
<use xlink:href="/assets/icons/utility-sprite/svg/symbols.svg#close"></use>
</svg>
<span class="slds-assistive-text">Cancel and close</span>
</button>
<div class="slds-modal__header">
<h1 id="modal-heading-02" class="slds-modal__title slds-hyphenate">Add Products</h1>
</div>
<div class="slds-modal__content slds-p-around_medium" id="modal-content-id-2">
<lightning-record-form
object-api-name={productObject}
fields={myFieldsProduct}
onsuccess={handleProductCreated}
oncancel={customHideModalPopupProduct}>
</lightning-record-form>
</div>
</div>
</section>
<div class="slds-backdrop slds-backdrop_open" role="presentation"></div>
</template>
</template>
JS(丢弃了一些与lightning-datatable相关的不必要的代码):
import { LightningElement, track, api, wire} from 'lwc';
//import { updateRecord } from 'lightning/uiRecordApi';
import { deleteRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import getVisitCommentList from '@salesforce/apex/datatableWithRowsSelectionVisitComment.getVisitCommentList';
import { NavigationMixin } from 'lightning/navigation';
import { refreshApex } from '@salesforce/apex';
import VISITCOMMENT_OBJECT from '@salesforce/schema/VisitComment__c';
import NAME_FIELD from '@salesforce/schema/VisitComment__c.Name';
import INDIVIDUAL_FIELD from '@salesforce/schema/VisitComment__c.Individual__c';
import DATE_FIELD from '@salesforce/schema/VisitComment__c.Date__c';
import LOCATION_FIELD from '@salesforce/schema/VisitComment__c.Location__c';
import COMMENT_FIELD from '@salesforce/schema/VisitComment__c.Comments__c';
import PRODUCT_OBJECT from '@salesforce/schema/AddedProduct__c';
import PRODUCTNAME_FIELD from '@salesforce/schema/AddedProduct__c.Product__c';
import PRIORITY_FIELD from '@salesforce/schema/AddedProduct__c.Priority__c';
import REACTION_FIELD from '@salesforce/schema/AddedProduct__c.Reaction__c';
import PRESCRIPTION_FIELD from '@salesforce/schema/AddedProduct__c.Prescription__c';
import getSingleVisitComment from '@salesforce/apex/datatableWithRowsSelectionVisitComment.getSingleVisitComment';
const actions= [
{ label : 'View', name : 'view' },
{ label : 'Delete', name : 'delete' }
];
const columns= [
{ label : 'Visit Comment Name', fieldName : 'Name' },
{ label : 'Date', fieldName : 'Date__c' },
{ label : 'Comments', fieldName : 'Comments__c' },
{
type : 'action',
typeAttributes : { rowActions : actions }
}
];
export default class DatatableVisitComment extends NavigationMixin(LightningElement) {
visitCommentObject = VISITCOMMENT_OBJECT;
productObject= PRODUCT_OBJECT;
myFields = [NAME_FIELD, INDIVIDUAL_FIELD, DATE_FIELD, LOCATION_FIELD, COMMENT_FIELD];
myFieldsProduct =[PRODUCTNAME_FIELD, PRIORITY_FIELD, REACTION_FIELD, PRESCRIPTION_FIELD];
columns = columns;
@track data = [];
@api recordId; //stores current page Id
@api objectApiName;
@track customFormModal = false;
@track customFormModalProduct = false;
refreshTable;
@wire(getVisitCommentList, { lwcRecordId: '$recordId' })
relations(result) {
this.refreshTable = result;
if (result.data) {
this.data = result.data;
this.emptyList = true;
}
}
}
//Open the Modal
customShowModalPopup() {
this.customFormModal = true;
}
//Close the Modal
customHideModalPopup() {
this.customFormModal = false;
}
// Close the Modal and refresh datatable data after Visit Comment is inserted
handleVisitCommentCreated(){
this.customHideModalPopup();
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Record Inserted',
variant: 'success'
}),
);
this.handleAddProduct(); // METHOD TO OPEN UP PRODUCT MODAL
debugger;
return refreshApex(this.refreshTable);
}
handleAddProduct(){
this.customFormModalProduct = true;
}
}
handleadproduct()方法打开了模态,但我不知道如何指定访问评论Id,所以当创建产品记录时,与先前创建的访问评论相关。
有没有办法发送一个记录的Id刚刚在一个模态创建到另一个模态?
非常感谢您的帮助:)
您的onsuccess (handleVisitCommentCreated
)当前没有任何参数。科幻小说寄给你一些东西,但你忽略了它。如果您将其更改为接受一个变量,例如"event",您将能够获得创建的记录id为";event.detail.id"。存储在this.commentId
或其他地方,然后在第二次弹出使用。
https://developer.salesforce.com/docs/component-library/bundle/lightning-record-form/documentation重读"创建记录">
这是…这不是一个好的设计。您可能会创建没有子产品的"寡妇"、"父"记录(注释)。也许用户取消了,从未添加它们,也许在创建产品时出现错误……再来一点" proquot;Move将拥有一个复杂的表单,但只有一个保存按钮。并将所有数据传递给Apex,并在一次全有或全无的事务中创建两者。