LWC-创建新记录后,如何刷新闪电数据表的数据?

时间:2019-06-27 18:13:49

标签: salesforce salesforce-lightning

我正在使用有线服务以及apex控制器来初始获取数据。

像这样:@wire (fetchAccounts)parameters

我希望能够以如下方式在数据表上查看新创建的帐户

    <lightning-datatable 
       key-field="id"
       data={parameters.data}
       onrowaction={handleRowAction}
       row-number-offset={rowOffset}
       hide-checkbox-column="true"
       columns={columns}>
    </lightning-datatable>

能否请您就正确的方法给我建议?

2 个答案:

答案 0 :(得分:1)

为此,可以在以下位置找到文档:https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex#data_apex__refresh_cache

所以看起来像这样:


// add this import statement
import { refreshApex } from '@salesforce/apex';


    @track parameters;
    @track error;

    /** Wired Apex result so it can be refreshed programmatically */
    _wiredResult;

    @wire(fetchAccounts)
    wiredCallback(result) {
        this._wiredResult = result;
        if (result.data) {
            this.parameters = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error;
            this.parameters = undefined;
        }
    }

    // in order to refresh your data, execute this function:
    refreshData() {
        return refreshApex(this._wiredResult);
    }

并将您的HTML更改为:


<lightning-datatable 
   key-field="id"
   data={parameters}
   onrowaction={handleRowAction}
   row-number-offset={rowOffset}
   hide-checkbox-column="true"
   columns={columns}>
</lightning-datatable>


如果您想阅读一个不错的示例,可以在这里找到:https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/default/lwc/ldsDeleteRecord/ldsDeleteRecord.js

答案 1 :(得分:0)

您需要在服务器调用之后调用refreshApex(在.then()回调内部) 这是如何在更改数据库内容后如何实现html数据刷新的工作示例:

import { LightningElement, wire, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import findSomething from '@salesforce/apex/ApexController.findMethod';
import changeSomething from '@salesforce/apex/ApexController.changeMethod';
import {refreshApex} from '@salesforce/apex';

findSomething
changeSomething
@track paramForChangeOne
@track paramForChangeTwo

@wire(findSomething, { paramOne: '$paramOne', paramTwo: '$paramTwo' }) findSomething;

callFromHtml(){
    changeSomething({paramForChangeOne: this.paramForChangeOne, paramForChangeTwo: this.paramForChangeTwo})
    .then(result => {
        if(result){
            this.showNotification('success', 'Success Message', '');
        }
        // refreshing table data using refresh apex(HERE NEEDS TO BE SET)
        refreshApex(this.findSomething);
    })
    .catch(error => {
        console.log(error);
    });
}