从父 lwc 调用时,refreshApex 不会触发

发布于 2025-01-10 12:51:16 字数 5702 浏览 1 评论 0原文

我在父 lwc 上有一个按钮。调用时,它将调用子 lwc 上的方法,通过 apex 方法执行数据更新。完成后,它会调用refreshApex刷新闪电数据表上的数据。 RefreshApex 由于某种原因不会触发。

现在,如果我使用子项的保存按钮保存更改,refreshApex 将触发并且数据将更新,不会出现任何问题。

感谢您的帮助!

在此处输入图像描述

父组件

</div>
            <footer class="slds-modal__footer">
                <lightning-button label="Save" variant="brand" onclick={saveModal} class="slds-m-left_x-small"></lightning-button>
                <lightning-button label="Cancel" variant="neutral" onclick={closeModal} class="slds-m-left_x-small"></lightning-button>
            </footer>

父JS

saveModal() {
    //firing a child method
    this.template.querySelector("c-reuse-license-key-table").testHandleAction();
}

子组件

<template if:true={quotelinelist}>
        <div class="slds-grid_align-end" style="height: 500px; overflow: auto;">
            <lightning-datatable key-field="Id"
                                 data={quotelinelist}
                                 columns={columns}
                                 onsave= {testHandleAction}
                                 oncellchange= {handleRowAction}
                                 draft-values={fldsItemValues}
                                 hide-checkbox-column="true">  <!--suppress-bottom-bar="true"-->
            </lightning-datatable>
        </div>
    </template>

CHILD JS

@wire(getQuoteLinesList,{quoteId: '$parentRecordId', actionType: '$actionTypeForWire'})
cons(result) {
    console.log('check result 1 ', result)
    this.wiredDataResult = result;
    if(result.error) {
        console.log('inside wire error')
        //this.quotelineList = undefined;
    }
    else {
        console.log('inside else error')
        this.quotelinelist = result.data;
    }
}

handleRowAction(event) {
    lKeyDraftList.push(event.detail.draftValues);
}

@api async testHandleAction() {

    let strOriginalKeyValues = JSON.stringify(qlInList);
    let strlKeyDraftList = JSON.stringify(lKeyDraftList);
    let strDraftValue = [];

    lKeyDraftList.forEach(function(c){
        console.log('check c', c);
        c.forEach(function(c2){
            console.log('check c2', c2);
            strDraftValue.push(c2);
        });

    });

    await updateQuoteLines({IdToObjectForDraftValues : JSON.stringify(strDraftValue)})  //, IdToObjectForOriginalValues: strOriginalKeyValues
        .then(result => {
            const toast = new ShowToastEvent({
                title: 'Success',
                message: 'Records Updated Successfully!!',
                variant: 'success'
            });
            this.dispatchEvent(toast);
            this.actionTypeForWire = 'update';

            return refreshApex(this.wiredDataResult).then(() => {
                console.log('after refresh apex')
                this.fldsItemValues = [];
            });
        })
        .catch(error => {
            console.log('error');
            const toast = new ShowToastEvent({
                title: 'Error',
                message: 'An Error Occurred!!' + error.message,
                variant: 'error'
            });
            this.dispatchEvent(toast);
        }).finally(() => {
        console.log('inside final', this.fldsItemValues);
    });
}

APEX控制器

    @AuraEnabled(cacheable=true)
public static List<SBQQ__QuoteLine__c> getQuoteLinesList(Id quoteId, String actionType) {
    List<SBQQ__QuoteLine__c> qLineList = new List<SBQQ__QuoteLine__c>();

    if(quoteId != null){
        qLineList = [SELECT Id,Name,SBQQ__ProductName__c,SBQQ__Quantity__c,Reused_License_Key__c
                            FROM SBQQ__QuoteLine__c
                            WHERE SBQQ__RequiredBy__c != NULL
                            AND SBQQ__Quote__c = :quoteId
                            ORDER BY SBQQ__ProductName__c];
    }

    Boolean callLPUtility = true;

    for(SBQQ__QuoteLine__c ql : qLineList) {
        if(ql.Reused_License_Key__c != null) {
            callLPUtility = false;
            break;
        }
    }

    if(callLPUtility == true && actionType == 'begin') {
        LicenseProductUtility.toMapLicenseKeyForRenewal(qLineList, getSubscriptionsList(quoteId));
    }
    return qLineList;
}

@AuraEnabled
public static void updateQuoteLines(String IdToObjectForDraftValues) {  //String IdToObjectForOriginalValues

    List<Object> items = (List<Object>) JSON.deserializeUntyped(IdToObjectForDraftValues);
    List<SBQQ__QuoteLine__c> qlList = new List<SBQQ__QuoteLine__c>();
    List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();

    for(Object itemObj : items) {
        theJsonMapList.add((Map<String, Object>) itemObj);
    }

    for(Object o : theJsonMapList){

        SBQQ__QuoteLine__c q = new SBQQ__QuoteLine__c();
        q.Id = ((Map<String, Object>) o).get('Id').toString();
        q.Reused_License_Key__c = ((Map<String, Object>) o).get('Reused_License_Key__c').toString();
        qlList.add(q);
    }

    Savepoint sp = Database.setSavepoint();

    if(!qlList.isEmpty()){
        try {
            update qlList;
        } catch (Exception e) {
            Database.rollback(sp);
            System.debug('An exception occurred updateQuoteLines: ' + e.getMessage());
        }
    }
}

