开发者

Populate a combobox using DirectStore

开发者 https://www.devze.com 2022-12-23 17:28 出处:网络
I\'m having issues while populating a combobox using DirectStore, the combobox is as follow: this.Combo = new Ext.form.ComboBox({

I'm having issues while populating a combobox using DirectStore, the combobox is as follow:

this.Combo = new Ext.form.ComboBox({
        fieldLabel: "Name",
        editable: false,
        triggerAction: 'all',
        mode: 'remote',
        store: new Ext.data.DirectStore({
            reader: new Ext.data.JsonReader({
                successProperty: 'success',
                idProperty: 'name',
                root: 'data',
                fields: [ 'name' ]
            }),
            autoLoad: true,
            api: { read: SS.MyApi.getNames }
        }),
        valueField: 'name',
        displayField: 'name'
    });

The returned json is:

[{"type":"rpc","tid":7,"action":"MyApi","method":"getNames","result":{"success":true,"data":{"name":["name1","name2","name3"]}}}]

And the c# code that generates the json

[DirectMethod]
public JObject getNames()
{
        List<string> names = new List<string>();
  names.Add("name1");
  names.Add("name2");
  names.Add("name3");

  JObject data = new JObject();
  d开发者_开发知识库ata.Add(new JProperty("name", names));
        return  new JObject(
                            new JProperty("success", true),
                            new JProperty("data", data)
                    );
}

The combobox is showing only one entry with "name1,name2,name3". How can i have one entry per name? Thanks in advance!


your returned json tells the combobox exactly what to do

"data":{"name":["name1","name2","name3"]}

i only has 1 field (name) in data and this has the value name1, name2, name3 your json has to look more like this:

data : [
   {
      name : "name1"
   }, {
      name : "name2"
   }, {
      name : "name3"
   }
]


Trick: I don't know how to mapping this yet, but you can convert it to specific type (anonymous) like this (using Linq):

var list = names.Select(s => new { name = s });

you return
JObject > JProperty data
     |----> JObject > JProperty name
          |----> List<string>

For me one of the JObject > JProperty is OPTIONAL, lets said is name... then you reader root is ok (data) and the field should is also mapping correctly with the transformation that we did.

In your code, you return
JObject > JProperty data
     |----> Enumerable<{name}>

"result":{"success":true,"data":[{"name":"name1"},{"name":"name2"},{"name":"name3"}]}

NOTE: Obviously if you know how to mapping string directly you don't have to convert it and will be better.

0

精彩评论

暂无评论...
验证码 换一张
取 消