【问题标题】:Extjs GridPanel validationExtjs GridPanel 验证
【发布时间】:2011-09-06 11:00:26
【问题描述】:

当 IN1>OU1 或 IN2>OU2 时,我该如何做一个验证函数让我出错??

这是我的代码(带有 roweditor 插件的网格面板)

{
 xtype: 'gridpanel',
 height: 250,
 width: 400,
 title: 'My Grid Panel',
 columns: [
           {
             xtype: 'datecolumn',
             text: 'IN1',
             dataindex 'F02ORAIN1',
             field: {
               xtype: 'timefield',
               id 'editF02ORAIN1'
             }
           },
           {
             xtype: 'datecolumn',
             dataindex 'F02ORAOU1',
             text: 'OU1',
             field: {
               xtype: 'timefield',
               id 'editF02ORAOU1'
             }
           },
           {
              xtype: 'datecolumn',
              text: 'IN2',
              dataindex 'F02ORAIN2',
              field: {
                xtype: 'timefield',
                id 'editF02ORAIN2'
              }
           },
           {
             xtype: 'datecolumn',
             text: 'OU2',
             dataindex 'F02ORAOU2',
             field: {
               xtype: 'timefield',
               id 'editF02ORAOU2'
            }
          }
 ],
 plugins: [
    Ext.create('Ext.grid.plugin.RowEditing', {
    })
 ]
}

【问题讨论】:

    标签: javascript extjs extjs4


    【解决方案1】:

    我认为最好的方法是使用字段的validator config:

    // ...
    {
        xtype: 'datecolumn',
        text: 'IN1',
        dataIndex: 'F02ORAIN1',
        field: {
            xtype: 'timefield',
            id: 'editF02ORAIN1',
            validator: function(value) {
                if (!Ext.getCmp('editF02ORAOU1').getValue()) return true;
                if (this.getValue() > Ext.getCmp('editF02ORAOU1').getValue())
                  return 'IN1 should be less then OU1';
                return true;
            }
        }
    }, {
        xtype: 'datecolumn',
        dataIndex: 'F02ORAOU1',
        text: 'OU1',
        field: {
            xtype: 'timefield',
            id: 'editF02ORAOU1'
        }
    },
    // ...
    

    Here is demo

    【讨论】:

    • 我刚刚尝试过,但收到此错误:Ext.getCmp("editF02ORAOU1").getValue() 为空,但在我的网格中,所有行的所有列中都有数据!我该怎么办?
    • @jack,我已经更新了我的答案并添加了demo
    • 非常感谢你的榜样!!我尝试了一下,但我不知道为什么会收到此错误:值未定义 [Interrompi per questo errore] return Function.prototype....ctor.apply(Function.prototype, args);
    • 如果要在表单验证的时候验证,网格是否包含记录(什么情况下有效)?
    【解决方案2】:

    不要使用 getCmp(除非调试,否则永远不要这样做),而是从 Store 获取数据以与值进行比较。

    【讨论】:

    • 你能给我发个例子吗?
    • 我认为这应该是另一个答案下方的评论。这样(断章取义)感觉真的很奇怪。也许你没有足够的代表来发布 cmets...这里有一些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多