I have a button on the parent lwc. When called, it will call a method on the child lwc to perform data update via apex method. Once done, it calls refreshApex to refresh the data on the lightning datatable. RefreshApex will not trigger for some reason.

Now, if I use the child's save button to save the changes, refreshApex will trigger and data will update without any issues.

Appreciate your help!!!

enter image description here

PARENT COMPONENT

</div>
            <footer class="slds-modal__footer">
                <lightning-button label="Save" variant="brand" onclick={saveModal} class="slds-m-left_x-small"></lightning-button>
                <lightning-button label="Cancel" variant="neutral" onclick={closeModal} class="slds-m-left_x-small"></lightning-button>
            </footer>

PARENT JS

saveModal() {
    //firing a child method
    this.template.querySelector("c-reuse-license-key-table").testHandleAction();
}

CHILD COMPONENT

<template if:true={quotelinelist}>
        <div class="slds-grid_align-end" style="height: 500px; overflow: auto;">
            <lightning-datatable key-field="Id"
                                 data={quotelinelist}
                                 columns={columns}
                                 onsave= {testHandleAction}
                                 oncellchange= {handleRowAction}
                                 draft-values={fldsItemValues}
                                 hide-checkbox-column="true">  <!--suppress-bottom-bar="true"-->
            </lightning-datatable>
        </div>
    </template>

CHILD JS

@wire(getQuoteLinesList,{quoteId: '$parentRecordId', actionType: '$actionTypeForWire'})
cons(result) {
    console.log('check result 1 ', result)
    this.wiredDataResult = result;
    if(result.error) {
        console.log('inside wire error')
        //this.quotelineList = undefined;
    }
    else {
        console.log('inside else error')
        this.quotelinelist = result.data;
    }
}

handleRowAction(event) {
    lKeyDraftList.push(event.detail.draftValues);
}

@api async testHandleAction() {

    let strOriginalKeyValues = JSON.stringify(qlInList);
    let strlKeyDraftList = JSON.stringify(lKeyDraftList);
    let strDraftValue = [];

    lKeyDraftList.forEach(function(c){
        console.log('check c', c);
        c.forEach(function(c2){
            console.log('check c2', c2);
            strDraftValue.push(c2);
        });

    });

    await updateQuoteLines({IdToObjectForDraftValues : JSON.stringify(strDraftValue)})  //, IdToObjectForOriginalValues: strOriginalKeyValues
        .then(result => {
            const toast = new ShowToastEvent({
                title: 'Success',
                message: 'Records Updated Successfully!!',
                variant: 'success'
            });
            this.dispatchEvent(toast);
            this.actionTypeForWire = 'update';

            return refreshApex(this.wiredDataResult).then(() => {
                console.log('after refresh apex')
                this.fldsItemValues = [];
            });
        })
        .catch(error => {
            console.log('error');
            const toast = new ShowToastEvent({
                title: 'Error',
                message: 'An Error Occurred!!' + error.message,
                variant: 'error'
            });
            this.dispatchEvent(toast);
        }).finally(() => {
        console.log('inside final', this.fldsItemValues);
    });
}

APEX Controller

    @AuraEnabled(cacheable=true)
public static List<SBQQ__QuoteLine__c> getQuoteLinesList(Id quoteId, String actionType) {
    List<SBQQ__QuoteLine__c> qLineList = new List<SBQQ__QuoteLine__c>();

    if(quoteId != null){
        qLineList = [SELECT Id,Name,SBQQ__ProductName__c,SBQQ__Quantity__c,Reused_License_Key__c
                            FROM SBQQ__QuoteLine__c
                            WHERE SBQQ__RequiredBy__c != NULL
                            AND SBQQ__Quote__c = :quoteId
                            ORDER BY SBQQ__ProductName__c];
    }

    Boolean callLPUtility = true;

    for(SBQQ__QuoteLine__c ql : qLineList) {
        if(ql.Reused_License_Key__c != null) {
            callLPUtility = false;
            break;
        }
    }

    if(callLPUtility == true && actionType == 'begin') {
        LicenseProductUtility.toMapLicenseKeyForRenewal(qLineList, getSubscriptionsList(quoteId));
    }
    return qLineList;
}

@AuraEnabled
public static void updateQuoteLines(String IdToObjectForDraftValues) {  //String IdToObjectForOriginalValues

    List<Object> items = (List<Object>) JSON.deserializeUntyped(IdToObjectForDraftValues);
    List<SBQQ__QuoteLine__c> qlList = new List<SBQQ__QuoteLine__c>();
    List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();

    for(Object itemObj : items) {
        theJsonMapList.add((Map<String, Object>) itemObj);
    }

    for(Object o : theJsonMapList){

        SBQQ__QuoteLine__c q = new SBQQ__QuoteLine__c();
        q.Id = ((Map<String, Object>) o).get('Id').toString();
        q.Reused_License_Key__c = ((Map<String, Object>) o).get('Reused_License_Key__c').toString();
        qlList.add(q);
    }

    Savepoint sp = Database.setSavepoint();

    if(!qlList.isEmpty()){
        try {
            update qlList;
        } catch (Exception e) {
            Database.rollback(sp);
            System.debug('An exception occurred updateQuoteLines: ' + e.getMessage());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文