2

这是下面的我的代码

<script type="text/javascript">
			
		var test1 = {
				"type" : "success",
				"code" : "600",
				"data" : [ ]
			};
		
		var template = $.templates("#template");
		var htmlOutput = template.render(test1);
		$("#billListBox").html(htmlOutput);
		
		function billButtonOnClick(searchBillId,isComeFromBillClick,billDiv){
			console.log(searchBillId);
			console.log(isComeFromBillClick);
			console.log(billDiv);
		}

	</script>
<div id="billListBox"></div> 

我想检查 jsrender 中的数据是否为空或为空?如何检查?

4

3 回答 3

3

你不能检查一下test1.data && test1.data.length > 0

jsrender 也有 if/else 语句

于 2015-03-28T06:57:17.277 回答
2

如果您正在使用,{{for data}}那么您实际上不需要测试空/空(无论是在代码中还是通过包装{{if}}...{{/if}})。

你可以简单地写

{{for data}}
    ...
{{/for}}

即使它为空或为空,它也能正常工作 - 它只会为该块呈现任何内容。

或者,如果您愿意,可以使用

{{for data}}
    ...
{{else}}
    output this if data is empty or null
{{/for}}

它将为空/空情况输出您想要的任何内容。

http://www.jsviews.com/#fortag

于 2015-03-28T19:43:29.440 回答
0

尝试这个

var test1 = {
				"type" : "success",
				"code" : "600",
				"data" : [ ]
			};

//check condition if array is empty or not
if(jQuery.isEmptyObject(test1.data)){
	alert("array is empty");
}else{
	alert("array is not empty");
}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
于 2015-03-28T15:27:28.573 回答