Ext JS - Combo 只显示 SimpleStore 中每项的第一个字母?

3

你好,我有一个非常简单的Ext JS组合框,我只是想将其绑定到一个数组上。 以下是组合框的配置:

BPM.configs.ViewsCombo = {
    xtype: 'combo',
    emptyText: 'Select View',
    disableKeyFilter: true,
    triggerAction: 'all',
    displayField: 'name',
    mode: 'remote',
    render: function(combo) {
            this.store.load();
        }
    },
    store: new Ext.data.SimpleStore({
        proxy: new Ext.data.HttpProxy({
        url: '/Service.svc/GetUserViewNames',
            method: 'POST'
        }),
        root: 'GetUserViewNamesResult',
        fields: ['name']
    })
};

以下是Ajax调用的响应/JSON:

{"GetUserViewNamesResult":["something","tree"]}

但是当我查看组合框项目时,列表中只看到字母's'和't'。这是怎么回事?我的返回数组格式有问题吗?
非常感谢。

1
@29er,你可以将其发布为自己问题的答案。这将有助于其他人找到已解答的问题和解决方案。 - Abdel Raoof Olakara
3个回答

4

我发现结果应该像这样:{"GetUserViewNamesResult":[["something"],["tree"]]}

有点糟糕,因为现在我必须更改我的服务器端对象序列化的方式 :(


1

0

是的,ExtJs仍然没有一个能够处理字符串列表的读取器。在服务器端(至少在Java、C#等)当你尝试编组ENUM类型时,这通常是你会得到的。

我不得不编写自己的类,这个类在ExtJs 4.1 MVC样式中使用:

/**
 * This extends basic reader and is used for converting lists of enums (e.g. ['a', 'b', 'c']) into lists of objects:
 * [ {name: 'a'}, {name:'b'}, {name:'c'}]. All models using this type of reader must have a single field called name. Or you can
 * pass a config option call 'fieldName' that will be used.
 *
 * This assumes that the server returns a standard response in the form:
 * { result: {...., "someEnum" : ['a', 'b', 'c']},
 *   total: 10,
 *   success: true,
 *   msg: 'some message'
 * }
 */
Ext.define('MY.store.EnumReader', {
    extend: 'Ext.data.reader.Json',
    alias: 'reader.enum',

    //we find the Enum value which should be a list of strings and use the 'name' property
    getData: function(data) {
        var me = this;
        //console.dir(data);
        var prop = Ext.isEmpty(this.fieldName) ? 'name' : this.fieldName;
        console.log('Using the model property: \''+ prop +'\' to set each enum item in the array');
        try {
            var enumArray = me.getRoot(data);
            //console.dir(enumArray);
            if (!Ext.isArray(enumArray)){
                console.error("expecting array of string (i.e. enum)");
                throw new Exception('not an array of strings - enum');
            }
            var enumToObjArray = Array.map(enumArray, function(item){
                    var obj = {};
                    obj[prop] = item;
                    return obj;
                }
            );
            //console.dir(enumToObjArray);
            var nodes = me.root.split('.');
            var target = data;
            var temp = "data";
            Array.forEach(nodes, function(item, index, allItems){
                temp += "['" + item + "']";
            });
            temp += " = enumToObjArray";
            //console.log('****************' + temp + '*****************');
            //evil 'eval' method. What other choice do we have?
            eval(temp);
            //console.dir(data);
            return data;
        }
        catch(ex){
            console.error('coudln\'t parse json response: ' + response.responseText);
            return this.callParent(response);
        }
    }
}, function() {
    console.log(this.getName() + ' defined');
});

然后,如果您想在您的商店中使用这种类型的读取器,您可以添加:

requires: ['My.store.EnumReader'],
...

proxy: {
    type: 'ajax',
    url:  ...
    reader: {
            type: 'enum',

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接