2010-08-04 48 views
12
var store = new Ext.data.JsonStore({ 
    id:'jfields', 
    totalProperty:'totalcount', 
    root:'rows', 
    url: 'data.php', 
    fields:[{ name:'jfields' }, 
      { name:'firstyear' , mapping :'firstyear' , type:'float' }, 
      { name:'secondyear', mapping :'secondyear', type:'float' }, 
      { name:'thirdyear' , mapping :'thirdyear' , type:'float' }, 
      { name:'fourthyear', mapping :'fourthyear', type:'float' }, 
      { name:'fifthyear' , mapping :'fifthyear' , type:'float' } ] 

    }              
}); 

我想要的是在此商店的最後添加數據,但我完全困惑,我所做的是將以下代碼添加到它但不工作。
如何在json-store中添加記錄

listeners: { 
    load : function(){ 
     BG_store.add([{"jfields":"Monthly","firstyear":22.99,"secondyear":21.88,"thirdyear":21.88,"fourthyear":22.99,"fifthyear":21.88}]); 
    } 
} 

但我不認爲我的概念被清除,請任何機構表現出一定的方式如何做到這一點。

回答

18

您需要定義一個記錄類型,創建它,並在它,如:

TaskLocation = Ext.data.Record.create([ 
    {name: "id", type: "string"}, 
    {name: "type", type: "string"}, 
    {name: "type_data", type: "string"}, 
    {name: "display_value", type: "string"} 
]); 

然後:

var record = new TaskLocation({ 
    id: Ext.id(), 
    type: "city", 
    type_data: "", 
    display_value: "Brighton" 
}); 

然後:

my_store.add(record); 
my_store.commitChanges(); 

的時候記住數據在商店中的格式與您發送它的格式不同,但在Record對象中。

+0

thanyou非常多LIoyd,我也想出了一種方法,我也加了我的答案,再一次謝謝你 – 2010-08-04 16:59:43

5

我也想通了一個簡單的解決這個:

listeners: { 
    load: function(xstore , record , option) { 
     var u = new xstore.recordType({ jfields : 'monthly' }); 
     xstore.insert(record.length, u); 
    } 
} 

在這裏我必須補充的是,這個監聽器當數據被載入它會創建記錄類型和u可以添加字段的數據很多都是你想要的

6

查看JsonStore中的recordType屬性。這是一個可以用作所討論商店的記錄構造函數的函數。像這樣使用它:

var newRecord = new myStore.recordType(recordData, recordId); 
myStore.add(newRecord);