PDA

View Full Version : i need update-delete operations or at least one fo them in my grid ...



masoud_tamizy
24 Jan 2012, 12:37 AM
i have a model and a store that have an ajax proxy . i read from server through ajax into grid and i need to write to MySQL-PHP through ajax & delete . my code is in below and i need your help for doing it ...


Ext.require([
'Ext.data.*',
'Ext.grid.*',
'Ext.panel.*',
'Ext.form.*',
'Ext.util.*',
'Ext.state.*',
'Ext.tip.*',
'Ext.Action'
]);


Ext.onReady(function() {
var types = Ext.data.Types; // allow shorthand type access
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'national_code', type: 'string' },
{ name: 'user_id', type: 'int' },
{ name: 'first_name', type: 'string' },
{ name: 'last_name', type: 'string' },
{ name: 'father_name', type: 'string' },
{ name: 'sh_sh', type: 'string' },
{ name: 'sh_s_sh', type: 'string' },
{ name: 'gender', type: 'string' },
{ name: 'birth_date', type: 'date' },
{ name: 'birth_location', type: 'string' },
{ name: 'email', type: 'email' },
{ name: 'home_address', type: 'string' },
{ name: 'home_phone', type: 'string' },
{ name: 'postal_code', type: 'string' },
{ name: 'job_title', type: 'string' },
{ name: 'job_address', type: 'string' },
{ name: 'job_phone', type: 'string' },
{ name: 'mobile', type: 'string' },
{ name: 'fax', type: 'string' },
{ name: 'comment', type: 'string' }
],
validations: [
{type: 'presence', field: 'national_code'},
{type: 'presence', field: 'first_name'},
{type: 'presence', field: 'last_name'},
{type: 'presence', field: 'father_name'},
{type: 'presence', field: 'gender'},
{type: 'presence', field: 'home_address'},
{type: 'presence', field: 'home_phone'},
{type: 'presence', field: 'postal_code'},
{type: 'length', field: 'first_name', min: 1, max:45},
{type: 'length', field: 'last_name', min: 1, max:45},
{type: 'length', field: 'father_name', min: 1 , max:45},
{type: 'inclusion', field: 'gender', list: ['Male', 'Female']},
{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}
]
});


