Salesforce How To Refresh Page Data in Lightning Web Component(二)

2022-04-29 13:29:00 浏览数 (1)

删除处理同样可以利用refreshApex方法进行页面刷新

代码语言:javascript复制
handleDeleteClick() {
   deleteRecord(this.selectedRecord)
      .then(() => {
            refreshApex(this.wiredRecordList);
      })
      .catch(error => {
      })
 }

效果展示:

basicDatatable.html

代码语言:javascript复制
<template>
    <div style="background-color: white; border-bottom-style:double;">
        <lightning-layout multiple-rows="true" horizontal-align="center" vertical-align="center">
            <lightning-layout-item padding="around-small" size="5" style="position: absolute; left: 0%;">
                <span class="slds-var-p-right_x-small" style="font-size: 16px; font-weight: bold;">担当者</span>
                <span class="slds-var-p-right_x-small" style="font-size: 32px; font-weight: bold;">{recordLength}</span>
                <span class="slds-var-p-right_x-small" style="font-size: 16px; font-weight: bold;">件</span>
            </lightning-layout-item>
            <lightning-layout-item padding="around-small" size="2">
                <span>&nbsp;</span>
            </lightning-layout-item>
            
            <lightning-layout-item padding="around-small" size="5" style="position: absolute; left: 80%;">
                <a style="background-color: #333333; color: white; padding: 10px; display: inline;" onclick={refresh}>
                    <lightning-icon class="slds-button__icon
                    slds-icon-utility-down slds-icon_container forceIcon" icon-name="utility:refresh" size="x-small">
                    </lightning-icon>
                </a>
                <span>&nbsp;</span>
                <a style="background-color: #333333; color: white; padding: 10px; display: inline;" onclick={handleDeleteClick}>
                    <lightning-icon class="slds-button__icon
                    slds-icon-utility-down slds-icon_container forceIcon" icon-name="utility:delete" size="x-small">
                    </lightning-icon>
                </a>
            </lightning-layout-item>
        </lightning-layout>
    </div>
    <div style="height: 300px;">
        <lightning-datatable
                show-row-number-column
                max-row-selection="1"
                onrowselection={handelSelection}
                key-field="id"
                data={records}
                columns={columns}>
        </lightning-datatable>
    </div>
</template>

basicDatatable.js

代码语言:javascript复制
import { LightningElement, wire, track } from 'lwc';
import getContactListView from '@salesforce/apex/MC_ContactListViewController.getContactListView';
import { loadStyle } from 'lightning/platformResourceLoader';
import COMMON_STATIC from '@salesforce/resourceUrl/common_sfdc_css';
import { refreshApex } from '@salesforce/apex';
import { deleteRecord } from 'lightning/uiRecordApi';
const columns = [
    { label: 'Name', fieldName: 'name', type: 'text' },
    { label: 'Email', fieldName: 'email', type: 'text' },
    { label: 'Phone', fieldName: 'phone', type: 'text' },
    { label: 'OwnerName', fieldName: 'ownerName', type: 'text' },
    { label: 'Birthdate', fieldName: 'birthdate', type: 'date' },
];

export default class BasicDatatable extends LightningElement {
    columns = columns;
    @track refreshFlag = false;
    @track wiredRecordList = [];
    @track records;
    @track recordLength;
    @track selectedRecord;

    renderedCallback() {
        Promise.all([
            loadStyle(this, COMMON_STATIC   '/mcCommon.css')
        ]);
    }
    // eslint-disable-next-line @lwc/lwc/no-async-await
    async connectedCallback() {
    }

    @wire(getContactListView, {refreshFlag : '$refreshFlag'})
    wireRecords(result) {
        console.log('>>>>>result>>' JSON.stringify(result));
        this.refreshFlag = false;
        this.wiredRecordList = result;
        if (result.data) {
            this.records = result.data;
            this.recordLength = result.data.length;
        } else if (result.error) {
            // エラー
        }
    }
    // refresh
    refresh() {
        refreshApex(this.wiredRecordList);
    }
    handelSelection(event) {
        if (event.detail.selectedRows.length > 0) {
            this.selectedRecord = event.detail.selectedRows[0].id;
        }
    }
    handleDeleteClick() {
        deleteRecord(this.selectedRecord)
        .then(() => {
            refreshApex(this.wiredRecordList);
        })
        .catch(error => {
        })
    }
}

MC_ContactListViewController.cls

代码语言:javascript复制
public with sharing class MC_ContactListViewController {
    @AuraEnabled(cacheable=true)
    public static List<ContactWrapper> getContactListView(Boolean refreshFlag){
        List<ContactWrapper> wappers = new List<ContactWrapper>();
        List<Contact> resultList = [SELECT Id,Name,Email,Phone,Birthdate,Owner.Name
                        FROM Contact
                        WHERE OwnerId != :UserInfo.getUserId()
                        LIMIT 100];

        if (resultList!=null && resultList.size() > 0) {
            for (Contact con : resultList) {
                ContactWrapper wapper = new ContactWrapper();
                wapper.id = con.Id;
                wapper.name = con.Name;
                wapper.email = con.Email;
                wapper.phone = con.Phone;
                wapper.ownerName = con.Owner.Name;
                wapper.birthdate = con.Birthdate;
                wappers.add(wapper);
            }
        }
        return wappers;
    }

    public class ContactWrapper {
        @AuraEnabled
        public String id;
        @AuraEnabled
        public String idLink;
        @AuraEnabled
        public String name;
        @AuraEnabled
        public String email;
        @AuraEnabled
        public String ownerName;
        @AuraEnabled
        public String phone;
        @AuraEnabled
        public Date birthdate;
    }
}

0 人点赞