PDA

View Full Version : why CRUD(Grid row-edit) alwasy send create instead of update ?



masoud_tamizy
4 Feb 2012, 12:14 AM
i create a row-edit grid (CRUD) . but it sends request as create operation always instead of update (when i press update button) . how can i do it (update) ?

Farish
4 Feb 2012, 12:49 AM
this happens mostly when you are not assigning an id to your records. If you are not, you need to add an id field to your store model:


{name: 'id', type: 'int'}

and assign a unique ID to your records. Newly created records are assigned ID '0' and if you do not have an id field, it might confuse an edited record as a new one. if this doesn't answer your question, then post the code of your store and grid.

vietits
4 Feb 2012, 1:07 AM
In case your record id is not 'id', then you can use idProperty to set record id field when defining your model.

masoud_tamizy
4 Feb 2012, 1:27 AM
thanks , but now , when i press update button in row-edit , happens nothig !!!

Farish
4 Feb 2012, 1:54 AM
without looking at the code, its difficult to say.

masoud_tamizy
4 Feb 2012, 2:16 AM
Ext.require([
'Ext.data.*',
'Ext.grid.*',
'Ext.panel.*',
'Ext.form.*',
'Ext.util.*',
'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' }
],
idProperty:"user_id",
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: '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: 'PHP/user_real/read.php',
create:'PHP/user_real/create.php',
update: 'PHP/user_real/update.php',
destroy: 'PHP/user_real/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
});

var grid = Ext.create('Ext.grid.Panel', {
listeners: {

},
store: userstore,
selType: 'rowmodel',// 'cellmodel'
loadMask: true,
plugins: [rowEditing],
columns: [
{ dataIndex: 'national_code',
width: 75,
hideable: false,
header:'??????',
sortable:false,
align:'right',
editor: { id:'national',
xtype:'numberfield',
hideTrigger: true,
allowBlank: false,
mouseWheelEnabled: false,
keyNavEnabled: false,
allowDecimals:false,
minLengthText:"! ?? ???? 10 ??? ?? ????",
maxLength:10,minLength:10,
selectOnFocus:true,
enforceMaxLength:true,
listeners:{
blur:function(me){
var code=me.getValue();
Ext.Ajax.request({
url: 'php/user_real/national.php',
params: 'code='+code,
success: function(response) {
if (response.responseText==1){
me.focus(true);
alert('! ?????? ?????? ??');
}
},
failure: function() {/* do some things on failure*/}
});
}
}
}
},{ dataIndex:'first_name',
text:'???',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'last_name',
text:'?????',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'father_name',
text:'??? ???',
width: 75,
align:'right',
field: {xtype:'textfield',allowBlank: false}
},{ dataIndex:'sh_sh',
text:'????? ????????',
width: 90,
align:'right',
sortable:false,
field: {xtype:'numberfield',hideTrigger: true,allowBlank: 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}
},{ 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,selectOnFocus:true}
},{ dataIndex:'home_phone',
text:'???? ????',
width: 80,
align:'right',
field: {xtype:'numberfield',hideTrigger: true,allowBlank: false}
},{ dataIndex:'postal_code',
text:'?? ????',
width: 75,
align:'right',
field: {xtype:'numberfield',hideTrigger: 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}
},{ dataIndex:'mobile',
text:'???? ?????',
width: 80,
align:'right',
field: {xtype:'numberfield',hideTrigger: true,allowBlank: true},
hidden:true
},{ dataIndex:'fax',
text:'????',
width: 80,
align:'right',
hidden:true,
field: {xtype:'numberfield',hideTrigger: true,allowBlank: 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.save();
//insert at top
userstore.insert(0, record);
//edit at row 0
rowEditing.startEdit(0, 0);
//Ext.example.msg('Done','in the name of god');
}
} ],
height:575,
width:1366,//viewConfig: {forceFit: true},
listeners: {
select: function(rowModel, record) {
var code = record.get('national_code');
var id = record.get('user_id');
console.log('????? ????? ?????? ?? :',code);
console.log('????? ?????? :',id);
},
selectionchange: function(selModel, selected) {
var rec = grid.getSelectionModel().getSelection()[0];
if (rec) {
console.log("Sell " + rec.get('last_name'));
} else {
console.log('Please select a company from the grid');
}
}
},
renderTo: Ext.getBody(),
tools: [{ type: 'refresh' }]
});
grid.getSelectionModel().on('selectionchange', function(selModel, selections){
grid.down('#remove').setDisabled(selections.length === 0);
});