var userstore = Ext.create('Ext.data.Store', {
id:'userstore',
model: 'User',
autoLoad: true,
autoSync: true,
proxy: {
type: 'ajax',
noCache:true,
api:{
create:'insert.php',
read: 'prepare.php',
update: 'update.php',
destroy: 'delete.php'
},
reader : {type:'json',root:'users',successProperty: 'success'},
writer:{type:'json',writeAllFields: true,/*root:'users',*/successProperty: 'success'},
listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show({
title: 'REMOTE EXCEPTION',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
}
},
listeners: {
write: function(proxy, operation){
if (operation.action == 'destroy') {
alert('destroy operation selected');
}
Ext.example.msg(operation.action, operation.resultSmessageet);
}
}
});
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
errorSummary:true
});
var grid = Ext.create('Ext.grid.Panel', {
store: userstore,
selType: 'rowmodel',
loadMask: true,
plugins: [rowEditing],
columns: [
{ dataIndex: 'national_code',
width: 75,
hideable: false,
header:'کدملّي',
align:'right' ,
editor : null
},{ dataIndex:'first_name',
text:'نام',
width: 75,
align:'right',
editor: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'last_name',
text:'فاميل',
width: 75,
align:'right',
editor: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'father_name',
text:'نام پدر',
width: 75,
align:'right',
editor: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'sh_sh',
text:'شماره شناسنامه',
width: 90,
align:'right',
sortable:false,
field: {type:'textfield',vtype:'alphanum',allowBlank: false},
},{ dataIndex:'sh_s_sh',
width: 125,
text:'شماره سريال شناسنامه',
align:'right',
editor: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'gender',
text:'جنسيت',
width: 50,
align:'right',
editor: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'birth_date',
text:'تاريخ تولذ',
width: 75,
hidden:true,
renderer : Ext.util.Format.dateRenderer('d/m/Y'),
editor: {xtype:'datefield',allowBlank: false}
},{ dataIndex:'birth_location',
text:'محل تولد',
width: 100,
hidden:true,
align:'right',
field: {type:'textfield',allowBlank: false}
},{ dataIndex:'email',
text:'پست الکترونيک',
width: 150,
hidden:true,
align:'left',
field: {type:'textfield',vtype:'email',allowBlank:true}
},{ dataIndex:'home_address',
text:'آدرس منزل',
width: 175,
align:'right',
field: {type:'textfield',allowBlank: true}
},{ dataIndex:'home_phone',
text:'تلفن منزل',
width: 80,
align:'right',
field: {type:'textfield',allowBlank: false}
},{ dataIndex:'postal_code',
text:'کد پستي',
width: 75,
align:'right',
field: {type:'textfield',allowBlank: false}
},{ dataIndex:'job_title',
text:'شغل',
align:'right',
hidden:true,
field: {type:'textfield',allowBlank: true}
},{ dataIndex:'job_address',
text:'آدرس محل کار',
align:'right',
hidden:true,
field: {type:'textfield',allowBlank: true}
},{ dataIndex:'job_phone',
text:'تلفن محل کار',
align:'right',
width: 80,
hidden:true,
field: {type:'textfield',allowBlank: true}
},{ dataIndex:'mobile',
text:'تلفن همراه',
width: 80,
align:'right',
field: {type:'textfield',allowBlank: true},
hidden:true
},{ dataIndex:'fax',
text:'فاکس',
width: 80,
align:'right',
hidden:true,
field: {type:'textfield',allowBlank: true}
},{ dataIndex:'comment',
text:'توضيحات',
align:'right',
hidden:true
}],
title: 'ليست کاربران',
tbar : [ {
itemId : 'remove',
text : 'حذف',
iconCls : 'remove',
handler : function() {
i need this code ...
},
}, {
text : 'ذخيره',
iconCls : 'save',
handler : function() {
i need this code ...
}
} ],
height:575,
width:1366,
viewConfig: {
forceFit: true
},
listeners: {
//.....................................
}
},
renderTo: Ext.getBody(),
tools: [{ type: 'refresh' }]
});
grid.on('edit', function(editor, e) {
// commit the changes right after editing finished
e.record.save();
//grid.getStore().sync();
console.log('in the name of god');});
});

chramer
24 Jan 2012, 1:17 AM
for removing you can use this :

userstore.remove(grid.getSelectionModel().getSelection());

the "save" button is pointless cause your store has "autoSync : true" so it saves automatically..

masoud_tamizy
24 Jan 2012, 2:06 AM
for removing you can use this :

userstore.remove(grid.getSelectionModel().getSelection());

the "save" button is pointless cause your store has "autoSync : true" so it saves automatically..

thank very much my fiend 'chramer' .
but it only remove one record from store and how can i delete it from MySQL i.e. sending a request to server (ajax) with a parameter (for example national_code) ?

chramer
24 Jan 2012, 2:19 AM
check out this example (http://docs.sencha.com/ext-js/4-0/#%21/example/writer/writer.html)..dig into the code, enable Firebug or Google Dev Tools, and you'll see the magic ;)

masoud_tamizy
25 Jan 2012, 7:21 AM
i looked this example , but i didn't understand completely how it works !
i understand that proxy send an get or post request with a json to server . but i couldn't implement it !!!
if you can explain for me How it works .
thanks .

masoud_tamizy
28 Jan 2012, 11:55 PM
i looked this example , but i didn't understand completely how it works !
i understand that proxy send an get or post request with a json to server . but i couldn't implement it !!!
if you can explain for me How it works .
thanks .
i implement it , but it still only send READ Request and not more !!!
i need update & insert & delete operations too ...

masoud_tamizy
29 Jan 2012, 11:44 PM
thanks every one help me :)
my problem solved ...
i i didn't config idProperty of my Model !!! so i encountered this problem B)