1

更新记录后,我无法刷新 Lightning Web 组件中的数据表。我onclick在行内的按钮上调用一个操作,并强制调用 Apex 方法来更新该记录。然后我调用 refreshApex() 来更新输入数据表的数据。

但是,在 之后refreshApex(),for:each 中的表不会被新数据刷新。

记录被正确修改并在刷新整个页面时正确反映更改。

注意: LWC 中不支持 Task 对象,我无法使用该updateRecord()方法更新这些记录。

HTML:

<template>
  <template if:true="{taskCompWrapperList}">
    <!--<lightning-layout multiple-rows="false" pull-to-boundary="small">-->
    <template for:each="{taskCompWrapperList}" for:item="taskTemplate">
      <lightning-layout-item
        key="{taskTemplate.taskSectionOrder}"
        size="3"
        class="slds-p-around_x-small"
      >
        <!-- Start bear tile -->
        <lightning-card title="{taskTemplate.taskSectionTitle}">
          <div class="slds-m-around_medium">
            <template if:true="{taskTemplate.taskList}">
              <lightning-datatable
                key-field="Id"
                data="{taskTemplate.taskList}"
                onrowaction="{handleRowAction}"
                columns="{columns}"
                onsave="{handleSave}"
                draft-values="{draftValues}"
              >
              </lightning-datatable>
            </template>
            <template if:true="{contact.error}">
              <!-- handle Apex error -->
            </template>
          </div>
        </lightning-card>
        <!-- End bear tile -->
      </lightning-layout-item>
    </template>
    <!--</lightning-layout>-->
  </template>
</template>

Javascript:

import { LightningElement, api, wire ,track} from 'lwc';
import getTaskCompWrappers from '@salesforce/apex/ENT_Task_Utility.getTaskComponentWrapper';
import updateTask from '@salesforce/apex/ENT_Task_Utility.updateTask';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';

const COLS = [
    {        
        type: 'button',
        label: 'Complete',
        typeAttributes: 
        {
            //iconName: 'action:preview',
            label: 'Complete',
            name: 'Complete',
            title: 'Complete',
            value: 'Complete',
            variant: 'brand',
            alternativeText: 'Complete'
        }
    },
    {        
        type: 'button-icon', 
        label: 'Start',       
        typeAttributes: 
        {
            iconName: 'action:approval',
            //label: 'Complete',
            name: 'Start',
            title: 'Start',
            value: 'Start',
            variant: 'success',            
            alternativeText: 'Start',
        }
    },
    {
        type: "button", 
        typeAttributes: 
        {  
            label: 'View',  
            name: 'View',  
            title: 'View',  
            disabled: false,  
            value: 'view',  
            iconPosition: 'left'  
        }
    },  
    {
        type: "button", 
        typeAttributes: 
        {  
            label: 'Edit',  
            name: 'Edit',  
            title: 'Edit',  
            disabled: false,  
            value: 'edit',  
            iconPosition: 'left'  
        }
    },  
    //{ label: 'Complete', fieldName: 'Task_Complete__c', editable: true },
    { label: 'Status', fieldName: 'Status', type: 'picklist', editable: true },
    { label: 'Completed', fieldName: 'Completed', type: 'boolean', editable: true },
    { label: 'Owner', fieldName: 'OwnerId', editable: true },
    { label: 'Subject', fieldName: 'Subject' },
    { label: 'Due Date', fieldName: 'ActivityDate', type: 'date' }
];

export default class ENT_Task_Utility_LWC extends LightningElement {
    @api objApiName;
    @api recordId;
    @track testMessage = 'Test Failed :c';  

    @track error;
    @track columns = COLS;
    @track draftValues = [];
    taskCompWrapperList;
    @track error;

    //@wire(getTasks, {recordId: '$recordId'}) taskList;`
    @wire(getTaskCompWrappers, {recordId: '$recordId', objApiName: '$objApiName'}) 
    taskCompWrapperListWire({ error, data }) {
        if (data) {
            this.taskCompWrapperList = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.taskCompWrapperList = undefined;
        }
    }

    updateTaskValues (taskId, taskStatus) {
        // eslint-disable-next-line no-console
        console.log('updateTaskValues hit');
        for(var counter = 0; counter < this.taskCompWrapperList.length; counter++) {
            // eslint-disable-next-line no-console
            console.log('taskWrapper: ' + this.taskCompWrapperList[counter]);
            for(var counter2 = 0; counter2 < this.taskCompWrapperList[counter].taskList.length; counter2++) {
                // eslint-disable-next-line no-console
                console.log('task: ' + this.taskCompWrapperList[counter].taskList[counter2]);
                if(this.taskCompWrapperList[counter].taskList[counter2].Id == taskId)
                {
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Task Id Found!',
                            message: this.taskCompWrapperList[counter].taskList[counter2].Id,
                            variant: 'success'
                        })
                    );
                    this.taskCompWrapperList[counter].taskList[counter2].Status = taskStatus;
                }
            }
        }
    }

    handleRowAction(event) {
        //TODO
    }    
}

