简体   繁体   中英

Handlebars.js each helper with if condition

I am using the below helper to iterate through a JSON array and return a result based on the condition to find whether an Account is closed or not ( OpenOrClosedDesc=='Closed' ). I am getting all of the Accounts that are closed. But now I want to print an error message on the screen if there are no Closed Accounts.

Handlebars.registerHelper('each_Closed', function(list, opts) {
                var i, result = '';
                try {
                    //console.log("List Closed length "+ list.length)
                    for (i = 0; i < list.length; i++)
                        if (list[i].OpenOrClosedDesc == 'Closed'){
                          //  console.log("List Closed  "+ list[i].OpenOrClosedDesc == 'Closed')
                            result = result + opts.fn(list[i]);
                        }
                return result;
            }catch(e){

            }
        });

HTML Code :

<div id = "Revolving_ClosedAcc">
   {{#repData}}
     {{#each_Closed arf.TradeLine.TradeLine.[Revolving Accounts]}}
    .
    .
    .
    .
    .
    {{/each_Closed}}
  {{/repData}}
</div>

EDIT Explanation for possible duplicate: I tried to return the error message from an else branch but it did not give accurate results. It simply shows there are no closed accounts if it finds one of the accounts is closed. It doesn't go through the for loop. The for loop is the main path as I want to iterate through the whole object array.

EDIT for having count of each account:

Handlebars.registerHelper('each_Closed', function(list, opts) {
                var i, result = '',resCounter=0,closedAccountFound = false;
                try {
                    //console.log("List Closed length "+ list.length)
                    for (i = 0; i < list.length; i++) {
                        if (list[i].OpenOrClosedDesc == 'Closed') {
                            // console.log("List Closed  "+ list[i].OpenOrClosedDesc == 'Closed')
                            result = result + opts.fn(list[i]);
                            resCounter++;
                            closedAccountFound = true;
                        }
                    }
                    console.log(resCounter);
                    return closedAccountFound ? result : "No close account found.";
                }catch(e){

                }
            });

I am getting count in console.log(resCounter) but how can i return that or in other words how i can print that in handlebars ? Do i have to write another helper ?

How can i do that?

Thanks in advance.

Just add a variable closedAccountFound = false and assign it true when you found any closed account then before returning the result check the value if closedAccountFound is true then return you result else return the message.

Handlebars.registerHelper('each_Closed', function(list, opts) {
            var i, result = '', closedAccountFound = false;
            try {
                //console.log("List Closed length "+ list.length)
                for (i = 0; i < list.length; i++)
                    if (list[i].OpenOrClosedDesc == 'Closed'){
                      //  console.log("List Closed  "+ list[i].OpenOrClosedDesc == 'Closed')
                        result = result + opts.fn(list[i]);
                        closedAccountFound = true; 
                    }
            return closedAccountFound ? result : "No close account found.";
        }catch(e){

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM