ethraza
6 Jul 2007, 2:03 AM
I crossed the night trying to figure out how can I get new added rows in a grid and if exist a way... I simple don't know.
Only because I want a grid that handles insertion, deletion and update all using only the grid itself. (I mean with no forms to do that)
What I'm trying to do is:
In my Grid header I have 4 buttons (Insert, Remove, Reload and Commit changes), so I can edit the values that come from database, insert new rows and edit that rows and remove rows. After to do any of that things I click the "Commit changes" button to send to my php, via json, all the things to update, the new rows to insert and the ones to delete.
I have created a removedRows array to store the removed rows, but the same don't works for added rows because the new inserted Rows don't have an ID or values when add event is launched.
The fact is... Is easy to discovery the updated rows with getModifiedRecords(), because all changed itens get dirty. But the removed itens don't get dirty, they simple go away, and added itens don't get dirty too, even if a edit they cells, they get marked with the red triangle but they don't come in getModifiedRecords().
Please, somebody...
How can I discovery what rows was added to my grid?
And if possible... there is a way to discovery the removed rows too, without the need of keep my primaryKey of deleted rows?
Here is my code so far:
Ext.get('locbox').boxWrap();
// returns [id, Grid, dataStore]
var locgrid = ATW.jsonGrid(null,'loc_consulta_exec.php?sid=<?echo $_REQUEST['sid']?>',{start:0,limit:15},null,null,'loccon',15);
var deletedRows = new Array;
var gridHead = locgrid[1].getView().getHeaderPanel(true);
var tb = new Ext.Toolbar(gridHead,[
{
text: 'Incluir',
icon:'img/icons/default/add.png',
cls:'x-btn-text-icon',
handler: function(){
var ds = locgrid[1].getDataSource();
var nr = new Array;
Ext.each(ds.fields.keys, function(k){
nr[k]='';
});
locgrid[1].stopEditing();
locgrid[2].insert(0, new locgrid[2].reader.recordType(nr));
locgrid[1].startEditing(0, 0);
}
},{
text: 'Excluir',
icon:'img/icons/default/delete.png',
cls:'x-btn-text-icon',
handler: function(){
var sr = locgrid[1].getSelectionModel().getSelected();
deletedRows.push(sr.get('LocId'));
locgrid[2].remove(sr);
}
},'-',{
tooltip: 'Recarregar',
icon:'img/icons/default/arrow_refresh.png',
cls:'x-btn-icon',
handler: function(){
locgrid[1].getDataSource().reload();
//locgrid[1].getView().refresh();
}
},'-',{
text: 'Gravar Alterações',
icon:'img/icons/default/accept.png',
cls:'x-btn-text-icon',
handler: function(){
var dt = new Array;
Ext.each(locgrid[2].getModifiedRecords(), function(record){
dt.push(record.data);
});
//ajaxRequest('loc_consulta_grava.php','sid=<?echo $_REQUEST['sid']?>&update='+Ext.util.JSON.encode(dt)+'&remove='+Ext.util.JSON.encode(deletedRows));
alert('&update='+Ext.util.JSON.encode(dt)+'&remove='+Ext.util.JSON.encode(deletedRows));
locgrid[2].commitChanges();
}
}
]);
And attached is a screenshot of my grid to help to undestand me!
Thanks for any help!
Only because I want a grid that handles insertion, deletion and update all using only the grid itself. (I mean with no forms to do that)
What I'm trying to do is:
In my Grid header I have 4 buttons (Insert, Remove, Reload and Commit changes), so I can edit the values that come from database, insert new rows and edit that rows and remove rows. After to do any of that things I click the "Commit changes" button to send to my php, via json, all the things to update, the new rows to insert and the ones to delete.
I have created a removedRows array to store the removed rows, but the same don't works for added rows because the new inserted Rows don't have an ID or values when add event is launched.
The fact is... Is easy to discovery the updated rows with getModifiedRecords(), because all changed itens get dirty. But the removed itens don't get dirty, they simple go away, and added itens don't get dirty too, even if a edit they cells, they get marked with the red triangle but they don't come in getModifiedRecords().
Please, somebody...
How can I discovery what rows was added to my grid?
And if possible... there is a way to discovery the removed rows too, without the need of keep my primaryKey of deleted rows?
Here is my code so far:
Ext.get('locbox').boxWrap();
// returns [id, Grid, dataStore]
var locgrid = ATW.jsonGrid(null,'loc_consulta_exec.php?sid=<?echo $_REQUEST['sid']?>',{start:0,limit:15},null,null,'loccon',15);
var deletedRows = new Array;
var gridHead = locgrid[1].getView().getHeaderPanel(true);
var tb = new Ext.Toolbar(gridHead,[
{
text: 'Incluir',
icon:'img/icons/default/add.png',
cls:'x-btn-text-icon',
handler: function(){
var ds = locgrid[1].getDataSource();
var nr = new Array;
Ext.each(ds.fields.keys, function(k){
nr[k]='';
});
locgrid[1].stopEditing();
locgrid[2].insert(0, new locgrid[2].reader.recordType(nr));
locgrid[1].startEditing(0, 0);
}
},{
text: 'Excluir',
icon:'img/icons/default/delete.png',
cls:'x-btn-text-icon',
handler: function(){
var sr = locgrid[1].getSelectionModel().getSelected();
deletedRows.push(sr.get('LocId'));
locgrid[2].remove(sr);
}
},'-',{
tooltip: 'Recarregar',
icon:'img/icons/default/arrow_refresh.png',
cls:'x-btn-icon',
handler: function(){
locgrid[1].getDataSource().reload();
//locgrid[1].getView().refresh();
}
},'-',{
text: 'Gravar Alterações',
icon:'img/icons/default/accept.png',
cls:'x-btn-text-icon',
handler: function(){
var dt = new Array;
Ext.each(locgrid[2].getModifiedRecords(), function(record){
dt.push(record.data);
});
//ajaxRequest('loc_consulta_grava.php','sid=<?echo $_REQUEST['sid']?>&update='+Ext.util.JSON.encode(dt)+'&remove='+Ext.util.JSON.encode(deletedRows));
alert('&update='+Ext.util.JSON.encode(dt)+'&remove='+Ext.util.JSON.encode(deletedRows));
locgrid[2].commitChanges();
}
}
]);
And attached is a screenshot of my grid to help to undestand me!
Thanks for any help!