如何使用 jQuery.tmpl 在
我正在 中插入绑定到 JSON 对象的行。表中的单元格包含需要包含对象数量的
这可以在模板定义中完成吗?
编辑:我添加了示例数据和标记。根据这些数据,我想将 3 个 (1-3) 添加到 DisplayOrder
选择的 option>。
示例: http://jsfiddle.net/4SxTS/
var data = [{
"Id": 1,
"DisplayOrder": 1,
"LabelText": "Unit On-Line Status:"},
{
"Id": 2,
"DisplayOrder": 2,
"LabelText": "AGC Pct:"},
{
"Id": 3,
"DisplayOrder": 3,
"LabelText": "Main Steam Heat Rate
}];
<table border="1" cellpadding="0" cellspacing="0">
<thead><tr><th>Display Order</th><th>Label Text</th></tr></thead>
<tbody></tbody>
</table>
<script id="attributeTemplate" type="text/x-jquery-tmpl">
<tr>
<td><select name="DisplayOrder"></select></td>
<td><input type="text" name="LabelText" value="${LabelText}" /></td>
</tr>
</script>
I'm inserting rows in a <table>
that are bound to a JSON object. A cell in the table contains a <select>
that needs to contain the number of objects, so 7 objects would produce 7 <option>
s, 1-7.
Can this be done in the template definition?
Edit: I've added sample data and markup. From this data, I would want to add 3 <option>
s (1-3) to the DisplayOrder <select>
and have the appropriate <option>
selected for each <select>
.
Example: http://jsfiddle.net/4SxTS/
var data = [{
"Id": 1,
"DisplayOrder": 1,
"LabelText": "Unit On-Line Status:"},
{
"Id": 2,
"DisplayOrder": 2,
"LabelText": "AGC Pct:"},
{
"Id": 3,
"DisplayOrder": 3,
"LabelText": "Main Steam Heat Rate
}];
<table border="1" cellpadding="0" cellspacing="0">
<thead><tr><th>Display Order</th><th>Label Text</th></tr></thead>
<tbody></tbody>
</table>
<script id="attributeTemplate" type="text/x-jquery-tmpl">
<tr>
<td><select name="DisplayOrder"></select></td>
<td><input type="text" name="LabelText" value="${LabelText}" /></td>
</tr>
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有一些示例数据和标记,我不确定您的意思,但我认为您问题的主要部分涉及循环数字并为每个数字呈现
option
标记。您可以通过创建一个包含“N”个元素的数组,然后使用{{each}}
来实现此目的,但我认为使用自定义模板标记在这里效果很好:定义一个
{{for}}
标签,迭代数字 0 到 n:在模板中使用新标签:
调用方式:
在工作示例中最佳演示: http: //jsfiddle.net/xgE3Y/
您的情况可能更简单,但我的答案应该使您至少能够在模板中使用简单的
for
循环并对每次迭代的值进行操作。I'm not sure exactly what you mean without some sample data and markup, but I think the main part of your question deals with looping through numbers and rendering an
option
tag for each number. You could do this by creating an array containing "N" elements and then using{{each}}
, but I think that using a custom template tag works well here:Define a
{{for}}
tag that iterates through numbers 0 to n:Use the new tag in a template:
Called with:
Best demonstrated in a working example: http://jsfiddle.net/xgE3Y/
Your situation might be even simpler, but my answer should enable you to at least use a simple
for
loop in a template and act on each iteration's value.也许是这样的:
Html:
Maybe something like this:
Html: