How to select an item in selectbox?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to select an item in selectbox?

alexander.volik
I have one selectbox, described as:

var list = [{name: "Kansas", id: 1},{name: "Arizona", id: 2}];
var selectBox = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel(list);
var controller = new qx.data.controller.List(model, selectBox, "name");


I want to select an item in selectbox, for example with id=2. Should I use setModelSelection method?
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

rsantiagopaz
yes, try

var list = [{name: "Kansas", id: 1},{name: "Arizona", id: 2}];
var selectBox = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel(list);
var controller = new qx.data.controller.List(model, selectBox, "name");
this.getRoot().add(selectBox);
selectBox.setModelSelection([model.getItem(1)]);

For a dynamic application check this example and thread
http://tinyurl.com/2v28sn2
http://qooxdoo.678.n2.nabble.com/SelectBox-set-selection-td4798272.html



But I just realize this alternative dont work:

var list = [{name: "Kansas", id: 1},{name: "Arizona", id: 2}];
var selectBox = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel(list);
var controller = new qx.data.controller.List(model, selectBox, "name");
this.getRoot().add(selectBox);
controller.setSelection([model.getItem(1)]);

Why dont work?
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

alexander.volik
rsantiagopaz wrote
yes, try

var list = [{name: "Kansas", id: 1},{name: "Arizona", id: 2}];
var selectBox = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel(list);
var controller = new qx.data.controller.List(model, selectBox, "name");
this.getRoot().add(selectBox);
selectBox.setModelSelection([model.getItem(1)]);
Hm... It works!
But I want to select an item with certain id - I don't know item index in the model array. What should I do?
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

rsantiagopaz
check the example
http://tinyurl.com/2v28sn2
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

alexander.volik
Yes, good example, I have already seen it. But my case is little different. As I use .addTarget method of qx.data.controller.Object, it needs to pick target object and it's property (for example property "value" for the textBox): addTarget(Object targetObject, String targetProperty, String sourceProperty, Boolean bidirectional?, Map options?, Map reverseOptions?) What the value should I pick as a targetProperty parameter in case of SelectBox?
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

rsantiagopaz
I can't understand your case. Give me a little example for the Playground
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

alexander.volik
My example is here
http://tinyurl.com/2uvvegk
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

rsantiagopaz
try

var list = [{name: "Arizona", id: 1},{name: "Kansas", id: 2}];
var selectBox = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel(list);
var controller = new qx.data.controller.List(model, selectBox, "name");
this.getRoot().add(selectBox);

var data = {state:2};
var formModel = qx.data.marshal.Json.createModel(data, true);
var formController = new qx.data.controller.Object(formModel);
     
      // how to bind model "state" with selectbox model "id"
formController.addTarget(controller, "selection[0]", "state", true, {converter: function(data){
                for (var i = 0; i < model.getLength(); i++) {
                        if (model.getItem(i).getId() == data) return model.getItem(i);
                }
        }}, {converter: function(data){
                return data.getId();
        }});
     
formModel.addListener("changeBubble", function(event){
          // function for saving in database user selected state
}, this);
Reply | Threaded
Open this post in threaded view
|

Re: How to select an item in selectbox?

alexander.volik
This post was updated on .
I have a problem with this code http://tinyurl.com/2u6osht 
I want to make bidirectional binding a of selectbox with a form controller. I have inserted alert() into the converter function of the form controller .addTarget method to debug. Please, try to change the selection in the selectbox. It causes 3 alert windows: 2 with the model item and the third with the model item id. Why so? Why not only one alert?