PDA

View Full Version : how can i implement CRUD on my Grid ?



masoud_tamizy
19 Jan 2012, 8:16 AM
Please help me about implementing CRUD with simple example without MVC .
i need it soon . tnx

mitchellsimoens
19 Jan 2012, 9:52 AM
http://docs.sencha.com/ext-js/4-0/#!/example/writer/writer.html

masoud_tamizy
28 Jan 2012, 11:39 PM
http://docs.sencha.com/ext-js/4-0/#!/example/writer/writer.html
thanks my friend . i see it & implement it on my project , but i only can do read operation and nothing .
how can i do insert or delete operations ?

josephkirubakaran
29 Jan 2012, 6:27 AM
Given example contain all CRUD operations. Can you provide your code piece?

masoud_tamizy
29 Jan 2012, 7:04 AM
Given example contain all CRUD operations. Can you provide your code piece?
my code , now do read operation correctly , but when i press update key of row-editor , it calls create operation !
and don't send destroy request too ...
my code is :


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


Ext.onReady(function() {


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: 'sh_sh', min: 1, max:10},
{type: 'length', field: 'home_phone', min: 11, max:11},
{type: 'length', field: 'job_phone' , min: 11, max:11},
{type: 'length', field: 'fax' , min: 11, max:11},
{type: 'length', field: 'postal_code', min: 10, max:10},
{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: ['مذکر', 'مونث']}
]
});
var sex = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [
{"abbr":"M", "name":"مذکر"},
{"abbr":"F", "name":"مونث"}
]
});
var userstore = Ext.create('Ext.data.Store', {
id:'userstore',
model: 'User',
autoLoad: true,
autoSync: true,
proxy: {
type: 'ajax',
noCache:true,
api:{
read: 'prepare.php',
create:'insert.php',
update: 'update.php',
destroy: 'delete.php'
},
reader : {type:'json',root:'users',successProperty: 'success',messageProperty: 'message'},
writer : {type:'json',root:'users',writeAllFields: true/*,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') {
//main.child('#form').setActiveRecord(null); // insert action to perform fired destroy
alert('destroy operation selected');
}
Ext.example.msg(operation.action, operation.resultSmessageet);
}
}
});
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
errorSummary: false
});
// Create the Grid
var grid = Ext.create('Ext.grid.Panel', {
store: userstore,
selType: 'rowmodel',
loadMask: true,
plugins: [rowEditing],
columns: [
{ dataIndex: 'national_code',
width: 75,
hideable: false,
header:'کدملّي',
sortable:false,
align:'right',
editor: {xtype:'numberfield',hideTrigger: true,allowBlank: false,mouseWheelEnabled: false,keyNavEnabled: false,blankText:"! کد ملي را وارد کنيد",allowDecimals:false,minLengthText:"! کد ملّي 10 رقم مي باشد",maxLengthText:"! کد ملّي 10 رقم مي باشد",minLength:10,maxLength:10,selectOnFocus:true}
},{ dataIndex:'first_name',
text:'نام',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false,blankText:"! نام را وارد کنيد",selectOnFocus:true}
},{ dataIndex:'last_name',
text:'فاميل',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false,blankText:"! نام خانوادگي را وارد کنيد",selectOnFocus:true}
},{ dataIndex:'father_name',
text:'نام پدر',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false,blankText:"! نام پدر را وارد کنيد",selectOnFocus:true}
},{ dataIndex:'sh_sh',
text:'شماره شناسنامه',
width: 90,
align:'right',
sortable:false,
field: {xtype:'numberfield',hideTrigger: true,allowBlank: true,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,selectOnFocus:true}
},{ dataIndex:'sh_s_sh',
width: 125,
text:'شماره سريال شناسنامه',
sortable:false,
align:'right',
field: {xtype:'textfield',allowBlank: true,selectOnFocus:true}
},{ dataIndex:'gender',
text:'جنسيت',
width: 50,
align:'right',
editor: {xtype:'combo',store: sex,editable:false,allowBlank: false,displayField: 'name',blankText:"! جنسيت را انتخاب کنيد"}
},{ dataIndex:'birth_date',
text:'تاريخ تولذ',
width: 75,
align:'right',
hidden:true,
renderer : Ext.util.Format.dateRenderer('d/m/Y'),
editor: {xtype:'datefield',allowBlank: true,selectOnFocus:true}
},{ dataIndex:'birth_location',
text:'محل تولد',
width: 100,
hidden:true,
align:'right',
field: {type:'textfield',allowBlank: true,selectOnFocus:true}
},{ dataIndex:'email',
text:'پست الکترونيک',
width: 150,
hidden:true,
align:'left',
field: {type:'textfield',vtype:'email',allowBlank:true,selectOnFocus:true}
},{ dataIndex:'home_address',
text:'آدرس منزل',
width: 175,
align:'right',
field: {type:'textfield',allowBlank: false,blankText:"! آدرس منزل را وارد کنيد",selectOnFocus:true}
},{ dataIndex:'home_phone',
text:'تلفن منزل',
width: 80,
align:'right',
field: {xtype:'numberfield',hideTrigger: true,allowBlank: false,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,blankText:"! تلفن منزل را وارد کنيد",selectOnFocus:true}
},{ dataIndex:'postal_code',
text:'کد پستي',
width: 75,
align:'right',
field: {xtype:'numberfield',hideTrigger: true,allowBlank: false,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,blankText:"! کد پستي را وارد کنيد",minLength:10,selectOnFocus:true}
},{ dataIndex:'job_title',
text:'شغل',
align:'right',
hidden:true,
field: {xtype:'textfield',allowBlank: true,selectOnFocus:true}
},{ dataIndex:'job_address',
text:'آدرس محل کار',
align:'right',
hidden:true,
field: {type:'textfield',allowBlank: true,selectOnFocus:true}
},{ dataIndex:'job_phone',
text:'تلفن محل کار',
align:'right',
width: 80,
hidden:true,
field: {xtype:'numberfield',hideTrigger: true,allowBlank: true,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,minLength:10,selectOnFocus:true}
},{ dataIndex:'mobile',
text:'تلفن همراه',
width: 80,
align:'right',
field: {xtype:'numberfield',hideTrigger: true,allowBlank: true,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,minLength:10,selectOnFocus:true},
hidden:true
},{ dataIndex:'fax',
text:'فاکس',
width: 80,
align:'right',
hidden:true,
field: {xtype:'numberfield',hideTrigger: true,allowBlank: true,mouseWheelEnabled: false,keyNavEnabled: false,allowDecimals:false,minLength:10,selectOnFocus:true}
},{ dataIndex:'comment',
text:'توضيحات',
align:'right',
hidden:true
},
title: 'ليست کاربران',
tbar : [{
itemId : 'remove',
text : 'حذف',
iconCls: 'employee-remove',
handler : function() {
var select = grid.getSelectionModel();
rowEditing.cancelEdit();
userstore.remove(select.getSelection());
},
disabled : true
}, {
text : 'کاربر جديد',
iconCls: 'employee-add',
handler : function() {
var record = Ext.create('User',{
national_code: '1234567890',
first_name: 'نام',
last_name: 'فاميل',
father_name: 'نام پدر',
gender: 'مذکر',
home_address:'آدرس منزل',
home_phone:'1234567890',
postal_code:'1234567890'
});
rowEditing.cancelEdit();
userstore.insert(0, record);
rowEditing.startEdit(0, 0);
}
} ],
height:575,
width:1366,
viewConfig: {
forceFit: true
}},
renderTo: Ext.getBody(),
tools: [{ type: 'refresh' }]
});
grid.getSelectionModel().on('selectionchange', function(selModel, selections){
grid.down('#remove').setDisabled(selections.length === 0);
});
});

vietits
29 Jan 2012, 3:46 PM
You must set idProperty for your models. Default of this config is 'id' which I didn't see in your model definition.

masoud_tamizy
29 Jan 2012, 6:42 PM
You must set idProperty for your models. Default of this config is 'id' which I didn't see in your model definition.
thanks a lot :)