将自定义记录添加到组合框的JSON数据存储(ExtJS)

时间:2010-08-19 13:01:18

标签: ajax extjs

我有一个用于组合框选择的JSON数据存储,它工作正常,但我希望在组合框中添加一个自定义记录,但它无法正常工作。这是我的代码:

注意:我删除了一些代码以便于阅读。

var ds = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: 'ajax.aspx?type=CR'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'clientId'
        }, [
            {name: 'name', mapping: 'clientName'},
            {name: 'address', mapping: 'clientAddress'}
        ])
    });

    // add the Other record

    // create a Record constructor from a description of the fields
    var TopicRecord = Ext.data.Record.create([ // creates a subclass of Ext.data.Record
        {name: "id"}, 
        {name: 'name'}, 
        {name: 'address'}
    ]);

    // create Record instance
    var myNewRecord = new TopicRecord({
        id: Ext.id(), 
        name: 'Other',
        address: 'Other'
    });

    ds.add(myNewRecord);
    ds.commitChanges();

    var carrierSearch = new Ext.form.ComboBox({
        // removed some code here
        onSelect: function(record){ 
            carrierSearch.setValue(record.data.name);
            document.getElementById(carrierIdHidden).value=record.id;
            fieldOnBlurPost(document.getElementById(id), page, true);
            carrierSearch.collapse();
        }
    });

为什么“//添加其他记录”(直到ds.commitChanges();)之前的部分没有添加我的自定义记录?

谢谢,

Domenic

3 个答案:

答案 0 :(得分:2)

首先,我想说 O-H !!

来自data.Store.add的ExtJS文档:

  

add(Ext.data.Record [] records):void   将记录添加到商店并触发添加事件。要从远程源向存储添加记录,请使用load({add:true})。另请参见recordType和insert。

我假设你的ComboBox正在使用模式:'remote'。我相信您需要使用加载方法并将其添加到{add:true}那里。

答案 1 :(得分:1)

我从未尝试过这个,但是从文档中我认为你不需要使用Ext.data.Record.Create()再次创建记录类型,因为数据存储区提供了一个读者,它应该在商店中可用。实际上,我的猜测是记录类型的id属性会创建一个与商店配置不匹配的不同类型的记录对象,因此会导致add函数失败。

尝试这样的事情:

var defaultData = {
    name: 'Other Name'
    address: 'Other Address'
};
var r = new ds.recordType(defaultData, Ext.id()); // create new record
ds.add(r);

我的基础是Ext.data.Store类http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Store的'recordType'属性中的示例信息

答案 2 :(得分:1)

当我在数据存储中添加记录时,它起作用了:

var ds = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: 'ajax.aspx?type=CR&searchtype=begins'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'clientId'
        }, [
            {name: 'name', mapping: 'clientName'},
            {name: 'address', mapping: 'clientAddress'}
        ]),
        listeners: {
                load: function() {

                    // add the Other record

                    // create a Record constructor from a description of the fields
                    var TopicRecord = Ext.data.Record.create([ // creates a subclass of Ext.data.Record
                        {name: "clientId"},
                        {name: 'name'}, 
                        {name: 'address'}
                    ]);

                    // create Record instance
                    var myNewRecord = new TopicRecord({
                        name: 'Other'
                    },'Other');

                    this.add(myNewRecord);
                    this.commitChanges();
                }
        }
    });