PDA

View Full Version : Deleting a record on the grid using cellclick



mcruise
2 May 2011, 3:06 AM
Hi,
I would like to know how to delete a row/record from a grid on the click of a particular cells, which is marked with a delete icon image in it. I'm trying do so using 'cellclick' and since the grid doesn't have a selection model defined. The record has to be deleted in the database and also reflect in the grid.
The portion of the code used in the application is as follows,



var griduser = {
xtype: 'grid',
id: 'usertable',
store: storeuser,
columnLines:true,
stripeRows:true,
autoScroll:true,
trackMouseOver: false,
listeners: {
cellclick: function(o,row,col,e) {
if (col == 6) {
Ext.MessageBox.show({
title:'Delete?',
msg: 'Do you really want to delete this user?',
buttons: Ext.MessageBox.YESNO,
fn: function(btn){
if (btn == 'yes'){
var conn = new Ext.data.Connection();
conn.request({
url: 'user_delete.php',
params: {
action: 'delete',
id: e.record.id,
row: e.row
},
success: function(resp,opt) {
grid.getStore().remove(e.record);
Ext.Msg.alert('Success', 'Selected user has been deleted');
},
failure: function(resp,opt) {
Ext.Msg.alert('Error',
'Unable to delete movie');
}
});
}else{

}
}
});
}
}
},
columns: [
{header: "Login", dataIndex: 'login_id', width: 110},
{header: "Name", dataIndex: 'Name', width: 160},
{header: "Email Id", dataIndex: 'email', width: 210},
{header: "Group", dataIndex: 'group_name', width: 120},
{header: "Send Email", dataIndex: 'email', renderer: renderIcon, width: 70},
{header: "Edit", dataIndex: 'Edit', renderer: editIcon, width: 60},
{header: "Delete", dataIndex: 'Delete', renderer: deleteIcon, width: 60}
]
}
function deleteIcon(value, p, record) {
p.attr = 'ext:qtip="<b>Delete</b>"';
return (value, '<img src="images/icon_delete.png">') ;
}

pmatsumura
2 May 2011, 3:32 AM
Try

o.getStore().remove(e.record);instead of

grid.getStore().remove(e.record);

Maybe you'll have to reload the grid's store too.

mcruise
2 May 2011, 3:59 AM
Tried out with the changes mentioned, still the record clicked on does not get deleted. I've slightly modified the idea given for deleting a record with the delete button placed outside the grid and am not sure whether the values specified in the params: are correct. The php code being used is,



$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="users";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_query ("DELETE FROM users WHERE user_id = '".$_REQUEST['id']."'");

pmatsumura
2 May 2011, 4:07 AM
There is no way for me to know if the params you send to your backend are correct. I'd try to get the record using the rowIndex. Check the GridPanel API (http://dev.sencha.com/deploy/ext-3.3.1/docs/?class=Ext.grid.GridPanel) for more help on the cellclick event.

After your backend successfully deleted the row, you'll want to reload your store.

Please update your posts and wrap your code with the proper tags. It helps me and others read your code.