persian = (function() {
return {
init: function() {
Ext.tip.QuickTipManager.init();
Ext.Ajax.request({
url: "JS/fa.js",
success: this.onSuccess,
failure: this.onFailure,
scope: this
});
},
onSuccess: function(response, opts) {
eval(response.responseText);
//Ext.Msg.alert('Status', 'Changes saved successfully.');
//this.setupDemo();
},
onFailure: function() {
Ext.Msg.alert('???? ???????', '! ???????? ???? ????? ?? ??? ????? ??? ???');
//this.setupDemo();
}
};
})();
persian.init();
});

masoud_tamizy
4 Feb 2012, 6:49 AM
:-/

Farish
4 Feb 2012, 7:21 AM
I am sorry i couldnt find anything wrong in your code. Try without using idProperty and changing ur user_id field to 'id'. Although it should work with idProperty but just give it a try.

masoud_tamizy
4 Feb 2012, 7:31 AM
i did it , but didn't work again !
i saw this example :
http://docs.sencha.com/ext-js/4-0/#!...er/writer.html (http://docs.sencha.com/ext-js/4-0/#%21/example/writer/writer.html)
but i don't have anything more ... for update !!!

can anyone help me ???

masoud_tamizy
5 Feb 2012, 7:00 AM
Please Helppppppppppp !!! :-|

vietits
5 Feb 2012, 8:27 PM
I have tried your code and it worked well:
- add new record: call create.php
- edit existing record: call update.php
- remove record: call delete.php

masoud_tamizy
7 Feb 2012, 5:45 AM
finally , i find it , but how can i correct it ?
when i change my proxy such this :
api : {
read: 'PHP/prepare.php',
create:'PHP/user_real/create.php',
update: 'PHP/user_real/update.php',
destroy: 'PHP/user_real/destroy.php'
}
it works correctly , but when i change 'PHP/prepare.php' to "PHP/user_real/read.php" , update doesn't work !!!
prepare.php is :


<?php $hostname="127.0.0.1";$username="root";$passwd="";$dbname="cash";$port=3306;$mysqli=mysqli_init(); // create mysqli object$mysqli->options(MYSQLI_INIT_COMMAND, "use cash;");$mysqli->real_connect($hostname,$username,$passwd,$dbname,$port);$in_national_code="";$in_first_name="";$in_last_name="";$in_father_name="";$in_sh_sh="";$in_sh_s_sh="";if(mysqli_connect_errno()){ echo "<p>Sorry, no connection! ", mysqli_connect_error(), "</p>\n"; exit();}else{ $query="call select_user_real('".$in_national_code."','".$in_first_name."','".$in_last_name."','".$in_father_name."','".$in_sh_sh."','".$in_sh_s_sh."')"; if($result=$mysqli->query($query)) { $num_row=$result->num_rows; $num_col=$result->field_count; $json="{\"success\": true,\"users\":["; $flag=false; while ($row=$result->fetch_object()) { if ($flag){ $json.=","; } $flag=true; $national_code=$row->national_code; $user_id=$row->user_id; $first_name=$row->first_name; $last_name=$row->last_name; $father_name=$row->father_name; $sh_sh=$row->sh_sh; $sh_s_sh=$row->sh_s_sh; $gender=$row->gender; if($gender=="M"){$gender="????";}elseif($gender=="F"){$gender="????";} $birth_date=$row->birth_date; $birth_location=$row->birth_location; $email=$row->email;//echo $email; $home_address=$row->home_address; $home_phone=$row->home_phone;//echo $home_phone; $postal_code=$row->postal_code;//echo $postal_code; $job_title=$row->job_title;//echo $job_title; $job_address=$row->job_address; $job_phone=$row->job_phone; $mobile=$row->mobile;//echo $mobile; $fax=$row->fax;//echo $fax; $comment=$row->comment;//echo $comment; $json.="{\"national_code\":\"".$national_code."\",\"user_id\":\"".$user_id."\",\"first_name\":\"".$first_name."\",\"last_name\":\"".$last_name."\",\"father_name\":\"".$father_name."\",\"sh_sh\":\"".$sh_sh."\",\"sh_s_sh\":\"".$sh_s_sh."\",\"gender\":\"".$gender."\",\"birth_date\":\"".$birth_date."\",\"birth_location\":\"".$birth_location."\",\"email\":\"".$email."\",\"home_address\":\"".$home_address."\",\"home_phone\":\"".$home_phone."\",\"postal_code\":\"".$postal_code."\",\"job_title\":\"".$job_title."\",\"job_address\":\"".$job_address."\",\"job_phone\":\"".$job_phone."\",\"mobile\":\"".$mobile."\",\"fax\":\"".$fax."\",\"comment\":\"".$comment."\"}";//echo $json,"<br>"; } $json.="]}"; echo $json; } else { echo $mysqli->error," ",$mysqli->errno; $result->close(); $mysqli->close(); }}?>

