Login Register

Simple select fields

Hello,

Is there a very simple select? Which is themed and allows a user to select something, but does not include the ability to edit the entry in the text box? I've looked at the Combo and Filtered select but both have additional features that are not required..

John

John, dijit doesn't have a

John,

dijit doesn't have a Select widget, but I created one by copying dijit.form.MultiSelect and just making a couple minor changes - here it is if you want to use it - just place it in your distribution directory in the dijit/form directory and call it Select.js. I'm not sure but there might be a dijit.form.Select in the upcoming 1.2 release so when that comes out check if the dojo team included something like this and use that instead.

dojo.provide("dijit.form.Select");

dojo.require("dijit.form._FormWidget");

dojo.declare("dijit.form.Select",dijit.form._FormWidget,{
        // summary: Wrapper for a native select element to
        //            interact with dijit.form.Form

        // size: Number
        //            Number of elements to display on a page
        //            NOTE: may be removed in version 2.0, since elements may have variable height;
        //            set the size via style="..." or CSS class names instead.
        size: 7,
       
        templateString: "<select dojoAttachPoint='containerNode,focusNode' dojoAttachEvent='onchange: _onChange'></select>",

        attributeMap: dojo.mixin(dojo.clone(dijit.form._FormWidget.prototype.attributeMap),
                {size:"focusNode"}),

        addSelected: function(/* dijit.form.Select */select){
                // summary: Move the selected nodes af an passed Select widget
                //                  instance to this Select widget.
                //
                // example:
                // |    // move all the selected values from "bar" to "foo"
                // |    dijit.byId("foo").addSelected(dijit.byId("bar"));
               
                select.getSelected().forEach(function(n){
                        this.containerNode.appendChild(n);
                },this);
        },
                                       
        getSelected: function(){
                // summary: Access the NodeList of the selected options directly
                return dojo.query("option",this.containerNode).filter(function(n){
                        return n.selected; // Boolean
                });
        },
       
        _getValueDeprecated: false, // remove when _FormWidget:_getValueDeprecated is removed in 2.0
        getValue: function(){
                // summary: Returns an array of the selected options' values
                return this.getSelected().map(function(n){
                        return n.value;
                });
        },
       
        _multiValue: false, // for Form
        setValue: function(/* Array */values){
                // summary: Set the value(s) of this Select based on passed values
                dojo.query("option",this.containerNode).forEach(function(n){
                        n.selected = (dojo.indexOf(values,n.value) != -1);
                });
        },
               
        invertSelection: function(onChange){
                // summary: Invert the selection
                // onChange: Boolean
                //            If null, onChange is not fired.
                dojo.query("option",this.containerNode).forEach(function(n){
                        n.selected = !n.selected;
                });
                this._handleOnChange(this.getValue(), onChange==true);
        },

        _onChange: function(/*Event*/ e){
                this._handleOnChange(this.getValue(), true);
        },
       
        // for layout widgets:
        resize: function(/* Object */size){
                if(size){
                        dojo.marginBox(this.domNode, size);
                }
        },
       
        postCreate: function(){
                this._onChange();
        }
});

Josh

That's superb, thanks. I've

That's superb, thanks.

I've never been a fan of JS toolkits, and have crafted JS by hand for a number of years, but I have to admit that Dojo/Dijit is very smart. Well done chaps.

Thanks for posting this,

Thanks for posting this, Josh. But may I suggest that if it's really going to implement a single-select Select box, then setValue shouldn't be trying to set multiple items, and invertSelection probably shouldn't exist.

But the real questions are a) why doesn't Dojo have a simple select widget already? and b) why should multiple select be an entirely different widget, instead of just being an option on a select widget that implements both?