Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add records in json-store

Tags:

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

    }                                                    
});

What I want is to add data at the end for this store , but I am totally confused , what I did is I add the following code to it but not working.

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

But I do not think my concept are cleared ,Please any body show some way how to do it .

like image 629
Amit Sharma Avatar asked Aug 04 '10 12:08

Amit Sharma


3 Answers

You need to define a record type, create it and at it, e.g:

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

Then:

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

Then:

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

Remember by the time the data is in the store it's not in the same format as you sent it down but in Record objects instead.

like image 137
Lloyd Avatar answered Oct 27 '22 06:10

Lloyd


I have also figured out a simple solution to this:

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

Here what I have to add is this listeners as when the data loads it will create the record type and u can add fields as data as many as u want

like image 21
Amit Sharma Avatar answered Oct 27 '22 04:10

Amit Sharma


See the recordType property in the JsonStore. It's a function that can be used as a record constructor for the store in question. Use it like this:

var newRecord = new myStore.recordType(recordData, recordId);
myStore.add(newRecord);
like image 38
Roy Tinker Avatar answered Oct 27 '22 06:10

Roy Tinker