简体   繁体   English

Handlebars.js每个辅助条件为if

[英]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' ). 我正在使用下面的助手来遍历JSON数组,并根据条件返回结果以查找是否关闭帐户( 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 : HTML代码:

<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. 编辑可能重复的说明:我试图从else分支返回错误消息,但未给出准确的结果。 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. 它不会通过for循环。 The for loop is the main path as I want to iterate through the whole object array. for循环是我要遍历整个对象数组的主要路径。

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 ? 我在console.log(resCounter)中得到计数,但是如何返回该值,换句话说,如何在车把中打印该计数呢? 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. 只需添加一个变量closedAccountFound = false并在找到任何已关闭的帐户时将其赋值为true,然后在返回结果之前检查值,如果closedAccountFound为true,则返回结果,否则返回消息。

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){

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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