How to check null values using kendo template

33,226

Solution 1

You can use Javascripts inline if format

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

Solution 2

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

Solution 3

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>
Share:
33,226

Related videos on Youtube

jestges
Author by

jestges

Updated on April 09, 2020

Comments

  • jestges
    jestges about 4 years

    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?

    • jestges
      jestges almost 11 years
      I've got the soluion '#if(Count===null) {# 1 #}else{# #=Count# # }# ' it is working for me
  • SamG
    SamG over 7 years
    If you have a Kendo drop down list column as a custom MVC editor in a Kendo grid and the drop down list is empty, you have to use the 'typeof' check cited. Initially, I was only checking if the value is null, but it's actually undefined. One symptom of this is that you'll get an undefined reference error from jquery on your custom template.
  • Vamshi Vangapally
    Vamshi Vangapally over 5 years
    That works! In a multi-valued address (street1,county, state,country) - how to skip commas beside null values
  • A.J.
    A.J. about 5 years
    you can do an .indexOf(',') to see if the string has a comma in it. Is that what you are asking?