Eric Freitas
Eric Freitas

Reputation: 90

What is the best way to populate the combobox dojo (1.8) programmatically?

Hello I have sought the best way to populate a combobox with data from my database. I tried it this way, but I think there are better ways to do it in JavaScript.

Thank you all

var albumStore = new JsonRest({target:"/album", idAttribute:"id"});
var store = new Memory();

albumStore.query('').then(function(data) {

    json = new Array();
    for(i in data){
        item = {
            name: data[i].nome,
            id: data[i].id
        }
        json[i] = item;
    }
    store .setData(json);
})

props =  {
    required : true,
    missingMessage: 'campo obrigatório',
    store: store
}

Upvotes: 2

Views: 1237

Answers (2)

phusick
phusick

Reputation: 7352

You can use Array slice method to clone arrays:

var restStore = new JsonRest({ target: "/album/", idProperty: "Key" });
var memoryStore = new Memory({ idProperty: "Key" });

restStore.query().then(function(response) {
    memoryStore.setData(response.slice(0));
});

Please note, you can also use JsonRest store directly with ComboBox, if your REST server supports it. See how it works with fakeServer of sinon.js at this jsFiddle: http://jsfiddle.net/phusick/N8DqG/

Upvotes: 1

Assuming that store in your case is ItemFileWriteStore, replace the following code:

json = new Array();
                for(i in data){
                    item = {
                        name: data[i].nome,
                        id: data[i].id
                    }
                    json[i] = item;
                }
                store .setData(json);

with the newItem API of store as shown below

            for(i in data){
                item = {
                    store.newItem({id: data[i].id, name: data[i].name});
                }
            }

Similarly, there is API for deleting items from store.

store.deleteItem(item);

Upvotes: 0

Related Questions