22
votes

Hi I've a kendo grid like below and I wanted to check null value for column and based on condition I want to display some default number to the column

Here is my sample code.

 $("#eCount").kendoGrid({
        dataSource: {
            data: myModel,
            pageSize: 5
},      
 columns: [
            {
                field: "Count",
                title: "Count",
                template: '# if (Count == "null" ) {#1#} else {#Count#}#'
            }]
});

But I'm not getting how to get it done. Any solution?

3
I've got the soluion '#if(Count===null) {# 1 #}else{# #=Count# # }# ' it is working for me - jestges

3 Answers

46
votes

You can use Javascripts inline if format

#= street2 != null ? street2 : '' #
18
votes

I found this to be the most usefull:

#= typeof street2 == "undefined" || street2 == null ? "" : street2 #

The typeof check can be useful when adding rows programatically to the grid's datasource and not specifying the value for the street2 field:

grid.dataSource.add({}); //this line will generate an error when you're not using 'typeof' check

Also related to your question, for more complex scenarios, I've also found useful to do other checks inside the template using data.xxx, like this:

# if (data.street2 && data.street2.length) { #
    <span>#: street2 # </span>
# } else { #
    <span>N/A</span>
# } #
1
votes
var dataSource = new kendo.data.DataSource({
    transport: {
    ...
    },
    schema: {
        model: {

            myCount: function () {
                return this.get("Count") == null ? 1 : this.get("Count");
            }
        }
    }

<script id="template">
        #=myCount()#
</script>

Or you can do this if you are not using a datasource.

<script id="template">
    # var count = data.Count || 1; # // Javascript  #   #
    <span>#=count#</span>            // Binding  #=   #
</script>