lwc inline edit

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值