Sum up input field value inside iterative Table in LWC

arraylightning-web-components

I have an iterative table contains input field onchange event i need to sum up the value of the iterative input box ?

  <table aria-describedby="conatact-list" class="slds-table slds-table_cell-buffer slds-table_bordered ">
                <!--Header of the table-->
                <thead>
                    <tr class="slds-line-height_reset">
                        
                        <th class="" scope="col">
                            <div class="slds-truncate" title="Prdt">Prdt</div>
                        </th>
                        <th class="" scope="col">
                            <div class="slds-truncate" title="Quantity">Quantity</div>
                        </th>
                        
                       
                    </tr>
                </thead>

                <!--Body of the table-->
                <tbody>
                    <template for:each={PrdtList} for:item="item" for:index="indexVar">
                        <tr key={item.recId} class="slds-hint-parent">
                           
                           
                             <td class="slds-truncate">
                                <div>
                                    {item.ProdName}
                                </div>
                            </td> 
                            
                            <td class="slds-truncate">
                                <div>
                                    <input type="number"  data-index={indexVar}  data-target-id={item.Qty}  onchange={handleQtyChange}></input>
                       
                                </div>
                            </td> 
                            
                          
                        </tr>
                    </template>
                    
                </tbody>
            </table>

handleQtyChange(event)
  {
    const value = event.target.value;
   
   
      const TotalList = JSON.parse(JSON.stringify(this.PrdtList))
     
        this.totalVal=TotalList.reduce(function(accum, val) {
                        console.log('accum',accum)
                        return accum + Number(value)}, 0);
                    
  }

If suppose in the list contains two input fields then those two input fields needs to be added and store in totalVal.My approach is not working? what i have done wrong ? Please guide me.

Expected result:
if 1st input value in the list if i enter is 6
and 2nd input value in the list i enter 4 then totalVal become 6+4=10.

Best Answer

You have a small mistake in your markup:

<div>
    <input type="number"  data-id={item.recId} value={item.Qty}  onchange={handleQtyChange}></input>
</div>

Now, having the record Id, you can:

  handleQtyChange(event) {
    this.PrdtList.find(
      (record) => record.recId === event.target.dataset.id)
      .Qty = parseFloat(event.target.value)
    this.totalVal = this.PrdtList.reduce((p,v) => p + v.Qty, 0)
  }

Demo.