2010-08-19 54 views
0

我有一个JSON数据存储用于组合框选择工作正常,但是我希望将自定义记录添加到组合框并且它不工作。这里是我的代码:将自定义记录添加到组合框的JSON数据存储(ExtJS)

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

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

+0

我在http://stackoverflow.com/questions/8912160/issues-with在Ext.data.store类似的问题-ext-data-store-and-listview-in-sencha-touch-javascript – user1152262 2012-01-18 15:07:54

回答

2

首先,我想比如O H !!

从ExtJS的文档data.Store.add:

加(Ext.data.Record []记录):无效 记录添加到存储并触发add事件。要将记录从远程源添加到商店,请使用load({add:true})。另请参阅recordType并插入。

我假设你的ComboBox正在使用mode:'remote'。我相信你需要使用load方法并在那里给它添加{add:true}。

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类的“RECORDTYPE”属性中找到样本信息http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Store

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(); 
       } 
     } 
    }); 
+0

是否有可能只是ID的属性名称中的错误?在你放入TopicRecord对象的第一篇文章中,你有一个名为'ID'的属性,在这篇文章中你将它命名为'clientID',它应该是正确的。 – SBUJOLD 2010-08-23 17:22:31

相关问题