3

各ノードの横にラジオ ボタンを含めるように dijit.Tree を拡張するクラスを作成しました。ユーザーがフォルダーを選択できるフォルダーツリーを表示するフォームで使用しています。コードは次のとおりです。

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);
        }
    });
});

問題は、フォームが送信されると、送信される値は常に、選択された最初のラジオ ボタンの値であり、その後他のラジオ ボタンがクリックされた場合でもです。

dom を調べると、チェックされたラジオ ボタンの値属性が正しい値になっていることがわかります。ただし、送信されるのは常に最初に選択された値です。

代わりにチェックボックスウィジェットを使用する同様のクラスがあり、それは正常に機能します。

いくつかのフィードバックに基づいて編集ストアの属性を使用してチェック済みの状態を追跡しない、このクラスのさらに単純なバージョンを作成しました。

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);
        }
    });
});

ただし、これでも同じ問題があります。ユーザーが最初にクリックしたラジオ ボタンは、その後にクリックされた他のボタンに関係なく、送信される値です。

4

2 に答える 2

0

私はこれとまったく同じ問題を抱えています。以前は古い道場で機能していました。具体的には、radioButtons のすべてが"dijit.byId("whatever").checked"、関数中に誤って true を返しますonClickedonClicked関数が FireBug コンソールを使用して終了した後に手動でチェックすると、上記のプロパティは正しい値を返します。これはバグだと思いonClickedます。次のように、ボタンごとに異なる機能を持たせることでのみ回避しました。

<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>
于 2013-03-01T22:49:08.787 に答える
0

ラジオボタンの onchange イベントにフックすることで、この問題を回避することができました。フックは、チェックされていないラジオボタンでチェックを明示的にfalseに設定します。これにより、問題が解決したようです。なぜこれが必要なのかはわかりません。

于 2013-01-31T16:41:36.373 に答える