You need to sign in to do that
Don't have an account?
Nishchal Vashisht 1
handelsave is called on oncellchange event in datatable. My Question is why i am getting error on this line 'this.contactObj[row]=finall;'.
Actually I have created a datatable with inline edit & I am updating the childobject each time user update any cell.
Here's my datatable. I didn't add full code in JS I have mentioned my Line where I am getting my error.
Why JSON Object is not updating
import { LightningElement, api, track, wire } from 'lwc'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import contactDetails from '@salesforce/apex/GetContact.getContacts'; import addContactRow from '@salesforce/apex/GetContact.addContactRow'; import deleteContactRow from '@salesforce/apex/GetContact.deleteContactRow'; const columns = [ { label: 'Delete', type: 'button-icon', typeAttributes: { iconName: 'utility:delete', name: 'Delete', variant: 'brand', } }, { label: 'FirstName', fieldName: 'FirstName', editable: true, }, { label: 'LastName', fieldName: 'LastName', editable: true, }, { label: 'Email', fieldName: 'Email', type: 'email', editable: true, }, { label: 'Phone', fieldName: 'Phone', type: 'Phone', editable: true, }, ]; export default class EOperation extends LightningElement { data = []; columns = columns; step = '1'; @api recordId; boolvalue = true; emaildata = ''; subject = ''; body = ''; hasError = false; error; contactObj; fldsItemValues = []; temp = []; secondContact @wire(contactDetails, { accId: '$recordId' }) wiredContacts({ error, data }) { if (data) { console.log('Temp Data is ' + data) console.log('Stringify data ' + JSON.stringify(data)) this.contactObj = data; this.secondContact = data; this.error = undefined; } else if (error) { console.log('error is ' + error) this.error = error; this.contactObj = undefined; } } handleSave(event) { console.log('draft valus ID ' + JSON.stringify(event.detail.draftValues)); console.log(' type are ' + typeof (event.detail.draftValues) + ' IS Array ' + Array.isArray(event.detail.draftValues)); this.temp.push(event.detail.draftValues); console.log('Temp 0 ' + JSON.stringify(this.temp['id'])) let row = JSON.stringify(event.detail.draftValues).split(',')[1]; row = row.substring(row.indexOf('-') + 1, row.indexOf('}') - 1) console.log('row is ' + row) let field = JSON.stringify(event.detail.draftValues).split(',')[0] + '}'; console.log('ffffis ' + field) let fieldValue = field.substring(field.indexOf(':') + 2, field.indexOf('}') - 1); console.log('fieldvalue is ' + fieldValue) let queryString = '' + field.substring(field.indexOf('{') + 1, field.indexOf('}')) + ''; console.log('queryString ' + typeof (queryString)) field = field.substring(field.indexOf('"') + 1, field.indexOf(':') - 1); console.log('field is sss ' + field) let val = this.contactObj[row][field] let rs = `"${field}":"${val}"` console.log('rs is ' + typeof (rs)) console.log(' IS Array ' + Array.isArray(this.contactObj)) //let ts1 = this.contactObj.splice(row,1) console.log(JSON.stringify(this.contactObj[row]).replace(rs, queryString)) console.log('tss is ') let finall = JSON.parse(JSON.stringify(this.secondContact[row]).replace(rs, queryString)); // this.contactObj[row] =JSON.parse(JSON.stringify(this.contactObj[row]).replace(rs,queryString)) console.log(typeof (finall) + ' >>colomn are ' + typeof (this.secondContact[row])) this.contactObj[row]=finall; //this.secondContact[row]= '[{"Id":"0035j00000MM49HAAT","FirstName":"Alok","LastName":"Thirdss","Phone":"44564754654","Email":"bcd@gmail.com"}]' // console.log(ts.replace(rs,queryString)) //console.log(this.contactObj[row]) //console.log(this.contactObj[row][field]) // let temp2 = // this.contactObj[row][field] = fieldValue; } }
handelsave is called on oncellchange event in datatable. My Question is why i am getting error on this line 'this.contactObj[row]=finall;'.
Actually I have created a datatable with inline edit & I am updating the childobject each time user update any cell.
Here's my datatable. I didn't add full code in JS I have mentioned my Line where I am getting my error.
<template> <div style="height: 300px;"> <lightning-card title="Contact Details"> <lightning-button-icon icon-name="utility:adduser" variant="brand" alternative-text="Add user" class="slds-m-left_xx-small " slot="actions" title="AddUser" onclick={addUser}> </lightning-button-icon> <div> <lightning-datatable key-field="id" data={contactObj} suppress-bottom-bar hide-checkbox-column="true" onrowaction={handleRowActions} oncellchange={handleSave} draft-values={fldsItemValues} columns={columns}> </lightning-datatable> </div> <p slot="footer"> <lightning-button-icon icon-name="utility:save" variant="brand" alternative-text="Save" class="slds-m-left_xx-small slds-float_right" title="Save" onclick={saveRecord}> </lightning-button-icon> </p> </lightning-card> </div> </template>