function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shuhbam SinhaShuhbam Sinha 

How to update specific filed from a table in lwc

Hello Everyone,
I have created a custom table which has multiple rows and this table consists of two lightning date input fields where user can enter the date. Now my requirement is if a user enters one date input field other date-input field should automatically get updated by adding 6 months . Issue is I am able to update it automatically but it is actually updating all the row's field . I want only corresponding row's field should be updated .Let say I have 5 rows and user enters the date on the first row then only first row's other date input field should be updated not all the rows. I hope my problem is clear. I am not sure how to segregate the rows with user input. If anyone has any idea ,please help me on this.
Thanks in advance. 

AshwiniAshwini (Salesforce Developers) 
Hi Shubam,
Can you share your code here?
Shuhbam SinhaShuhbam Sinha
Hi Ashwini,
Please find the code below: -
 
<!----Html--->           
 <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
                <div class="slds-modal__container widthIncrease">
                    <header class="slds-modal__header">
                        <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick={hideModalBoxAccount}>
                            <lightning-icon icon-name="utility:close"
                                alternative-text="close"
                                class='grayutilityicon'
                                variant="inverse"
                                size="small" ></lightning-icon>
                            <span class="slds-assistive-text">Close</span>
                        </button>
                        <h2 id="modal-heading-accountAggregrator" class="slds-text-heading_medium slds-hyphenate">Account Aggregrator Document List</h2>
                    </header>
                    <div class="slds-modal__content slds-p-around_medium" id="mocontent-id-2">
                        <div class="slds-scrollable slds-m-around_medium">
                            <lightning-card title="Create Financial Details">
                                   <template for:each={accounts} for:item="acc" for:index="index">
                                       <tr key={acc.Id}>
                                           <td>
                                                <lightning-input  data-id={acc.Id} type="checkbox" onchange={handleChecked} ></lightning-input>
                                           </td>
                                           <td>
                                                <div class="slds-cell-wrap" style = "width:150px" title={acc.Title}> {acc.Title} </div> 
                                           </td>
                                           <td> <div class="slds-cell-wrap" style = "width:140px">
                                            <!--- <lightning-input type="date" name="input1"  onchange={handleChangeDate} required></lightning-input>-->
                                                <lightning-record-edit-form object-api-name="Financial_Details__c">
                                                    <lightning-input-field 
                                                        field-name="Year_Months_From__c"
                                                        variant= "label-hidden"
                                                        data-id={acc.Id} 
                                                        required
                                                        onchange={handleDateFrom}> 
                                                    </lightning-input-field>
                                                </lightning-record-edit-form> 
                                                </div>
                                           </td>
                                           <td><div class="slds-cell-wrap" style = "width:140px">
                                           <!---  <lightning-input type="date" name="input2"  onchange={handleChangeDateTo} required></lightning-input> -->
                                                <lightning-record-edit-form object-api-name="Financial_Details__c">
                                                    <lightning-input-field 
                                                        field-name="Year_Months_To__c"
                                                        variant= "label-hidden"
                                                        data-id={acc.Id}
                                                        value={yearsMonthsTo}
                                                        onchange={handleDateTo}> 
                                                        required> 
                                                    </lightning-input-field>
                                                </lightning-record-edit-form> 
                                                </div>
                                           </td>
                                       </tr>
                                   </template>
                            </lightning-card>
                       </div>  
                    </div>
                    <footer class="slds-modal__footer" style="display: block !important;">
                        <template if:true={showSaveButton}>
                            <button class="slds-button slds-button_neutral" onclick={saveRecord}>Save</button>
                        </template>
                        <button class="slds-button slds-button_neutral" onclick={hideModalBoxAccount}>Cancel</button>
                    </footer>
                </div>
            </section>
 
handleDateFrom(event) {
    let currentDate = new Date().toJSON().slice(0, 10);
    //console.log('todays data',currentDate);   
    this.dateFrom = event.detail.value;
    //const monthFrom = new Date(JSON.stringify(this.dateFrom)).toJSON().slice(0, 10);
    //console.log('InputFormat',monthFrom);
    console.log('dateFrom',JSON.stringify(this.dateFrom));
   // console.log('dateFrom',event.target.dataset.id);
    this.listDateFrom.push({Id : event.target.dataset.id ,DateFrom : JSON.stringify(this.dateFrom)});
    console.log( "newdateFrom",this.listDateFrom);
    // Months Logic Starts----------------------
    const monthFromTest = new Date(JSON.stringify(this.dateFrom));
    const monthToTest = new Date();
    let monthDiffTest = monthToTest.getMonth() - monthFromTest.getMonth();
    let yearDiffTest = monthToTest.getYear() - monthFromTest.getYear();
    let differenceMonthTest = monthDiffTest + yearDiffTest * 12;
    console.log('monthDiff ', differenceMonthTest);
  //  const newDate = this.subtract6Months(monthFromTest);
   // console.log('date sixdiff',newDate.toJSON().slice(0, 10)); 

   //this.yearsMonthsTo = newDate.toJSON().slice(0, 10);
    var inputDate = monthFromTest.toJSON().slice(0, 10);
    var todayDate = monthToTest.toJSON().slice(0, 10);
    console.log('inputDate ', inputDate);
    console.log('todayDate ', todayDate);
    if(inputDate > todayDate){
        this.showErrors = true;
        this.errorMessagess = 'Year Months From Date cannot be future date';
        this.showSaveButton = false
    }
    else{
        this.showErrors = false;
        this.showSaveButton = true;

    } 
    /*else if(monthFrom > monthTo){
        this.showError = true;
        this.errorMessages = 'Year Months From Date cannot be greater than Year Months To Date';
    }else if(differenceMonth != fields.Duration__c){
        this.showError = true;
        this.errorMessages = 'Please select Year Months From Date '+fields.Duration__c + ' months back from Year Months To Date';
    }*/
    var key = event.target.dataset.field;
   // console.log('target.name ', key);
   /* for (let i = 0; i < this.accounts.length; i++) {
        if (event.target.dataset.id == this.accounts[i].Id) {
            console.log('specififc',newDate.toJSON().slice(0, 10)); 
            this.yearsMonthsTo[i].Id = newDate.toJSON().slice(0, 10);
        }
    } */
    
}
handleDateTo(event){
    this.dateTo = event.detail.value;
    console.log('dateTo',JSON.stringify(this.dateTo));
    this.listDateTo.push({Id : event.target.dataset.id ,DateTo : JSON.stringify(this.dateTo)});
    console.log( "newdateto",this.listDateTo);
    const monthFromTo = new Date(JSON.stringify(this.dateTo));
    const monthToDay = new Date();
    let monthDiffTo = monthToDay.getMonth() - monthFromTo.getMonth();
    let yearDiffTo = monthToDay.getYear() - monthFromTo.getYear();
    let differenceMonthTest = monthDiffTo + yearDiffTo * 12;

    var inputDate = monthFromTo.toJSON().slice(0, 10);
    var todayDate = monthToDay.toJSON().slice(0, 10);
    console.log('inputDate ', inputDate);
    console.log('todayDate ', todayDate);
    if(inputDate > todayDate){
        this.showErrorTo = true;
        this.errorMessagesTo = 'Year Months To Date cannot be future date';
        this.showSaveButton = false
    }
    else{
        this.showErrorTo = false;
        this.showSaveButton = true;

    } 
}