使用 Jquery 模板处理无结果行的更好方法
我正在使用 JQuery 模板,并且希望生成一行,如果发送到模板的数组中没有结果,则显示“未找到结果”。我现在处理它的方法是有两个模板 - 一个输出行,另一个输出不存在行的实例,但这对我来说似乎很脏。有更好的方法来处理这种情况吗?
if(results.length == 0)
$( "#NoRowsTemplate" ).tmpl( results ).appendTo("#tableid")
else
$( "#HasRowsTemplate" ).tmpl( new Array(1)).appendTo("#tableid")
以下是包含行的模板示例:
<script id="HasRowsTemplate" type="text/x-jquery-tmpl">
<tr>
<td>${name}</td>
</tr>
</script>
以下是不包含行的模板示例:
<script id="NoRowsTemplate" type="text/x-jquery-tmpl">
<tr>
<td>No Rows Were Found</td>
</tr>
</script>
I am using JQuery templates and want to generate a single row that says "no results found" if there are no results in the array that gets sent to the template. The way I handle it now is to have two templates - one that outputs the rows and another with output for the no rows exist instance, but this seems dirty to me. Are there better ways to handle this situation?
if(results.length == 0)
$( "#NoRowsTemplate" ).tmpl( results ).appendTo("#tableid")
else
$( "#HasRowsTemplate" ).tmpl( new Array(1)).appendTo("#tableid")
Here is a sample of the template with rows:
<script id="HasRowsTemplate" type="text/x-jquery-tmpl">
<tr>
<td>${name}</td>
</tr>
</script>
And here is a sample of the template without rows:
<script id="NoRowsTemplate" type="text/x-jquery-tmpl">
<tr>
<td>No Rows Were Found</td>
</tr>
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选择是将数组包装在匿名对象中,这样就不会为数组中的每个项目调用模板,然后您可以使用条件语句 (
{{if}}
) 来检查长度并使用{{each}}
循环遍历项目。因此,请像这样传递它:
$("#Template").tmpl({ items: results }).appendTo("#tableid")
One option is to wrap the array in an anonymous object, so that the template is not called for each item in the array, then you can use a conditional statement (
{{if}}
) to check for the length and use{{each}}
to loop through the items.So, pass it like:
$("#Template").tmpl({ items: results }).appendTo("#tableid")
您可以使用
{{if}}
和{{else}}
。如果没有实际看到相关模板,很难透露更多信息。
更新:
好的,您发布了一些示例,所以我现在可以说更多。首先,桌子的其余部分在哪里?为什么模板中没有?原始数据被附加到现有表中,但我假设这只执行一次,因为否则您不会将“未找到行”消息附加到表中,但您会替换现有内容。
首先,在这种特殊情况下,您可能会使用快速破解并仅使用:
它应该只执行您想要的操作,但我不会推荐此方法,因为它会更改数组并可能会导致代码其他部分出现一些问题。我建议这样做:
使用这个新模板:
并且我还将表 HTML 的其余部分添加到该模板的开头和结尾。
You can use
{{if}}
and{{else}}
.It's hard to tell more without actually seeing the template in question.
Update:
Ok, you posted some examples so I can say more now. First of all, where is the rest of your table? Why is it not in the template? The raws are appended to an existing table but I assume this is done only once because otherwise you wouldn't append the "No Rows Were Found" message to the table, but you would replace the existing content.
First of all in this particular case you might use a quick hack and just use:
and it should just do what you want but I wouldn't recommend this method because it changes the array and may cause some problems in other parts of your code. I would recommend doing something like this:
with this new template:
and I would also add the rest of the table HTML to the very beginning and the very end of this template.