简体   繁体   English

如何从DOM元素获取Ext JS组件

[英]How to get Ext JS component from DOM element

Trying to create an inline edit form. 尝试创建内联编辑表单。 I have a form that looks like this: 我有一个看起来像这样的表格:

var editPic = "<img src='https://s3.amazonaws.com/bzimages/pencil.png' alt='edit' height='24' width='24' style='margin-left: 10px;'/>";
var submitPic = "<img id='submitPic' src='https://s3.amazonaws.com/bzimages/submitPic.png' alt='edit' height='24' width='24'/>";

Ext.define('BM.view.test.Edit', {
extend: 'Ext.form.Panel',
alias: 'widget.test-edit',

layout: 'anchor',
title: 'Edit Test',
defaultType: 'displayfield',

items: [
    {name: 'id', hidden: true},

    {
        name: 'name',
        fieldLabel: 'Name',
        afterSubTpl: editPic,
        cls: 'editable'
    },
    {
        name: 'nameEdit',
        fieldLabel: 'Name',
        xtype: 'textfield',
        hidden: true,
        cls: 'editMode',
        allowBlank: false,
        afterSubTpl: submitPic
    }
]
});

The controller looks like this (a lot of events): 控制器看起来像这样(很多事件):

init: function() {
    this.control({
        'test-edit > displayfield': {
            afterrender: this.showEditable
        },
        'test-edit': {
            afterrender: this.formRendered
        },
        'test-edit > field[cls=editMode]': {
            specialkey: this.editField,
            blur: this.outOfFocus
        }
    });
},

outOfFocus: function(field, event) {
    console.log('Lost focus');
    this.revertToDisplayField(field);
},

revertToDisplayField: function(field) {
    field.previousNode().show();
    field.hide();
},

formRendered: function(form) {
    Ext.get('submitPic').on('click', function (event, object) {
        var field = Ext.get(object).parent().parent().parent().parent();
        var cmp = Ext.ComponentQuery.query('test-edit > field[cls=editMode]');
    });
},

editField: function(field, e) {
    var value = field.value;
    if (e.getKey() === e.ENTER) {
        if (!field.allowBlank && Ext.isEmpty(value)){
            console.log('Not permitted!');
        } else {
            var record = Ext.ComponentQuery.query('test-edit')[0].getForm().getRecord();
            Ext.Ajax.request({
                url: '../webapp/tests/update',
                method:'Post',
                params: {
                    id: record.getId(),
                    fieldName: field.name,
                    fieldValue: field.value
                },
                store: record.store,
                success: function(response, t){
                    field.previousNode().setValue(value);
                    t.store.reload();
                    var text = response.responseText;
                    // process server response here
                    console.log('Update successful!');
                }
            });

        }
        this.revertToDisplayField(field);

    } else if (e.getKey() === e.ESC) {
        console.log('gave up');
        this.revertToDisplayField(field);
    }
},

showEditable: function(df) {
    df.getEl().on("click", handleClick, this, df);

    function handleClick(e, t, df){
        e.preventDefault();
        var editable = df.nextNode();
        editable.setValue(df.getValue());
        editable.show();
        editable.focus();
        df.hide();
    }
},

I'm using the 'afterSubTpl' config to add the edit icon, and the accept icon. 我正在使用“ afterSubTpl”配置添加编辑图标和接受图标。 I have listeners set up to listen on click events concerning them, but after they are clicked, I only have the element created by Ext.get('submitPic'). 我设置了侦听器来侦听与它们有关的单击事件,但是在单击它们之后,我只有由Ext.get('submitPic')创建的元素。 Now I want to have access to the the Ext field and form that surround it. 现在,我想访问Ext字段和围绕它的表格。 The parent method only brings back other DOM elements. 父方法仅带回其他DOM元素。 How do I connect between them? 我如何在它们之间建立联系? You can see what I tried in formRendered. 您可以看到我在formRendered中尝试过的内容。

I hope someone can clarify this little bit for me. 我希望有人可以为我澄清这一点。

You can get the component by id, but only if your component and its dom element have the same id (they usually do): 您可以通过id获得组件,但前提是您的组件及其dom元素具有相同的id(通常是这样):

Ext.getCmp(yourelement.id)

But this is not exactly good practice -- it would be better to set up your listeners so that the handler methods already have a reference to the component. 但这并不是一个很好的做法-最好设置您的侦听器,以便处理程序方法已经具有对该组件的引用。 For example, in your 'submitPic' component, you could define the click listener like this: 例如,在您的“ submitPic”组件中,您可以这样定义点击监听器:

var me = this;
me.on({
    click: function(arguments){
         var cmp = me;
         ...

});

Walk up the DOM tree until you find a component for the element's id: 走到DOM树,直到找到元素ID的组件:

 getCmpFromEl = function(el) {
    var body = Ext.getBody();
    var cmp;
    do {
        cmp = Ext.getCmp(el.id);
        el = el.parentNode;
    } while (!cmp && el !== body);
    return cmp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM