【问题标题】:How to prevent a store from updating a record, if another record has the same property?如果另一条记录具有相同的属性,如何防止商店更新记录?
【发布时间】:2015-07-02 08:35:46
【问题描述】:

我目前正在处理使用 RowEditing 插件的网格,当我单击链接到网格的“添加”按钮时,一条新的空记录将添加到网格绑定到的存储中。当我添加一个新行时,我可以检查用户尝试添加的值,如果这个 'user_id' 值为空或重复,我会提醒用户他的重复条目并将其从商店中删除。

我有一个我无法弄清楚的边缘情况 - 当用户想要更新现有行并将“user_id”更改为现有行或空行时,就会发生这种情况。我不想擦除这条记录,我只想假设用户犯了一个错误而不更新这条记录,我想撤消他/她的更新。

这是我店铺的更新方法:

update : function(com, record, successful, eOpts){
        var store = Ext.data.StoreManager.lookup('EpaymentKioskOptStores');
        var table_id = record.data.kiosk_user_id;
        var _try = false;
        var _wipe = false;
        //if the record is empty handle it
        if(table_id.trim() == null || table_id.trim() == undefined || table_id.trim() == ''){
            if(record.dirty){
                //restore to original value, and cancel update
                record.data.kiosk_user_id = record.modified.kiosk_user_id;
                _try = true;
            }else{
                store.remove(record);
                _wipe = true;
            }
        }
        //if the record's kiosk_user_id is not empty
        else{
            store.each(function(r){
                var temp = r.data.kiosk_user_id;
                //the record we are trying to enter is a new record
                if(record.phantom){
                    //make sure the record is new and if we find another with the same key we can wipe record
                    if(!r.phantom && (temp == table_id)){
                        store.remove(record);
                        _wipe = true;
                    }
                }
                else{
                    //somehow have to make sure the record and r arent the same
                    if((r.internalId != record.internalId) && (temp == table_id)){
                        record.data.kiosk_user_id = record.modified.kiosk_user_id;
                        _try = true;
                    }
                }
            });
        }
        if(_try){
            Ext.Msg.alert('Warning', 'Your change was reverted');
            store.commitChanges();
        }
        if(_wipe){
            Ext.Msg.alert('Alert', 'Your changes were discarded');
            store.commitChanges();
        }
    }

编辑:这是我为解决我的问题而实施的修复程序,它有点 hacky

【问题讨论】:

    标签: extjs store extjs4.2


    【解决方案1】:

    检查重复记录时,检查记录是否为phantom。如果记录有 phantom = true,则表示它是新添加的记录。对于用户尝试编辑的现有记录,record.phantom 将为 false。

    用户想要更新现有行并将“user_id”更改为 现有的或空的。我不想抹掉这个记录,我只是 想假设用户犯了错误而不更新这条记录,我 想要撤消他/她的更新。

    • 要检查一个空的 - 根据需要标记该字段。
    • 检查用户是否已更新为现有值:在服务器端检查,如果发现重复则返回错误消息。不让用户知道他/她的更改为什么不起作用是不好的

    【讨论】:

    • 完美,解决了记录是新的情况,但不能解决我们正在尝试更新的现有记录。
    • 没有解决我们正在尝试更新的现有记录哪一条?
    • 要检查一个空的 - 根据需要标记该字段。检查用户是否已更新到现有值:在服务器端检查,如果发现重复则返回错误消息。不让用户知道他/她的更改为什么不起作用是不好的
    • 我想在商店里做这一切,而不是在服务器端。我认为这个解决方案(在编辑中提供)虽然很老套,但很有效。让我知道你的想法
    • rec.data.kiosk_user_id = rec.modified.kiosk_user_id; ??
    【解决方案2】:

    您可以在网格(行编辑器)的 validateedit 中添加此验证,而不是 store 的 update 方法;见http://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.grid.plugin.RowEditing-event-validateedit

    listeners: {
      validateedit: function(editor, context) {
        var newId = context.newValues.userId;
        if (!newId) {
          editor.editor.getForm().findField('userId').markInvalid('User ID is required');
          return false;
        }
        // Now iterate through the store and check all userIds against newId and return false 
        // if you see a duplicate
      }
    }
    

    【讨论】:

    • 这是一个比我的“最终”版本更好的解决方案,将研究它并可能实施它。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2014-01-21
    • 2014-02-12
    • 2021-09-23
    相关资源
    最近更新 更多