Dojo : ComboBox selected and show data id

天大地大妈咪最大 提交于 2019-12-08 08:32:13

问题


I have Dojo ComboBox with data

    var store = new dojo.data.ItemFileReadStore( { data: {
        identifier: "name",
        items: [
        {name:"Alabama", id:"AL"},
        {name:"Alaska", id:"AK"},
        {name:"American Samoa", id:"AS"},
        {name:"Arizona", id:"AZ"},
        {name:"Arkansas", id:"AR"},
        {name:"Armed Forces Europe", id:"AE"},
        {name:"Armed Forces Pacific", id:"AP"},
        {name:"Armed Forces the Americas", id:"AA"},
        {name:"California", id:"CA"},
        {name:"Colorado", id:"CO"},
        {name:"Connecticut", id:"CT"},
        {name:"Delaware", id:"DE"}
    ]
    }});

        var comboBox = new dijit.form.ComboBox({
        id: "stateSelect",
        name: "state",
        value: "-- Select --",
        store: store,
        searchAttr: "name"
    }, "stateSelect");

and I have textbox to search data id.

Then I want to selected(ComboBox) that data. And show the data id and value .

How can I do that.


回答1:


comboBox.item is a reference to the currently selected element.

You can get the id and value of your item through :

var id = store.getValue(comboBox.item, "id");
var name = store.getValue(comboBox.item, "name");

Then you use id and name to display the values wherever you need to...

See this jsfiddle for an example : http://jsfiddle.net/psoares/RS2Z5/




回答2:


Or you can just call comboBox.item.id and comboBox.item.name in javascript....

To set the value programmically (programmically set dojo comboBox selection)..(this might not be relative to your question...just in case someone might need it) I found that the comboBox is quite buggie when you set the selected value, if you just do:

comboBox.set('value', yourValue);
//such as comboBox.set('value', "Bob");

the box will update its display...but when you call comboBox.item it will return null....so what i did to set the selection of dojo ComboBox: //set the display

comboBox.set('value', yourValue);
//set the actual selected item
comboBox.item = comboBox.store.query({myAttributesName:"myValue"})[0];
//such as comboBox.item = comboBox.store.query({name:"Bob"})[0] it will return the   Object with name = "Bob"

Then you will be fine by using comboBox.item.myAttribute to return whatever value you want after set selection.... Happy coding.....:p




回答3:


the id and value can be accessed via comboBox.item.id and comboBox.item.name, but in my experience the comboBox.item is null until the user clicks on the ComboBox and selects an option, once they select an option then the comboBox.item is populated with the option object that was selected.

In order to select an option and set the item you can do as Vin.X suggested or you can set it in the ComboBox initialization parameters 'item' (http://dojotoolkit.org/api/?qs=1.9/dijit/form/ComboBox)

var comboBox = new dijit.form.ComboBox({
    id: "stateSelect",
    name: "state",
    store: store,
    searchAttr: "name",
    item: {name:"Alabama", id:"AL"} // or store.query({name:"Alabama"})[0]
}, "stateSelect");

Hopefully it helps someone




回答4:


you can find the answer in the official docs.

http://dojotoolkit.org/reference-guide/1.8/dijit/form/FilteringSelect.html#dijit-form-filteringselect

dijit.byId('stateSelect').get('value')
dijit.byId('stateSelect').get('displayedValue')



回答5:


This worked for me:

dijit.byId("mySelect").item.i.id

I don't know what "i" stands for but I got it by logging contents of dijit.byId("mySelect").item



来源:https://stackoverflow.com/questions/8739857/dojo-combobox-selected-and-show-data-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!