Default value for enum column

3 posts, 0 answers
  1. Richard
    Richard avatar
    10 posts
    Member since:
    Aug 2013

    Posted 18 Nov 2014 Link to this post

    Suppose I have an enum that I'd like to use in a Grid, using the _underlying value_ as the cell value:

    public enum Currency : int {
       USD = 0,
       GBP = 1
    }

    The editor is thus something similar to:

    @(Html.Kendo().DropDownListFor(m => m)
          .BindTo(Enum.GetValues(typeof(Currency)).Cast<Currency>().Select(v => {
              return new DropDownListItem()
              {
                  Text = v.DisplayName(),
                  Value = ((int)v).ToString(),
                  Selected = v == Model,
              };
          }).ToList()))

    If I don't include an entry in the data source model for the field, the field defaults as expected to 0.

    However, if I include the following entry in the data source model, Kendo attempts to default the value to "usd":

    model.Field(d => d.Currency).DefaultValue((int) Currency.USD);

    This isn't a problem for this case, as I can just leave off the model configuration. However, it becomes a problem if I change the type of the column to Nullable<Currency> because there is no way to default the value to 0. Not configuring the model attempts to use null (which causes Kendo grid to throw an exception), and attempting to configure the model uses "usd", which is invalid.

    I have attempted manually setting the schema using JS:

    $(document).ready(function () {
         $("#MyGrid").data("kendoGrid").dataSource.options.schema.model.fields.Currency.defaultValue = 0;
    }

    However when I add a new column, the grid still attempts to use "usd" as the default value.

    Please advise how to set the default value to 0 for a column type of Nullable<Currency>.



  2. Richard
    Richard avatar
    10 posts
    Member since:
    Aug 2013

    Posted 18 Nov 2014 in reply to Richard Link to this post


    As a work around, I've overwritten the _createNewModel method for the Grid.  

    $(document).ready(function () { 
        // Kendo Hack to correct the default of currency to 0 (for USD)
          var gridSource = $("#MyGrid").data("kendoGrid").dataSource;
        gridSource.options.schema.model.fields.Currency.defaultValue = 0;
        gridSource._createNewModel = function (model) {
            if (this.reader.model) {
                var result = new this.reader.model(model);
                result.Currency = 0;
                return result;
            }
     
            if (model instanceof ObservableObject) {
                return model;
            }
     
            return new ObservableObject(model);
        };
    });
  3. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 20 Nov 2014 Link to this post

    Hello,

    Could you clarify what exception is thrown when no default value is specified?
    I reproduced the problem with the default value conversion and we will fix it for one of the next internal builds. For now I can suggest to set a function for the default value that returns the underlying value;
    model.Field(o => o.Currency).DefaultValue(new Kendo.Mvc.ClientHandlerDescriptor
        {
            HandlerName = "function() {return " + (int)Currency.USD + "}"
        });

    As for setting the value via JavaScript - this can be achieved as in the snippet below without overriding the dataSource methods:
    $(document).ready(function () {
        $("#MyGrid").data("kendoGrid").dataSource.reader.model.fn.defaults.Currency = 0;
    });


    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top