如何在json-store中添加记录

Ami*_*rma 12 extjs

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' } ]

    }                                                    
});
Run Code Online (Sandbox Code Playgroud)

我想要的是在这个商店的最后添加数据,但我完全困惑,我做的是我添加以下代码但不工作.

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

但我不认为我的概念已被清除,请任何机构以某种方式表明如何做到这一点.

Llo*_*oyd 16

您需要定义记录类型,创建它并在其中,例如:

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

然后:

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

然后:

my_store.add(record);
my_store.commitChanges();
Run Code Online (Sandbox Code Playgroud)

请记住,当数据在商店中时,它的格式与您向下发送的格式不同,而是在Record对象中.


Roy*_*ker 6

查看recordTypeJsonStore中的房产.它是一个可以用作相关商店的记录构造函数的函数.像这样使用它:

var newRecord = new myStore.recordType(recordData, recordId);
myStore.add(newRecord);
Run Code Online (Sandbox Code Playgroud)


Ami*_*rma 5

我还想出了一个简单的解决方案:

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

这里我要添加的是这些监听器,因为当数据加载时它将创建记录类型,你可以添加字段作为你想要的数据