【问题标题】:JSRender pass two "datasets" to templateJSRender 将两个“数据集”传递给模板
【发布时间】:2018-05-14 10:08:48
【问题描述】:

我有这个工作模板:

<script id="UpdateTemplate" type="text/x-jsrender">

<div class="ms-PanelPoultry">
<button class="ms-Button" id="*****" style="visibility: hidden";>
<span class="ms-Button-label">Open Panel</span> 
</button>
<div class="ms-Panel ****">
<div class="ms-Panel-contentInner">
  <p class="ms-Panel-headerText"></p>
  <div class="ms-Panel-content">
    <span class="ms-font-m">
        <span style="color:#006; font-size:large">***</span>
        <hr>
        <form id="*****">
        <table width="100%" border="0">

                {{for}}
                    {{if (#index) % 3 === 0 }}
                     </tr>
                     <tr>
                     <td>
                        <div class="form-group">
                            <label for={{>name}}>{{>label}}</label>
                            <input type={{>type}} class="form-control" id={{>name}}>
                        </div>
                    </td>

                    {{else}}
                        {{if #index === 0 }}
                            <tr>
                                <td>
                                    <div class="form-group">
                                        <label for={{>name}}>{{>label}}</label>
                                        <input type={{>type}} class="form-control" id={{>name}}>
                                    </div>
                                </td>
                        {{else}}
                            <td style="padding-left:15px;">
                                <div class="form-group">
                                    <label for={{>name}}>{{>label}}</label>
                                    <input type={{>type}} class="form-control" id={{>name}}>
                                </div>
                            </td>
                        {{/if}}
                    {{/if}} 
                {{/for}} 
            </table>
            <hr>
            <table>
            <tr>
                <td>
                 <button type="submit" id="EditProductiebedrijfButton" class="btn btn-primary">Submit</button>
                 <button class="btn btn-secondary" type="Cancel" onClick="panelInstance.dismiss();">Cancel</button>
               </td>
            </tr>
        </table>            
    </form>
    </span> 
  </div>
</div>
</div>
</div>
</script>

它在 ms-panel 中使用此数据创建一个表单:

var Updatefields = [
        { name: "field1", type: "text", label: "blabla" },
        { name: "field2", type: "text", label: "bla" },
    { name: "field3", type: "date", label: "blablaaa" }
   ];

这一切正常并呈现我的表单。但我想将一些额外的数据传递给“模板的标题”。 “*****”现在在哪里。例如“表单 ID”。

我怎样才能做到这一点?

我还想为我的所有“名称”值使用“前缀”。例如名称是“field3”作为输入字段的 id 我想要“field3Update”

我试图做一些字符串连接,但到目前为止失败了。

编辑:最后一个问题非常简单。原来是这样做的:id={{>name}}Update

【问题讨论】:

  • 有人可以给我一个线索吗?

标签: javascript jsrender


【解决方案1】:

或者将附加数据作为数据传入,以及字段数组:

$("#xxx").render({
  fields: fields,
  other: otherData
});

其他数据在哪里

{
  whatever: ...,
  ...
}

然后写

<form id="{{>other.whatever...}}...">
  <table...>
    {{for fields}}

或将其他数据作为助手传递 (http://www.jsviews.com/#tmplrender@helpers):

$("#xxx").render(fields, otherData, true);

然后写

<form id="{{>~whatever...}}...">
  <table...>
    {{for}}

【讨论】:

  • 非常感谢鲍里斯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2022-11-14
相关资源
最近更新 更多