简体   繁体   English

有条件地显示EJS数据

[英]Conditionally Displaying EJS data

I am trying to reuse aa view in EJS. 我试图在EJS中重用一个视图。 It just creates a list of the results of a database query. 它只是创建数据库查询结果的列表。 My code is 我的代码是

   <ul data-role="listview" data-theme="a">
    <% for(var i=0; i<records.length; i++) { %>
        <li><a href="#actions" onClick="SetCurrentCustomer(<%= records[i].ID %>)">
PROBLEM HERE---->    <%= records[i].Name %> <%= records[i].TOJ%></a></li>
    <% } %>
    </ul>

I would only like to show each records[i].Name and records[i].TOJ if they are defined or if the view is called from a certain route or whatever is easier. 我只想显示每个记录[i] .Name和记录[i] .TOJ,如果它们被定义,或者视图是从某个路线调用的,还是更容易的。 I have tried inserting a function to filter, but it just got ignored. 我试过插入一个过滤函数,但它被忽略了。 How can i do this? 我怎样才能做到这一点?

Like for , you can also use if in EJS: for ,你也可以使用if在EJS:

<% if (someCondition) { %>
  <%= records[i].Name %> <%= records[i].TOJ %>
<% }; %>

Or, if you just want to prevent undefined being shown when a property isn't defined, you can use this: 或者,如果您只是想在undefined属性时阻止显示undefined ,则可以使用:

<%= records[i].Name || '' %> <%= records[i].TOJ || '' %>

(be aware that this would fail for properties containing the number 0 or a boolean false ) (请注意,对于包含数字0或布尔值false属性,这会失败)

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

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