Custom datatype in LWC datatable, Customizing the Quantity field as a custom input data type and the Price field for inline editing.
When attempting to change the Quantity value, the value is captured, but I also need to capture the row/record ID and the field name. How can this be achieved?"
Also when I change price field both onchange and handleCellChange method is calling.
Below is code snippet
├──customInput.html
├──CustomTypeGlobal.html
├──CustomTypeGlobal.js
└──CustomTypeGlobal.js-meta.xml
customInput.html
<template>
<lightning-input name="quantity" type="number" value={typeAttributes.value} onchange={typeAttributes.methodquantity}
variant="label-hidden" class="slds-input" data-id={rowId}></lightning-input>
</template>
CustomTypeGlobal.html
<template>
</template>
CustomTypeGlobal.js
import LightningDatatable from 'lightning/datatable';
import customInput from './customInput.html'
export default class CustomTypeGlobal extends LightningDatatable {
static customTypes = {
quantityInput: {
template: customInput,
standardCellLayout: true,
typeAttributes: ['label', 'value', 'methodquantity'],
}
}
}
accountDatatable.js
// accountDatatable.js
import { LightningElement, track } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
const columns = [
{ label: 'Account Name', fieldName: 'Name' },
{ label: 'Phone', fieldName: 'Phone' },
{ label: 'Industry', fieldName: 'Industry' },
{
label: 'Quantity', hideDefaultActions: true, type: 'quantityInput',
typeAttributes: {
value: { fieldName: 'Quantity_one__c' },
recid: { fieldName: 'Id' },
methodquantity: { fieldName: 'methodquantity' }
}
},
{ label: 'Price', fieldName: 'Price__c', editable: true }
];
export default class AccountDatatable extends LightningElement {
@track data = [];
@track error;
columns = columns;
connectedCallback() {
this.fetchAccounts();
}
fetchAccounts() {
getAccounts()
.then(result => {
this.data = result;
this.error = undefined;
console.log(' this.data -->' + JSON.stringify(this.data));
})
.catch(error => {
this.error = error;
this.data = undefined;
});
}
handleCellChange(event) {
console.log('handleCellChange -->' + event.detail);
}
handleSave() {
// Collect the draft values from the datatable
const draftValues = this.template.querySelector('lightning-datatable').draftValues;
if (draftValues.length > 0) {
}
}
handleChange(event) {
console.log('Value -->' + event.detail.value);
console.log('event.detail -->' + JSON.stringify(event.detail));
}
}
accountDatatable.html
<template>
<lightning-card title="Account List">
<!-- <lightning-datatable key-field="Id" data={data} columns={columns} hide-checkbox-column="true"
oncellchange={handleCellChange} draft-values={draftValues}>
</lightning-datatable> -->
<c-custom-type-global key-field="Id" data={data} columns={columns} hide-checkbox-column="true"
oncellchange={handleCellChange} draft-values={draftValues} onchange={handleChange}>
</c-custom-type-global>
</lightning-card>
<lightning-button label="Save" onclick={handleSave} class="slds-m-top_medium">
</lightning-button>
</template>