顶点方法:

@AuraEnabled(cacheable=true)
    global static List<Task> getTasks(String recordId)
    {
        return [SELECT Id, Subject, OwnerId FROM Task WHERE WhatId = :recordId];
    }

    @AuraEnabled(cacheable=true)
    global static List<ENT_Task_Comp_Wrapper> getTaskComponentWrapper(String recordId, String objApiName)
    {
        List<Task_Template__c> taskTemplateList = [SELECT Id, Task_Component_Section_Order__c, Task_Component_Section_Title__c, (SELECT Id FROM Task_Template_Items__r) 
                                                   FROM Task_Template__c 
                                                   WHERE Active__c = true AND sObject__c = :objApiName ORDER BY Task_Component_Section_Order__c ASC];

        List<Task> taskList = [SELECT Id, Task_Template_Item__c, OwnerId, Owner.Name, Subject, Description, Status, ActivityDate, Task_Complete__c FROM TasK WHERE WhatId = :recordId];

        List<ENT_Task_Comp_Wrapper> taskCompWrapperList = new List<ENT_Task_Comp_Wrapper>();

        for(Task_Template__c taskTemplate : taskTemplateList)
        {
            ENT_Task_Comp_Wrapper taskCompWrapper = new ENT_Task_Comp_Wrapper();
            taskCompWrapper.taskSectionTitle = taskTemplate.Task_Component_Section_Title__c;
            taskCompWrapper.taskSectionOrder = (Integer)taskTemplate.Task_Component_Section_Order__c;
            taskCompWrapper.taskList = new List<Task>();

            for(Task currentTask : taskList)
            {
                for(Task_Template_Item__c taskTemplateItem : taskTemplate.Task_Template_Items__r)
                {
                    if(taskTemplateItem.Id == currentTask.Task_Template_Item__c)
                    {
                        taskCompWrapper.taskList.add(currentTask);
                    }
                }
            }

            taskCompWrapperList.add(taskCompWrapper);

        }
        System.debug(taskCompWrapperList);
        return taskCompWrapperList;
    }

    @AuraEnabled
    global static void updateTask(String taskId, String newStatus)
    {
        System.debug(taskId);
        Task taskToUpdate = new Task(Id = taskId, Status = newStatus);
        update taskToUpdate;
        //update taskToUpdate;
    }

@AuraEnabled
    global static void updateTask(String taskId, String newStatus)
    {
        System.debug(taskId);
        Task taskToUpdate = new Task(Id = taskId, Status = newStatus);
        update taskToUpdate;
        //update taskToUpdate;
    }
4

3 回答 3

0

您可能需要等待下一个版本有正确的方法来处理这种情况。

如果我是正确的,您将通过 uiRecordApi 获取记录并通过 Apex 进行更新。然后你需要使用getRecordNotifyChange()可用的Winter 21 release

于 2020-09-16T09:42:11.137 回答
0

在您 refreshApex 使用此行导入的JS 代码中import { refreshApex } from '@salesforce/apex';

但是您没有分配给任何有线方法。因此数据不会刷新

请参阅本文档

要刷新连线方法,请将连线方法接收的参数(即连线值)传递给refreshApex(). 在此示例代码中,连线方法是taskCompWrapperListWire. 保持有线服务提供的值并将其传递给 refreshApex()。

@wire(getTaskCompWrappers, {recordId: '$recordId', objApiName: '$objApiName'}) 
taskCompWrapperListWire({ error, data }) {
    if (data) {
        this.taskCompWrapperList = data;
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.taskCompWrapperList = undefined;
    }
}

然后使用refreshApex()如下:

refreshApex(this.taskCompWrapperListWire);

更新您的代码如下

updateTaskValues({
        taskId: this.taskId,
        taskStatus: this. taskStatus
   })
    .then(() => {
        // your code logic 
        refreshApex(this.taskCompWrapperListWire);
    })
    .catch((error) => {
        this.message = 'Error received: code' + error.errorCode + ', ' +
            'message ' + error.body.message;
    });
于 2020-03-20T19:38:13.250 回答
0

除了 Sudarshan 提供的答案之外,您还应该将 taskCompWrapperList 定义为反应性属性,以使其在属性更新时重新呈现。

@track taskCompWrapperList = [];
于 2021-01-03T10:03:20.213 回答