最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - panel drag and drop is not working in extjs 4.1 - Stack Overflow

programmeradmin2浏览0评论

This code is working in Extjs 4.0.2a but when converted to 4.1 it no longer works and gives an error

Uncaught TypeError: Cannot call method 'query' of undefined

Ext.onReady(function() {

    var panel = new Ext.Panel({
        renderTo: divtag,
        draggable: {
            insertProxy: false,
            onDrag: function(e) {
                var el = this.proxy.getEl();
                this.x = el.getLeft(true);
                this.y = el.getTop(true);
            },
            endDrag: function(e) {
                this.panel.setPosition(this.x, this.y);
            }
        },
        title: 'Panel',
        width: 200,
        height: 100,
        x: 20,
        y: 20
    });
});

This code is working in Extjs 4.0.2a but when converted to 4.1 it no longer works and gives an error

Uncaught TypeError: Cannot call method 'query' of undefined

Ext.onReady(function() {

    var panel = new Ext.Panel({
        renderTo: divtag,
        draggable: {
            insertProxy: false,
            onDrag: function(e) {
                var el = this.proxy.getEl();
                this.x = el.getLeft(true);
                this.y = el.getTop(true);
            },
            endDrag: function(e) {
                this.panel.setPosition(this.x, this.y);
            }
        },
        title: 'Panel',
        width: 200,
        height: 100,
        x: 20,
        y: 20
    });
});
Share Improve this question edited Aug 26, 2013 at 15:31 Adi Inbar 12.3k13 gold badges59 silver badges70 bronze badges asked Dec 19, 2011 at 8:02 jayeshjayesh 2,4927 gold badges44 silver badges80 bronze badges 1
  • I tried following code that works fine with Component but not with Panel :( and throws error : Uncaught TypeError: Object [object Object] has no method 'ensureAttachedToBody' Ext.onReady(function() { new Ext.Component({ constrain: true, title:'Test', floating: true, style: { backgroundColor: '#fff', border: '1px solid black' }, html: '<h1 style="cursor:move">The title</h1><p>The content</p>', draggable: { delegate:this.body } }).show(); }); – BeautifulWorld Commented Dec 23, 2011 at 5:58
Add a ment  | 

3 Answers 3

Reset to default 3 +50

Apparently there is a bug in this version of Ext. It wont work even if you try default D'n'D for panel like this:

Ext.onReady(function() {
    var panel = new Ext.Panel({
        renderTo: 'divtag',
        draggable: true,
        title: 'Panel',
        width: 200,
        height:100,
        x: 20,
        y: 20
    }); //panel.show(); });
});

I menage to patch the code to work the way you want it, this code should work:

Ext.onReady(function() {
    var panel = new Ext.Panel({
        renderTo: 'divtag',
        draggable: {
            insertProxy: false,
            onDrag: function(e) {
                var el = this.proxy.getEl();
                this.x = el.getX();
                this.y = el.getY();
            },
            endDrag: function(e) {
                panel.setPosition(this.x,this.y);
            },
            alignElWithMouse: function() {
                panel.dd.superclass.superclass.alignElWithMouse.apply(panel.dd, arguments);
                this.proxy.sync();
            }
        },
        title: 'Panel',
        width: 200,
        height:100,
        x: 20,
        y: 20
    }); //panel.show(); });
});

As a side note I should probably advice you not to do this anyway, because you can define your own DD for panel that you can use, and even better Ext already have one defined, so you can just override Ext panel to use default Ext.util.ComponentDragger, or in code, I advice you to do this:

Ext.override(Ext.panel.Panel, {
    initDraggable: function() {
        var me = this,
            ddConfig;

        if (!me.header) {
            me.updateHeader(true);
        }

        if (me.header) {
            ddConfig = Ext.applyIf({
                el: me.el,
                delegate: '#' + me.header.id
            }, me.draggable);

            // Add extra configs if Window is specified to be constrained
            if (me.constrain || me.constrainHeader) {
                ddConfig.constrain = me.constrain;
                ddConfig.constrainDelegate = me.constrainHeader;
                ddConfig.constrainTo = me.constrainTo || me.container;
            }

            me.dd = Ext.create('Ext.util.ComponentDragger', this, ddConfig);
            me.relayEvents(me.dd, ['dragstart', 'drag', 'dragend']);
        }
    }
});
var panel = Ext.create('Ext.panel.Panel', {
    id: 'test',
    renderTo: 'divtag',
    draggable: true,
    floating: true,
    title: 'Panel',
    width: 200,
    height:100,
    x: 20,
    y: 20
});

Code for a initDraggable function in panel override is taken from current stable version of Ext.window.Window.initDraggable method.

I was able to get it working in 4.1: you have to add quotes around the id of the renderTo element, like:

renderTo : 'divtag',

Without quotes it was looking for an undefined variable named divtag.

Once I ran that I got no errors, and then I just did panel.show() to render it.

Just a suggestion: a Window ponent is a specialized Panel that has a floating mixin - might be all you need.

发布评论

评论列表(0)

  1. 暂无评论