Dijit.tree extension with radio buttons showing wrong value

I wrote a class that extends dijit.Tree to include a radio button next to each node. I use it on a form to show a folder tree from which the user can select a folder. Here is the code:

define("my/Tree/RadioButton",
    ['dojo/_base/declare', 'dijit/Tree', 'dijit/form/RadioButton', 'dojo/dom-construct', 'dojo/_base/connect', 'dojo/on', 'dojo/_base/lang'],
    function (declare, Tree, RadioButton, domConstruct, connect, on, lang){

    var TreeNode = declare(Tree._TreeNode, {
        _radiobutton: null,

        postCreate: function(){
            this._createRadioButton();

            this.inherited(arguments);
        },

        _createRadioButton: function(){
            this._radiobutton = new RadioButton({
                name: this.tree.name,
                value: this.tree.model.store.getIdentity(this.item) + '',
                checked: false
            });
            domConstruct.place(this._radiobutton.domNode, this.iconNode, 'before');

            if (this.tree.model.store.hasAttribute(this.item, 'checked')) {
                var attrValue = this.tree.model.store.getValue(this.item, 'checked');
                if (attrValue === true) {
                    this._radiobutton.set('checked', true);
                }
            }

            connect.connect(this._radiobutton, 'onClick', this, function(){
                // set any checked items as unchecked in the store
                this.tree.model.store.fetch({
                    query: {checked: true},
                    onItem: lang.hitch(this.tree.model.store, function(item){
                        console.log('found checked item ' + this.getValue(item, 'name'));
                        this.setValue(item, 'checked', false);
                    })
                });

                // check the one that was clicked on
                var radioValue = this._radiobutton.get('value');
                this.tree.model.store.setValue(this.item, 'checked', true);
            });
        }
    });

    return declare(Tree, {
        _createTreeNode: function(/*Object*/ args){
            return new TreeNode(args);
        }
    });
});

      

The problem is that when the form is submitted, the value presented is always the value of the first radio button that was selected, even if you click on other radio buttons afterwards.

I can see by checking the dom that the attribute value for the checked radio button has the correct value. But what is sent is always the originally selected value.

I have a similar class that uses a checkbox widget instead and that works fine.

Edit based on some feedback, I've created an even simpler version of this class that doesn't track the checked state using an attribute in the store:

define("my/Tree/RadioButton",
    ['dojo/_base/declare', 'dijit/Tree', 'dijit/form/RadioButton', 'dojo/dom-construct'],
    function (declare, Tree, RadioButton, domConstruct){

    var TreeNode = declare(Tree._TreeNode, {
        _radiobutton: null,

        postCreate: function(){
            this._createRadioButton();

            this.inherited(arguments);
        },

        _createRadioButton: function(){
            this._radiobutton = new RadioButton({
                name: this.tree.name,
                value: this.tree.model.store.getIdentity(this.item) + '',
                checked: false
            });
            domConstruct.place(this._radiobutton.domNode, this.iconNode, 'before');
        }
    });

    return declare(Tree, {
        _createTreeNode: function(/*Object*/ args){
            return new TreeNode(args);
        }
    });
});

      

but even this problem still has the same problem. Regardless of which buttons are clicked, the button the user first clicks on is the value to be sent.

+3


source to share


2 answers


I managed to solve this problem by connecting to the onchange event for the radio buttons. The hook explicitly sets a flag to false on an untested radio button which seems to fix the problem. I'm not sure why this is required.



0


source


I have the same problem. He used to work in the old Dojo. In particular, ALL radios incorrectly return true on "dijit.byId("whatever").checked"

during a function onClicked

. When manually checking after the function completes onClicked

using the FireBug console, the specified property returns the correct values. I think this is a bug and I only worked around it, having a different function for each button onClicked

, for example:



<form id="locateForm">
<label for="locate">Locate:</label><br />
<input type="radio" dojoType="dijit.form.RadioButton" name="locate" id="locateAddress" value="Address" checked="checked" onClick="enableLocate1();" />
<label for="locateAddress">Address</label>
<input type="radio" dojoType="dijit.form.RadioButton" name="locate" id="locatePlace" value="Place" onClick="enableLocate2();" />
<label for="locatePlace">Place</label>
</form>

      

0


source







All Articles