//component
<aura:component controller="LookupRelatedContactC"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="selectedLookUpRecord" type="sObject" default="{}"/>
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columns" type="List"/>
<aura:attribute name="selectedRows" type="List"/>
<aura:attribute name="rowIndex" type="String"/>
<aura:attribute name="options" type="List" default="[
{'label': 'Contacts', 'value': 'option1'},
{'label': 'Opportunities', 'value': 'option2'}]"/>
<aura:attribute name="value" type="String" default="option1"/>
<aura:handler name="change" value="{!v.selectedLookUpRecord}" action="{!c.selectedLookupChanged}"/>
<lightning:card title = 'Search by Account Name' >
<c:LookupReusable_Parent objectAPIName="account" IconName="standard:account"
selectedRecord="{!v.selectedLookUpRecord}"
label="Accounts"/>
<lightning:radioGroup name="radioGroup"
label="Select any one of these"
options="{! v.options }"
value="{! v.value }"
type="radio"/>
<br/><br/>
<lightning:datatable columns="{! v.columns }"
data="{! v.data }"
keyField="id"
hideCheckboxColumn="true"
onrowaction="{!c.removeRecord}"/><br></br>
<lightning:button variant="brand" label="Save" title="Brand action" onclick="{! c.handleClick}" />
<lightning:button variant="brand" label="Add Row" title="Brand action" onclick="{! c.handleClick}" />
</lightning:card>
</aura:component>
//js controller
({
selectedLookupChanged:function(component, event, helper) {
component.set('v.columns', [
{label: 'Contact Name',
fieldName: 'Name',
editable: true,
type: 'text'},
{label: 'Phone',
fieldName: 'Phone',
type: 'phone'},
{label: "Action",type: "button", typeAttributes: {
label: 'Delete',
name: 'delete',
title: 'Delete',
disabled: false,
value: 'delete',
iconPosition: 'left'
}},
]);
var aId = component.get("v.selectedLookUpRecord").Id;
var action=component.get('c.getCon');
action.setParams({accId : aId});
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
var rows1 = response.getReturnValue();
component.set("v.data", rows1);
}
});
$A.enqueueAction(action);
}
})
//apex class
public class LookupRelatedContactC {
@AuraEnabled
public static List<Contact> getCon(List<String> accId) {
return [SELECT Id, Name, Phone FROM Contact WHERE AccountId =:accId];
}
}
在上面的代码中,我被困在我无法在闪电数据表中动态添加行或删除任何行的点上,我在互联网上发现的大多数人都是我用它完成了光环迭代但没有用数据表完成的。所以当我按下删除它应该触发 js 事件或者当我添加行时它应该触发 js 我不想一次又一次地调用服务器端方法来添加或删除。所以,一旦我完成了删除或添加行,然后在按下保存按钮之后,它应该触发一次服务器端方法并应该保存到数据库中请帮助