ApexClass与LightningWebComponent结合
使用Lwc时候,有时会遇到因为权限问题无法更新的状况,这个时候就要通过ApexClass进行更新操作,因为ApexClass可以无视权限,下边这个例子就是ApexClass与LightningWebComponent结合,实现更新画面上的项目。
html
<template>
<div class="sds_disclosure">
<div class="sds_disclosure__txt">
<span>状态>>>>>::: {disclosureCurrentValue}</span>
</div>
<button class="sds_disclosure__btn" onclick={handleDisClick}>变更按钮</button>
</div>
<template if:true={isDisclosureOn}></template>
<section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01"
aria-modal="true" aria-describedby="modal-content-id-1"
class="slds-modal slds-fade-in-open">
<div class="slds-modal__container">
<header class="slds-modal__header">
<h2 id="modal-heading-04"
class="slds-text-heading_medium slds-hyphenate">状态切换
</h2>
</header>
<div class="slds-modal__content slds-p-around_medium dialog-style" id="modal-content-id-5">
这个状态可以进行切换吗?</br>
</div>
<footer class="slds-modal__footer">
<button class="slds-button slds-button_brand"
onclick={handleSaveConfirmClick}>可以
</button>
<button class="slds-button slds-button_neutral"
onclick={handleSaveCancelClick}>不可以
</button>
</footer>
</div>
<template if:true={isSpinner}>
<lightning-spinner size="small" variant="basic" >
</lightning-spinner>
</template>
</section>
<div class="slds-backdrop slds-backdrop_open"></div>
</template>
</template>
jsp
import { LightningElement, api, track, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import { NavigationMixin } from 'lightning/navigation';
import { refreshApex } from '@salesforce/apex';
import ID_FIELD from '@salesforce/schema/InsurancePolicy.Id';
import DISCLOSURE_OBJECT from '@salesforce/schema/InsurancePolicy.Disclosure__c';
import updateInsuranceObject from '@salesforce/apex/updateInsuranceSwitchController.updateInsuranceObject';
const insFields = [
DISCLOSURE_OBJECT,
ID_FIELD
];
export default class InsurancePolicySwitching extends NavigationMixin(LightningElement) {
@api recordId;
@track disclosureCurrentValue;
@track disclosure;
@track isDisclosureOn = false;
@track isSpinner = false;
@track error;
@track insurancePolicyId;
@track wiredInsurancePolicy;
@wire(getRecord, { recordId: '$recordId', fields: insFields })
loadIns(value) {
this.wiredInsurancePolicy = value;
const { data, error } = value;
if (error) {
//
} else if (data && data.fields) {
this.insurancePolicyId = data.fields.Id.value;
this.disclosureCurrentValue = data.fields.Disclosure__c.value;
}
}
handleSaveCancelClick() {
//
}
handleSaveConfirmClick() {
const allValid = [...this.template.querySelectorAll('lightning-input')]
.reduce((validSoFar, inputFields) => {
inputFields.reportValidity();
return validSoFar && inputFields.checkValidity();
}, true);
if (allValid) {
updateInsuranceObject({ insurancePolicyId: this.recordId, disclosureValue: this.disclosure })
.then(result => {
if (result === 'success') {
this.isSpinner = false;
return refreshApex(this.wiredInsurancePolicy);
}
})
.catch(error => {
});
} else {
}
}
handleDisClick(event) {
if (this.disclosureCurrentValue === '状态值456') {
this.isDisclosureOn = true;
this.isSpinner = true;
this.disclosure = '状态值123';
}
}
}
css
.dialog-style {
text-align: center;
white-space: pre-wrap;
}
.sds_disclosure {
display: flex;
align-items: center;
}
.sds_disclosure__txt {
font-size: 12px;
}
.sds_disclosure__btn {
padding: 0 .8rem;
background: rgb(0, 44, 118);
}
.sds_disclosure__btn:hover {
background-color: rgb(0, 25, 67);
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
<target>lightningCommunity__Page</target>
<target>lightningCommunity__Default</target>
</targets>
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="recordId" type="String"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
ApexClass
public with sharing class updateInsuranceSwitchController {
@AuraEnabled(cacheable=false)
public static String updateInsuranceObject(String insurancePolicyId,String disclosureValue) {
InsurancePolicy insItem = new InsurancePolicy();
insItem.Id = insurancePolicyId;
insItem.Disclosure__c = disclosureValue;
Database.SaveResult resultObj = Database.update(insItem);
if (resultObj.isSuccess()){
system.debug('更新成功>>>>>>>>>>>>>>:::::');
return 'success';
} else {
system.debug('実行結果エラー>>>>>>>>::' + resultObj.getErrors());
return 'fail';
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。