1. #1
    Sencha Premium Member
    Join Date
    Jan 2010
    Location
    Vancouver, Canada
    Posts
    133
    Vote Rating
    10
    bogc will become famous soon enough

      0  

    Default binding a Checkbox to a numeric field

    binding a Checkbox to a numeric field


    Hello:

    I have a model with an int field called HasNotes, which is 1 or 0 and this feeds a form CheckBox.

    When I call the form updateRecord, HasNotes is set to NaN because updateRecord feeds the model the CheckBox value, which is true or false.

    Is it possible to intercept the value and save in the store 1 or 0 instead? If yes, how?


    Thanks!

  2. #2
    Sencha Premium Member
    Join Date
    Aug 2011
    Posts
    59
    Vote Rating
    4
    ids is on a distinguished road

      0  

    Default


    Have you tried the "inputValue"/"uncheckedValue" properties of Ext.form.field.Checkbox?

  3. #3
    Sencha Premium Member
    Join Date
    Jan 2010
    Location
    Vancouver, Canada
    Posts
    133
    Vote Rating
    10
    bogc will become famous soon enough

      0  

    Default


    I actually did but I forgot to mention it in the post.

    I also looked at the source code for the Checkbox component. The problem is that when the data is moved from the form to the record (or model instance, if you want) ExtJs calls the getValue function of the component. If you look at getValue for Checkbox it simply returns:

    Code:
        getValue: function() {
            return this.checked;
        }
    What I basically want is getValue to behave like getSubmitValue():

    Code:
        getSubmitValue: function() {
            var unchecked = this.uncheckedValue,
                uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
            return this.checked ? this.inputValue : uncheckedVal;
        },
    
        isChecked: function(rawValue, inputValue) {
            return (rawValue === true || rawValue === 'true' || rawValue === '1' || rawValue === 1 ||
                          (((Ext.isString(rawValue) || Ext.isNumber(rawValue)) && inputValue) ? rawValue == inputValue : this.onRe.test(rawValue)));
        },
    In that case yes, inputValue and unchecked would work.

  4. #4
    Sencha Premium Member
    Join Date
    Jan 2010
    Location
    Vancouver, Canada
    Posts
    133
    Vote Rating
    10
    bogc will become famous soon enough

      0  

    Default


    I ended up using this convert function on the model field:

    Code:
    function (val, record) {if (val === true) return 1; if (val === false) return 0; return val;}

  5. #5
    Sencha Premium Member
    Join Date
    Jan 2010
    Location
    Vancouver, Canada
    Posts
    133
    Vote Rating
    10
    bogc will become famous soon enough

      0  

    Default


    Just as a side note, I think a better solution would have been to provide some config properties called:

    trueValue - this would be defaulted to true
    falseValue - this would be defaulted to false
    trueValues - array of values equivalent with "true"

    The name inputValue is confusing. uncheckedValue is less confusing but still.

    Then change the getValue function to return the trueValue if the checkbox is checked otherwise to return the falseValue.

Thread Participants: 1