Reputation: 22847
I was using dijit.form.ComboBox
, but the requirement has changed, and now dijit.form.Select
is more appropriate.
However, after changing the widget type I get an error:
TypeError: _b6d.getLabel is not a function
From the documentation http://dojotoolkit.org/reference-guide/1.10/dijit/form/Select.html I see that dijit.form.Select
expect fields id
and label
in the Store, while my Store has id
and name
.
The documentation gives no example how to override that defaults. So, how can I specify the id
and label
attributes for dijit.form.Select
?
My data looks like that:
[{"id":1,"name":"Item 1"},{"id":2,"name":"Item 2"},{"id":3,"name":"Item 3"}]
In case it's not possible, what dijit widget can I use for selection restricted only to items from Store?
Upvotes: 0
Views: 2261
Reputation: 44685
There is a labelAttr
property on the dijit/form/Select
which you can use to change the name of the label attribute.
For example:
new Select({
store: myStore,
labelAttr: "name" // Now the name attribute will be used
}, "mySelect");
If you also like to change the attribute used for the ID (it's not necessary in this case, but you asked how to specify that one as well), then you should look at the idProperty
of the dojo/store/Memory
, for example:
var myStore = new Memory({
idProperty: "id",
data: [{
"id":1,
"name":"Item 1"
}, {
"id":2,
"name":"Item 2"
}, {
"id":3,
"name":"Item 3"
}]
});
A full example can be found on JSFiddle: http://jsfiddle.net/NvKgH/
Upvotes: 3
Reputation: 2101
You can use a FilteringSelect to restrict the selection to only the items that are in the store. And it shouldn't be much trouble to implement it, because both ComboBox
and FilteringSelect
use dijit/form/ComboBoxMixin
to provide the combobox functionality.
Upvotes: 1
Reputation: 3330
I think you have misread the documentation.
It mentions over here that the data from the store must have id and label attributes, not value and label attributes.
The id of the store will be the value you get using the get("value") call.
For getting the value you need to use get("value") function on the widget i.e
widget.get("value")
and for label I am assuming that it is
widget.get("label")
[{"id":1,"name":"Item 1","label":"Item 1"},{"id":2,"name":"Item 2","label":"Item 2"},{"id":3,"name":"Item 3","label":"Item 3"}]
Upvotes: 0