【问题标题】:How render Input fields dynamically based on position set in database with EJS如何使用 EJS 根据数据库中设置的位置动态呈现输入字段
【发布时间】:2016-10-05 21:05:28
【问题描述】:

我有一个输入字段类型存储在数据库中,其位置由用户设置。所以例如名字,文本框1,姓氏文本框2。这个位置可以改变。我想渲染一个视图,显示输入元素在数据库中的位置。这是我从数据库中得到的数据数据库`公式:

   [ { FormularID: 187,                                                                                                                                                                                                
       CampaignID: 13,                                                                                                                                                                                                 
       FormLabel: 'Title',                                                                                                                                                                                             
       InputType: 'textbox',                                                                                                                                                                                           
       InputDetails: '',                                                                                                                                                                                               
       InputPosition: 1 },  
     FormularID: 188,                                                                                                                                                                                                
       CampaignID: 13,                                                                                                                                                                                                 
       FormLabel: 'DATE OF BIRTH',                                                                                                                                                                                             
       InputType: 'DATE',                                                                                                                                                                                           
       InputDetails: '',                                                                                                                                                                                               
       InputPosition: 2 },                                                                                                                                                                                             
     { FormularID: 189,                                                                                                                                                                                                
       CampaignID: 13,                                                                                                                                                                                                 
       FormLabel: 'First Name',                                                                                                                                                                                        
       InputType: 'textbox',                                                                                                                                                                                           
       InputDetails: '',                                                                                                                                                                                               
       InputPosition: 0 } ] }    `
Now in my view i have tried this but it doesn't arrange based on the position set.

    <% for(var i in templateData.formular){ %>
    <% if(templateData.formular[i].InputPosition==i){ %>
        <% if(templateData.formular[i].InputType=='textbox'){ %>

            <div class="form-group">
            <label for="inputEmail3"><%=templateData.formular[i].FormLabel + i %></label>
            <input type="text" class="form-control" id="inputEmail3">
            </div>
        <% } %>
<% if(templateData.formular[i].InputType=='date'){ %>
            <div class="form-group">
            <label for="inputEmail3" class="col-sm-1 col-form-label"><%=templateData.formular[i].FormLabel%></label>
            <input type="date" class="form-control" id="inputEmail3">
            </div>
        <% } %>
    <% } %>

因此,如果用户决定将名字设置为 2,将平铺设置为 1,将出生日期设置为 0。我希望视图按该顺序呈现字段。

【问题讨论】:

    标签: javascript node.js ejs


    【解决方案1】:

    我通过像这样修改上述结构解决了我的问题

    <% for(var i=0; i< templateData.formular.length; i++){ %>
            <div class="form-group">
            <label for="inputEmail3"><%=templateData.formular[i].FormLabel %></label>
            <input type="<%=templateData.formular[i].InputType %>"class="form-control" id="inputEmail3">
            </div>
    <% } %>
    

    然后在后端,当我从数据库中读取时,我通过我的数据库 orderBy InputPosition 并获取按升序排序的数据。 因此,无论用户决定放置元素,它们都按 1-2-3 ---n 的顺序排列并呈现该顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2017-04-06
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多