0

您能否指导我如何使包装类对象在 LWC js 中具有反应性。

我相信@track 属性是反应性的,但是当我更改this.coreAttributesjs 对象中任何属性的值时,它不会反映在Save按钮单击中

Js、Apex、Html文件如下


    import { LightningElement, track, api } from "lwc";
    import queryCoreAttributes from "@salesforce/apex/P1PlantInfoPromptSolar.queryCoreAttributes";

    export default class P1PlantInfoPromptSolar extends LightningElement {
    @track coreAttributes;

    connectedCallback() {
        queryCoreAttributes()
        .then(result => {
          this.coreAttributes = JSON.parse(result);
        }) 
        .catch({

        });

        this.promptSpecificAttributes = {
          noOfBlocks:"",
          flatHierarchy:false,
          drivePlus:false
        };
      }
    saveP1PlantInfoPromptMetadataHandler(){
        console.log(' prompt specific att -> '+ JSON.stringify(this.coreAttributes));
      }
    }```
    -----------------------------------------------------------------------------------------
    **HTML file**
    ```<template for:each={coreAttributes} for:item="coreAttribute">
                  <tr key={coreAttribute.key}>
                    <th>{coreAttribute.attributeHeader}</th>
                    <td>
                      <template if:false={coreAttribute.isPicklist}>
                        <input type={coreAttribute.attributeDataType} name={coreAttribute.attributeHeader}
                         value={coreAttribute.attributeValue}/>
                      </template>
                      <template if:true={coreAttribute.isPicklist}>
                        <select size="1" name={coreAttribute.attributeHeader}>
                          <option value="None">--None--</option>
                          <template for:each={coreAttribute.picklistValues} for:item="attributePickValues">
                            <option key={coreAttribute.key} value={coreAttribute.attributeValue}>{attributePickValues}</option>
                          </template>
                        </select>
                      </template>
                    </td>
                  </tr>
    <lightning-button
            class="slds-m-left_small"
            variant="brand"
            label="Save"
            title="Save"
            onclick={saveP1PlantInfoPromptMetadataHandler}
          ></lightning-button>
                </template>```
    ---------------------------------------------------------------------------------------------------------
    Apex class
    ```public with sharing class P1PlantInfoPromptSolar {

    @AuraEnabled(cacheable=true)
        public static String queryCoreAttributes() {
            List<Core_Attribute__c> coreAttributesList = new List<Core_Attribute__c>();
            Map<Id,Set<String>> picklistValuesToPicklistMasterMap = new Map<Id,Set<String>>();

            coreAttributesList = [SELECT Id, Name, Data_Type__c, Type__c, Picklist_Master__c 
                FROM Core_Attribute__c WHERE Base_Asset_Name__c IN (SELECT Id FROM Base_Asset_Template__c WHERE Name = 'Base PV Plant') 
                ORDER BY Name ASC];

            picklistValuesToPicklistMasterMap = Utils.getPicklistValues(); 

            System.debug(' picklistValuesToPicklistMasterMap '+ picklistValuesToPicklistMasterMap);

            List<coreAttributesWrapper> coreAttributesWrapperList = new List<coreAttributesWrapper>();
            for(Core_Attribute__c coreAttribute : coreAttributesList){
                coreAttributesWrapper coreAttWrapper = new coreAttributesWrapper();
                coreAttWrapper.attributeHeader = coreAttribute.Name;
                coreAttWrapper.attributeDataType = coreAttribute.Data_Type__c.toLowerCase();
                coreAttWrapper.picklistValues = (coreAttribute.Data_Type__c == 'Picklist') ? picklistValuesToPicklistMasterMap.get(coreAttribute.Picklist_Master__c): null;
                coreAttWrapper.isPicklist = (coreAttribute.Data_Type__c == 'Picklist');
                coreAttWrapper.attributeValue = '';
                coreAttributesWrapperList.add(coreAttWrapper);
            }
            System.debug(' core Att '+ coreAttributesWrapperList);
            return JSON.serialize(coreAttributesWrapperList);
        }

        public class coreAttributesWrapper{
            public String attributeHeader;
            public String attributeDataType;
            public Set<String> picklistValues;
            public boolean isPicklist;
            public String attributeValue;
        }
    }```


4

0 回答 0