2

This is My Code below

<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> 

I want to check data is empty or null in jsrender? how to check ?

Maulik Patel
  • 2,742
  • 4
  • 19
  • 28

3 Answers3

3

Couldn't you just check if test1.data && test1.data.length > 0

jsrender has if/else statements as well

chris van hooser
  • 388
  • 3
  • 11
2

If you are using {{for data}} then you don't actually need to test for empty/null (whether in code or by wrapping in {{if}}...{{/if}}).

You can simply write

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

and that will work fine even if it is empty or null - it will just render nothing for that block.

Or if you want you can use

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

and it will output whatever you want for the null/empty case.

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

BorisMoore
  • 8,444
  • 1
  • 16
  • 27
0

Try this

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>
Vijay
  • 421
  • 3
  • 12