在 ext js 中的按钮单击事件上从网格和数据库中删除记录
我正在使用 ext js 设计器和 ruby on Rails。 我想在按钮单击事件上从网格和数据库中删除记录。 谁能帮助我吗? 谢谢...
以下是我的网格的代码。
xtype: 'grid',
title: 'Products',
store: 'productMaster',
height: 176,
id:'mygrid',
name:'mygrid',
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: function(sm, row, rec) {
Ext.getCmp("myform").getForm().loadRecord(rec);
}
}
}),
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'name',
header: 'name',
sortable: true,
width: 100
},
{
xtype: 'gridcolumn',
dataIndex: 'price',
header: 'price',
sortable: true,
width: 100
},
{
xtype: 'gridcolumn',
dataIndex: 'category',
header: 'category',
sortable: true,
width: 100
},
以下是我的删除按钮代码,
bbar: {
xtype: 'toolbar',
height: 30,
items: [
{
xtype: 'button',
text: 'Delete',
width: 100,
height: 30,
id:'btnDelete',
handler: function() {
//alert('trying to delete the record...');
var store = Ext.getCmp("mygrid").getStore();
store.removeAt(store.getCount()-1);
}
由于此处理程序函数,最后一条记录 ID 被删除,但它是从商店中删除而不是从数据库中删除。我也希望从数据库中删除该记录...
谢谢...
i am using ext js designer with ruby on rails.
i want to delete a record from my grid and database on button click event.
can anyone help me?
thanks...
following is the code of my grid.
xtype: 'grid',
title: 'Products',
store: 'productMaster',
height: 176,
id:'mygrid',
name:'mygrid',
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: function(sm, row, rec) {
Ext.getCmp("myform").getForm().loadRecord(rec);
}
}
}),
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'name',
header: 'name',
sortable: true,
width: 100
},
{
xtype: 'gridcolumn',
dataIndex: 'price',
header: 'price',
sortable: true,
width: 100
},
{
xtype: 'gridcolumn',
dataIndex: 'category',
header: 'category',
sortable: true,
width: 100
},
and following is my code of delete button
bbar: {
xtype: 'toolbar',
height: 30,
items: [
{
xtype: 'button',
text: 'Delete',
width: 100,
height: 30,
id:'btnDelete',
handler: function() {
//alert('trying to delete the record...');
var store = Ext.getCmp("mygrid").getStore();
store.removeAt(store.getCount()-1);
}
due to this handler function the last record id deleted but it is deleted from the store not fro the database.i want the record to be deleted from the database also...
thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我们开始之前:我注意到您的网格的配置对象
本身禁止任何编辑功能和服务器交互(除了读取存储之外),因为您正在实例化一个Ext.grid.GridPanel,而(我认为)您需要一个Ext.grid.EditorGridPanel。为了使以下所有内容正常运行,您应该将此行更改为:
我要检查的第一件事是商店有一个 id 配置属性,并且它是商店阅读器的配置列。如果存储无法识别该记录,则会认为该记录不存在,并且不会向服务器生成请求。
第二件事要检查:您的商店是否已配置为生成服务器请求?也就是说,你的商店配置对象中有这样一行吗?
接下来,您是否在商店配置对象中定义了“writer”属性?
商店配置对象应该有一个proxy和一个writer属性,以便在删除时生成正确的服务器请求。
要为存储指定写入器,您只需编写(对于常见的 Json 写入器):
并且存储将向服务器发送所有适当的写入请求。
另一个痛苦的(我认为没有详细记录)问题可能是商店阅读器中存在必填字段。默认情况下所有列都是必填的;要将列标记为“非强制”,您必须指定(在商店读取器列数组中):
可能,当您向网格添加新记录时,并非所有列都会被填充。
在填充所有必填列之前,商店不会生成服务器创建请求。然后,如果您删除未保存的记录,存储将不会生成销毁请求,因为它会假设该记录“不存在”。
Before we start: I noticed the configuration object for your grid has
This, by itself, forbids any editing capability and server interactions (besides reading the store), because you are instancing an Ext.grid.GridPanel, while (I think) you need an Ext.grid.EditorGridPanel. For all of the following to run properly, you should change this line to:
First thing I would check is that the store has an id configuration attribute, and it is a configured column of the store reader. If the store cannot identify the record, it will think that the record does not exist, and will not generate a request to the server.
Second thing to check: has your store been configured to generate server requests? That is, is there a line like this in your store config object?
Next, have you defined the "writer" attribute in your store configuration object?
The store config object should have a proxy and a writer attribute to generate proper server requests upon deletion.
To specify a writer for the store you may simply write (for a common Json writer):
and the store will send all the appropriate write requests to the server.
Another painful (not well documented, I think) issue could be the presence of mandatory fields in the store reader. All columns are mandatory by default; to mark a column as "non-mandatory" you must specify (in the store reader column array):
Probably, when you add a new record to the grid, not all of the columns will be populated.
The store will not generate a server create request until all mandatory columns are populated. Then, if you delete an unsaved record, the store will not generate the destroy request because it will assume that the record "does not exist".