PDA

View Full Version : adding new Records to a grid store (again i know)



hadi78
16 Oct 2009, 3:13 AM
hi,

i have a grid with the following properties:



ds = new Ext.data.GroupingStore({
proxy: new Ext.data.HttpProxy({
url: '/traffickers/get_cases',
method: 'GET'
}),
groupField: 'ssn',
reader: new Ext.data.JsonReader({
root: 'trafficking_cases',
idProperty: 'id',
totalProperty: 'results'
},[
{ name: 'id', mapping: 'trafficking_case.id' }
,{ name: 'ssn', mapping: 'trafficking_case.trafficker.trafficker_social_security_number.ssn' }
,{ name: 'firstname', mapping: 'trafficking_case.trafficker.firstname' }
,{ name: 'lastname', mapping: 'trafficking_case.trafficker.lastname' }
,{ name: 'date_of_birth', mapping: 'trafficking_case.trafficker.date_of_birth' }
,{ name: 'date_case_initiated', mapping: 'trafficking_case.date_case_initiated' }
,{ name: 'case_number', mapping: 'trafficking_case.case_number' }
,{ name: 'created', mapping: 'trafficking_case.created_at' }
,{ name: 'updated', mapping: 'trafficking_case.updated_at' }
]),
remoteSort: true, // turn on server-side sorting
sortInfo: {field: 'id', direction: 'ASC'}
});

var cm = new Ext.grid.ColumnModel([
{id: 'id', header: 'Id', width: 40, dataIndex: 'id', hidden: true}
,{ header: 'Last Name', dataIndex: 'lastname' }
,{ header: 'First Name', dataIndex: 'firstname' }
,{ header: 'Date of birth', dataIndex: 'date_of_birth' }
,{ header: 'Social security number', dataIndex: 'ssn', hidden: true }
,{ header: 'Date case initiated', dataIndex: 'date_case_initiated' }
//,{ header: 'Trial outcome', dataIndex: 'trial_outcome' }
,{ header: 'Case Number', dataIndex: 'case_number' }
,{ header: 'Created at', dataIndex: 'created' }
,{ header: 'Updated at', dataIndex: 'updated' }
]);

Now i am trying to add new records to the grid receiving from a request. The received json records looks like this:


{"results": 1, "trafficking_cases": [{"trafficking_case": {"updated_at": "2009-10-16T09:03:12Z", "country_of_residence_id": null, "trafficker_id": 2, "number_of_victims_involved_id": null, "date_case_initiated": null, "criminal_record": "", "case_number": "3333", "trafficker": {"trafficker_social_security_number_id": 2, "updated_at": "2009-10-16T09:02:27Z", "sex_id": null, "criminal_record": "", "country_of_birth_id": null, "trafficker_social_security_number": {"updated_at": "2009-10-16T09:02:27Z", "ssn": "SSN2", "id": 2, "created_at": "2009-10-16T09:02:27Z"}, "lastname": "yyyy", "id": 2, "firstname": "xxx ", "deleted": false, "identification_number": "3434", "date_of_birth": null, "created_at": "2009-10-16T09:02:27Z"}, "id": 3, "citizenships": [], "trial_outcome_id": null }}]}i try to add this record to the grid/store like this:


success: function(f,a){
var cases = a.result.trafficking_cases;
var records = new Array();
var TRecord = Ext.data.Record.create([ // creates a subclass of Ext.data.Record
{ name: 'id', mapping: 'trafficking_case.id' }
,{ name: 'ssn', mapping:'trafficking_case.trafficker.trafficker_social_security_number.ssn' }
,{ name: 'firstname', mapping: 'trafficking_case.trafficker.firstname' }
,{ name: 'lastname', mapping: 'trafficking_case.trafficker.lastname' }
,{ name: 'date_of_birth', mapping: 'trafficking_case.trafficker.date_of_birth' }
,{ name: 'date_case_initiated', mapping: 'trafficking_case.date_case_initiated' }
,{ name: 'trafficking_case.case_number', mapping: 'trafficking_case.case_number' }
,{ name: 'created_at', mapping: 'trafficking_case.created_at' }
,{ name: 'updated_at', mapping: 'trafficking_case.updated_at' }
]);

ds.removeAll();
for (var i=0; i<cases.length; i++) {

var newRecord = new TRecord(cases[i]);
records.push(Ext.data.Record.create(newRecord));
ds.add(newRecord);
}What now happens is that a row is added to the grid, but its EMPTY :(

any ideas? thx

Condor
16 Oct 2009, 3:32 AM
The 'mapping' is not used by the Record constructor. Only the reader knows how to create a record from mapped data.

You want:

var r = store.reader.readRecords(a.result);
store.add(r.records);
or simply:

store.loadData(a.result, true);

hadi78
16 Oct 2009, 3:37 AM
OMG, how awesome is this ;) you reduced my code to only 1 row and the best part: it WORKS ;)

thx a alot

Condor
16 Oct 2009, 3:43 AM
Just one warning:
You should NOT use this method to replace existing records (use only new record ids), because Ext doesn't handle this correctly (even though the API docs say can).

hadi78
16 Oct 2009, 3:47 AM
thx for the advice ;) and again thx for the good support on this forum