使用 jquery 模板进行过多的递归
我正在尝试渲染递归模板,但出现“递归过多”错误...
任何人都可以告诉我这里做错了什么吗?: jsfiddle 中的代码
谢谢!
编辑 1(添加到帖子中的代码):
HTML
<script id="my_template" type="text/j-query-tmpl">
<ul class="modules_ul">
{{each(i, module) modules}}
<li>${module.mod_name}</li>
{{if module.sub_modules}}
{{tmpl "my_template_compiled"}}
{{/if}}
{{/each}}
</ul>
</script>
JS
var data = {"modules":[
{
"id_modules": "1",
"id_modules_parent": "0",
"mod_name": "mod 1",
"sub_modules": [
{
"id_modules": "5",
"id_modules_parent": "1",
"mod_name": "mod 1a"
},
{
"id_modules": "7",
"id_modules_parent": "1",
"mod_name": "mod 1b"
}
]
},
{
"id_modules": "2",
"id_modules_parent": "0",
"mod_name": "mod 2",
"sub_modules": [
{
"id_modules": "6",
"id_modules_parent": "2",
"mod_name": "mod 2a",
"sub_modules": [
{
"id_modules": "3",
"id_modules_parent": "6",
"mod_name": "mod 2aa"
}
]
},
{
"id_modules": "4",
"id_modules_parent": "2",
"mod_name": "mod 2b"
}
]
}
]};
$("#my_template").template("my_template_compiled");
$.tmpl("my_template_compiled", data).appendTo("#some_div");
I'm trying to render a recursive template, but I'm getting a "too much recursion" error...
Can anyone tell what I'm doing wrong here?: code in jsfiddle
Thanks!
EDIT 1 (code added to post):
The HTML
<script id="my_template" type="text/j-query-tmpl">
<ul class="modules_ul">
{{each(i, module) modules}}
<li>${module.mod_name}</li>
{{if module.sub_modules}}
{{tmpl "my_template_compiled"}}
{{/if}}
{{/each}}
</ul>
</script>
The JS
var data = {"modules":[
{
"id_modules": "1",
"id_modules_parent": "0",
"mod_name": "mod 1",
"sub_modules": [
{
"id_modules": "5",
"id_modules_parent": "1",
"mod_name": "mod 1a"
},
{
"id_modules": "7",
"id_modules_parent": "1",
"mod_name": "mod 1b"
}
]
},
{
"id_modules": "2",
"id_modules_parent": "0",
"mod_name": "mod 2",
"sub_modules": [
{
"id_modules": "6",
"id_modules_parent": "2",
"mod_name": "mod 2a",
"sub_modules": [
{
"id_modules": "3",
"id_modules_parent": "6",
"mod_name": "mod 2aa"
}
]
},
{
"id_modules": "4",
"id_modules_parent": "2",
"mod_name": "mod 2b"
}
]
}
]};
$("#my_template").template("my_template_compiled");
$.tmpl("my_template_compiled", data).appendTo("#some_div");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://jsfiddle.net/aNPvz/22/ (不要问我为什么要点不要在 jsFiddle 上渲染;当我构建自己的页面时,它们会渲染。)
{{tmpl "my_template_compiled"}}
调用整个模块而不仅仅是子模块。请注意对 json 结构的更改以使递归成为可能。http://jsfiddle.net/aNPvz/22/ (Don't ask me why the bullet points don't render on the jsFiddle; they do when I build my own page.)
{{tmpl "my_template_compiled"}}
was calling the entire modules instead of just the sub-modules. Note the change to your json structure to make recursion possible.