PDA

View Full Version : The added or updated record of the grid



wguo
5 Jul 2009, 11:09 PM
When I operate a grid, I need to know which row is added and which one is updated,how can I do ? Because before I commit them to the server, I will sign them with a flag, the added one mask 'ADD' and the updated one mask 'EDIT' . The result string is : {user:{userid:'',password:''},flag:'ADD'}.

Condor
5 Jul 2009, 11:16 PM
Does your record have an id?

In that case records with an id are updated records and records without and id (or with an auto-generated id) are added records.

ps. In Ext 3.0 added records have phantom:true.

wguo
5 Jul 2009, 11:31 PM
Do you mean the column model is [{id:''},{name:'userId'},{name:'password'}]. Here I can't have the column id. Or you mean another id?

wguo
5 Jul 2009, 11:33 PM
var records = this.getSelectionModel().getSelections();
// data array
var da = [];
// loop through records and create json with data
for(var i = 0; i < records.length; i++) {
var record = records[i];
var recData = Ext.apply({}, record.data);
// you can conditionally delete some props here if you need to
var jsonData = Ext.encode(recData);
da.push(jsonData);
}
var jsonString = '[' + da.join(',') + ']';
In the above , I used it to convert grid data to a json, do you have a better idea that I can insert the flag attribute to the records?

Condor
6 Jul 2009, 3:17 AM
No, I don't mean a column id, I mean a record id.

If you create a new record using:

var r = new store.recordType({field1: value1, field2: value2, ...}, 'NEW-' + (++Ext.data.Record.AUTO_ID));

Then you could use:

var records = this.getSelectionModel().getSelections();
var da = [];
for(var i = 0; i < records.length; i++) {
var record = records[i];
var recData = Ext.apply({}, record.data);
recData.flag = record.id.substr(0, 3) == 'NEW' ? 'ADD' : 'UPDATE';
var jsonData = Ext.encode(recData);
da.push(jsonData);
}
var jsonString = '[' + da.join(',') + ']';