<template>
<lightning-card variant="Weekly Report Item" title="Weekly Report Item" icon-name="standard:event">
<lightning-datatable
key-field="id"
hide-checkbox-column
data={data}
columns={columns}
onsave={handleSave}>
</lightning-datatable>
</lightning-card>
</template>
import { LightningElement, wire, api, track } from "lwc";
import getWeeklyReportItems from "@salesforce/apex/WeeklyReportApprovalController.getWeeklyReportItems";
import updateWeeklyReportItems from '@salesforce/apex/WeeklyReportApprovalController.updateWeeklyReportItems'
const columns = [
{
label: "Item",
editable: false,
fieldName: "Id",
type: "url",
typeAttributes: { label: { fieldName: "Name" }, target: "_blank" }
},
{
label: "Description",
editable: false,
fieldName: "Description__c",
type: "Text"
},
{
label: "Date",
editable: false,
fieldName: "Date__c",
type: "Date"
},
{
label: "Spent Hours",
editable: false,
fieldName: "Spent_Hours__c",
type: "Number"
},
{
label: "Recognized Hours",
editable: true,
fieldName: "Recognized_Hours__c",
type: "Number"
},
{
label: "Comments",
editable: true,
fieldName: "Comments__c",
type: "Text"
}
];
export default class WeeklyReportApproval extends LightningElement {
@api recordId;
@track columns = columns;
data = [];
@track data;
@track saveDraftValues;
// connectedCallback() {
// this.getWeeklyReportItems().then(data => {
// console.log(data)
// })
// }
@wire(getWeeklyReportItems, { ApprovalId: "$recordId" })
wiredTbale({ data, error }) {
let result = [];
if (data) {
data.forEach((item) => {
let approvalData = new Object();
approvalData.Id = item.Id ? "/" + item.Id : "";
approvalData.Name = item.Name ? item.Name : "";
approvalData.Date__c = item.Date__c ? item.Date__c : "";
approvalData.Description__c = item.Description__c ? item.Description__c : "";
approvalData.Spent_Hours__c = item.Spent_Hours__c
? item.Spent_Hours__c
: "";
approvalData.Recognized_Hours__c = item.Recognized_Hours__c
? item.Recognized_Hours__c
: "";
approvalData.Comments__c = item.Comments__c ? item.Comments__c : "";
result.push(approvalData);
});
this.data = result;
}
}
handleSave(event) {
this.saveDraftValues = event.detail.draftValues;
this.saveDraftValues.map(item=>{
item.id=this.data[item.id.split('-')[1]].Id.replace('/','');
});
console.log('saveDraftValues=======>'+JSON.stringify(this.saveDraftValues));
updateWeeklyReportItems({weeklyReportItemData:JSON.stringify(this.saveDraftValues)}).then(res => {
console.log('成功');
}).catch(err => {
console.log('失败');
})
window.location.reload();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__HomePage</target>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>