24

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?

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
jestges
  • 3,686
  • 24
  • 59
  • 95

3 Answers3

49

You can use Javascripts inline if format

#= street2 != null ? street2 : '' #
A.J.
  • 904
  • 9
  • 20
20

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>
# } #
Lucian
  • 3,981
  • 5
  • 30
  • 34
  • 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. – SamG Dec 31 '16 at 18:57
1
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>
jwize
  • 4,230
  • 1
  • 33
  • 51