首页 > 解决方案 > 在提供的 Ext.window.Window 中设置禁止编辑字段

问题描述

我需要设置禁止编辑 Ext.window.Window 中的文本字段,前提是下拉列表的值设置为推迟。

我正在尝试在 filterCombo 函数中执行此操作:

var inp = this.up ('window'). down ('# MyTextField');

inp.disable ();

但在控制台中我收到错误:TypeError: this.up is not a function

我究竟做错了什么?下面是我的代码:

var store = Ext.create('Ext.data.Store', {
            fields: ['order', 'id', 'name'],
            storeId: 'DoubleBookStore',
            data : [
                {"id": 23, name: "New", order_install: 1},
                {"id": 24, name: "In Work", order_install: 2},
                {"id": 29, name: "Postponed", order_install: 3},
                {"id": 34, name: "Shipped", order_install: 4},
                {"id": 31, name: "In_transit", order_install: 5}
            ]
        });

function filterCombo(combobox, records) {
    if(records.data.name == 'Postponed'){
      var inp = this.up('window').down('#MyTextField');
      console.log(inp); 
      inp.disable();
    }
    index = records.data.order_install;
    store = combobox.getStore();
    store.clearFilter();

    store.filterBy(
                function(record) {
                    if ((record.internalId == index - 1) || (record.internalId == index) || (record.internalId == index + 1)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            );
};

var window = Ext.create('Ext.window.Window', {
    title: 'Приложение',
    width: 300,
    height: 200,
    items:[{
                xtype: 'combobox',
                fieldLabel: 'Status',
                name: 'status',
                store: store,
                valueField: 'id',
                displayField: 'name',
                typeAhead: true,
                queryMode: 'local',
                value: 24,
                listeners: {
                select : function(combo, records) {
                    filterCombo(combo, records);
                    }
                }

            },
            {
                        xtype: 'textfield',
                        fieldLabel: 'Ваше имя:',
                        itemId:'MyTextField',
                        name: 'name'
            }]
});
window.show();

标签: javascriptuser-interfaceextjs

解决方案


当您在定义时定义 filterCombo 方法时,它将 this 作为全局范围。这就是为什么没有 this.up 的原因,因为这里是全局的。

要使您的代码正常工作,您需要在调用函数时传递范围,只需替换

    listeners: {
            select : function(combo, records) {
                filterCombo(combo, records);
                }
            }

    listeners: {
            select : function(combo, records) {
                filterCombo.apply(this,[combo, records]);
                }
            }

注意在你的方法中使用 apply 来改变 this 的行为。


推荐阅读