【问题标题】:How to change value of File Field in ExtJS?如何更改 ExtJS 中文件字段的值?
【发布时间】:2013-10-31 07:12:41
【问题描述】:

我使用 ExtJS 4.2。我想在浏览文件时更改文件字段的值。这样做的原因是删除“C:\fakepath”字符串。任何帮助表示赞赏。

【问题讨论】:

标签: extjs filefield


【解决方案1】:

这个C:\fakepath来自浏览器,所以你看不到真实的路径,但是可以隐藏路径,只显示文件名。您可以通过扩展文件字段来做到这一点:

Ext.define('Ext.form.field.ExtFile', {
    extend: 'Ext.form.field.File',

    onFileChange: function(button, e, value) {
        var newValue = value.replace(/^c:\\fakepath\\/i, ''); // remove fakepath

        return this.callParent([ button, e, newValue ]);
    }
});

工作样本:http://jsfiddle.net/Qppjc/1/

【讨论】:

  • 我的 Architect 似乎有问题。 onFileChange 函数不采用 button 参数。我将其替换为filefield。然后点击浏览按钮出现JS错误:Cannot call method 'apply' of undefined
  • 我不使用 Architect,所以帮不了你。此Cannot call method 'apply' of undefined 错误表示未找到某些方法(例如在callParent 中使用apply)。您可以尝试拨打Ext.form.field.ExtFile.superclass.onFileChange.apply(this, [ button, e, newValue ]) 而不是this.callParent([ button, e, newValue ])
  • 感谢您的帮助:)
【解决方案2】:

我发现最好的方法是覆盖该字段。

这是 ExtJs 4 和 5 的解决方案(在 ExtJs 6 上也适用于我):http://code.tonytuan.org/2014/10/extjs-get-rid-of-fake-path-in-file-field.html

Ext.define('Ext.enhance.form.field.File', {
    override: 'Ext.form.field.File',     
    onFileChange: function(button, e, value) {
        this.duringFileSelect = true;
        Ext.form.field.File.superclass.setValue.call(this, value.replace(/^.*(\\|\/|\:)/, ''));
        delete this.duringFileSelect;
    }   
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-02
    • 2021-05-12
    • 2011-09-29
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多