0

I have a datatable LWC where I have implemented inline editing. It behaves like every field is required and displays "Complete this field." if I try and delete the contents of any field. These fields are not required in the Sobject.

Is there a way to change this behavior?

HTML

<template>
<template if:false={showSpinner}>
    <lightning-datatable
        data={mydata}
        columns={columns}
        key-field={keyField}
        sorted-by={sortedBy}
        sorted-direction={sortedDirection}
        max-row-selection={maxRowSelection}
        selected-rows={selectedRows}
        hide-checkbox-column={hideCheckboxColumn}
        suppress-bottom-bar={suppressBottomBar}
        onsort={updateColumnSorting}
        onsave={handleSave}
        oncancel={cancelChanges}
        onrowselection={handleRowSelection}
        onrowaction={handleRowAction}>
    </lightning-datatable>
</template>
<template if:true={showSpinner}>
    <lightning-spinner alternative-text="Loading" size="medium"></lightning-spinner>
</template> 

JS

    handleSave(event) {
    // Only used with inline editing
    const draftValues = event.detail.draftValues;

    // apply drafts to mydata
    let data = [...this.mydata];
    data = data.map(item => {
        const draft = draftValues.find(d => d[this.keyField] == item[this.keyField]);
        if (draft != undefined) {
            let fieldNames = Object.keys(draft);
            fieldNames.forEach(el => item[el] = draft[el]);
        }
        return item;
    });

    // apply drafts to outputData
    let odata = [...this.outputData];
    odata = odata.map(oitem => {
        const odraft = draftValues.find(d => d[this.keyField] == oitem[this.keyField]);
        if (odraft != undefined) {
            let ofieldNames = Object.keys(odraft);
            ofieldNames.forEach(el => oitem[el] = odraft[el]);
        }
        return oitem;
    });  

    this.outputEditedRows = [...odata]; // Set output attribute values
    console.log('this.outputEditedRows',this.outputEditedRows);
    this.savePreEditData = [...data];   // Resave the current table values
    this.mydata = [...data];            // Reset the current table values

    // Force clearing of the edit highlights
    this.columns = [...this.columns];
}

cancelChanges(event) {
    // Only used with inline editing
    this.mydata = [...this.savePreEditData];
}

Error when trying to clear any field:

  • 1
    Could you please share some code and add more details about what is tried and where you are facing issue 😀 – Abhijith E Mar 19 '20 at 19:09
  • Where are you setting the "columns" variable? – Craig Austgen Mar 20 '20 at 14:39
  • Columns are being set in Apex – Eric Smith Mar 20 '20 at 18:08
0

The reason I was having this issue is that in my column attributes, I had defined typeAttributes = ''

Changing this to typeAttributes = null no longer requires a field to have a value when editing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.