Home > Net >  How to get the previous index value in nested loop in handlebars?
How to get the previous index value in nested loop in handlebars?

Time:10-08

In HBS, I need to get the index value of an outer loop. Getting the current index was the only way I could find it. Please help me to fix this.


             {{#each orderList}}
                    {{#each this.orders}}
                        {{#each this.productDetails}}
                                <td>{{../this.date}}</td>
                                <td>{{this.name}}</td>
                                <td>{{this.price}}</td>
                                <td>{{this.quantity}}</td>
                                <td>{{../this.billingAddress.address}}</td>
                                <td>{{../this.billingAddress.contact}}</td>
                                <td>{{../this.paymentMode}}</td>
                                <td >
                                    {{#if this.status}}
                                        <select onchange="changeStatus({{@index}},{{@index}});" id="statusBox"> 
                                        <option value="Placed">Placed</option>
                                        <option value="Shipped">Shipped</option>
                                        <option value="Cancel">Cancel</option>
                                        </select>
                                    {{else}}
                                        Canceled
                                    {{/if}}
                                </td>
                                <td>{{this.status}}</td>
                            </tr>
                        {{/each}}
                    {{/each}}
                {{/each}}

CodePudding user response:

I got the answer. the correct way to get the index of the outer loop is as follows

@../index

CodePudding user response:

The corrected code is as follows

{{#each orderList}}
                    {{#each this.orders}}
                        {{#each this.productDetails}}
                                <td>{{../this.date}}</td>
                                <td>{{this.name}}</td>
                                <td>{{this.price}}</td>
                                <td>{{this.quantity}}</td>
                                <td>{{../this.billingAddress.address}}</td>
                                <td>{{../this.billingAddress.contact}}</td>
                                <td>{{../this.paymentMode}}</td>
                                <td >
                                    {{#if this.status}}
                                        <select onchange="changeStatus({{@index}},{{@../index}});" id="statusBox"> 
                                        <option value="Placed">Placed</option>
                                        <option value="Shipped">Shipped</option>
                                        <option value="Cancel">Cancel</option>
                                        </select>
                                    {{else}}
                                        Canceled
                                    {{/if}}
                                </td>
                                <td>{{this.status}}</td>
                            </tr>
                        {{/each}}
                    {{/each}}
                {{/each}}
  • Related