and read.php is :


<?phprequire_once("../SQL.php");$sql=new SQL();$in_national_code="";$in_first_name="";$in_last_name="";$in_father_name="";$in_sh_sh="";$in_sh_s_sh="";$query="call select_user_real('".$in_national_code."','".$in_first_name."','".$in_last_name."','".$in_father_name."','".$in_sh_sh."','".$in_sh_s_sh."')";$result=$sql->queryObjectArray($query);if($result){ echo "{\"success\": true,\"users\":".json_encode($result)."}";}?>
SQL.php (a class for execute mysql commands) is :


<?phpclass SQL { protected $mysqli; function __construct() {
require_once('password.php'); $this->mysqli=mysqli_init(); // create mysqli object //$this->mysqli->options(MYSQLI_INIT_COMMAND, "use cash;"); //$this->mysqli->ssl_set("key", "cert", "ca", "capath", "cipher"); // set SSL options (optional) //$this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10); // set additonal options (optional) //$this->mysqli = @new mysqli($host,$user,$pass,$db,$port); // can be used instead of below line $this->mysqli->real_connect($host,$user,$pass,$db,$port); // create connection to MySQL server if(mysqli_connect_errno()) { // test whether connection OK printf("<p>Sorry, no connection! %s\n</p>",mysqli_connect_error()); // have to report a proper JSON message $this->mysqli = FALSE; exit(); } } // destructor (delete object) function __destruct() { $this->close(); } // explicitly terminate object/connection function close() { if($this->mysqli) $this->mysqli->close(); $this->mysqli = FALSE; } // execute SELECT return object field function queryObjectArray($sql) { if($result = $this->mysqli->query($sql)) { if($result->num_rows) { while($row = $result->fetch_object()) $result_array[] = $row; return $result_array; }else return FALSE; } else { printf("<p>Error: %s</p>\n", $this->mysqli->error); return FALSE; } } // execute SELECT return individual value // Note: return value for error is -1 (not 0)! function querySingleItem($sql) { if ($result = $this->mysqli->query($sql)) { if ($row=$result->fetch_array()) { $result->close(); return $row[0]; } else { return -1; } } else { printf("<p>Error: %s</p>\n", $this->mysqli->error); return -1; } } // execute SQL command without result (INSERT, DELETE, etc.) function execute($sql) { if ($this->mysqli->real_query($sql)) { return TRUE; } else { print $this->mysqli->error; return FALSE; } } // return insert_id function insertId() { return $this->mysqli->insert_id; }}?>

BastianKrones
24 Apr 2013, 6:39 AM
Hi masoud_tamizy,
your proxy-type is "ajax", you need to change it to "REST" which will send you a "PUT" => Updating your data.

For my solution follow this link:
http://www.sencha.com/forum/showthread.php?261899-GRID-CRUD-(REST)-gt-Only-able-to-get-quot-GET-quot-and-quot-POST-quot&p=960120#post960120

Hope, i could help you with it.