在 ext js 中的按钮单击事件上从网格和数据库中删除记录

发布于 2024-11-02 05:17:03 字数 2117 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-11-09 05:17:03

在我们开始之前:我注意到您的网格的配置对象

xtype:'grid'

本身禁止任何编辑功能和服务器交互(除了读取存储之外),因为您正在实例化一个Ext.grid.GridPanel,而(我认为)您需要一个Ext.grid.EditorGridPanel。为了使以下所有内容正常运行,您应该将此行更改为:

xtype:'editorgrid'

我要检查的第一件事是商店有一个 id 配置属性,并且它是商店阅读器的配置列。如果存储无法识别该记录,则会认为该记录不存在,并且不会向服务器生成请求。

第二件事要检查:您的商店是否已配置为生成服务器请求?也就是说,你的商店配置对象中有这样一行吗?

proxy: new Ext.data.HttpProxy({
    api:{
        read:'readscript.url',
        create:'insertscript.url',
        update:'updatescript.url',
        destroy:'deletescript.url'
    }
})

接下来,您是否在商店配置对象中定义了“writer”属性?
商店配置对象应该有一个proxy和一个writer属性,以便在删除时生成正确的服务器请求。

要为存储指定写入器,您只需编写(对于常见的 Json 写入器):

writer: new Ext.data.JsonWriter()

并且存储将向服务器发送所有适当的写入请求。

另一个痛苦的(我认为没有详细记录)问题可能是商店阅读器中存在必填字段。默认情况下所有列都是必填的;要将列标记为“非强制”,您必须指定(在商店读取器列数组中):

{name:'column_name', allowBlank:true, /* other stuff like 'type' etc. */ }

可能,当您向网格添加新记录时,并非所有列都会被填充。
在填充所有必填列之前,商店不会生成服务器创建请求。然后,如果您删除未保存的记录,存储将不会生成销毁请求,因为它会假设该记录“不存在”。

Before we start: I noticed the configuration object for your grid has

xtype:'grid'

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:

xtype:'editorgrid'

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?

proxy: new Ext.data.HttpProxy({
    api:{
        read:'readscript.url',
        create:'insertscript.url',
        update:'updatescript.url',
        destroy:'deletescript.url'
    }
})

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):

writer: new Ext.data.JsonWriter()

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):

{name:'column_name', allowBlank:true, /* other stuff like 'type' etc. */ }

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文