如果另一条记录具有相同的属性,如何防止商店更新记录?

时间:2015-04-23 22:41:16

标签: extjs store extjs4.2

我正在使用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

2 个答案:

答案 0 :(得分:0)

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

  

用户想要更新现有行并将'user_id'更改为   现有的或空的。我不想擦除这个记录   我想假设用户犯了一个错误而没有更新这条记录   想撤消他/她的更新。

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

答案 1 :(得分:0)

您可以在网格(行编辑器)validateedit中添加此验证,而不是商店的更新方法;见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
  }
}