Extjs:如何理解这个概念:添加、删除、添加实例

发布于 2024-10-28 19:15:05 字数 1030 浏览 7 评论 0原文

概念: 考虑有两个面板 AB,以及一个窗口 C,如下所示以下示例。窗口上的按钮在两个面板之间切换。

var A = new Ext.Panel({
    title: 'A'
});

var B = new Ext.Panel({
    title: 'B'
});

var C = new Ext.Window({
    layout: 'fit',
    width: 300,
    height: 300,
    items: A,
    buttons: [
    {
        text: 'Switch to A',
        handler: function() {
            C.removeAll(false);
            C.add(A);
            C.doLayout();
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.removeAll(false);
            C.add(B);
            C.doLayout();
        }
    }]
});
C.show();

这个概念非常简单:添加一个组件,删除它,然后再次添加相同的实例。

问题: 从 A 到 B 的切换有效,但返回 A 无效(B 保留且 A 不再显示)。

问题:考虑到 OOP,我希望上述概念能够发挥作用。由于情况并非如此,而且这是一个非常基本的策略,因此当我尝试时我应该如何思考 / 考虑 / 设计来做到这一点?

我知道在考虑FormPanel与其他布局/组件时可能会出现不同的情况 - 但必须有一个通用且正确的方法来做到这一点:)

Concept:
Consider having two panels A and B, and a window C like in the following example. The buttons on the window switches between the two panels.

var A = new Ext.Panel({
    title: 'A'
});

var B = new Ext.Panel({
    title: 'B'
});

var C = new Ext.Window({
    layout: 'fit',
    width: 300,
    height: 300,
    items: A,
    buttons: [
    {
        text: 'Switch to A',
        handler: function() {
            C.removeAll(false);
            C.add(A);
            C.doLayout();
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.removeAll(false);
            C.add(B);
            C.doLayout();
        }
    }]
});
C.show();

The concept is very simple: Add a component, remove it and add the same instance again.

Problem:
The switch from A to B works, but going back to A doesn't (B stays and A is not shown again).

Question: Thinking OOP, I would expect the above concept to work. Since this is not the case and its a very basic manouvre, how should I think / contemplate / design when I'm trying to do this?

I understand that there might be varying cases when considering a FormPanel vs. other layouts/components - but there must be a general and correct way to do this :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷默言语 2024-11-04 19:15:05

也许 card 布局正是您所需要的:

var C = new Ext.Window({
    layout: 'card',
    width: 300,
    height: 300,
    activeItem: 0,
    items: [A, B],
    buttons: [{
        text: 'Switch to A',
        handler: function() {
            C.getLayout().setActiveItem(0);
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.getLayout().setActiveItem(1);
        }
    }]
});
C.show();

我认为您的代码的问题是您再次重用同一个实例。一旦组件被写入 DOM 树,Ext 就会在内部设置一个 rendered 标志。由于从 C 中删除组件后,rendered 标志仍然为 true,因此当您再次添加组件时,不会重新绘制。

简单的修改即可使您的代码正常工作:在调用 C.doLayout( 之前分别添加 A.rendered = false;B.rendered = false ) 在按钮处理程序中。

但是卡片布局方法仍然是最佳实践。

Perhaps a card layout is exactly what you need:

var C = new Ext.Window({
    layout: 'card',
    width: 300,
    height: 300,
    activeItem: 0,
    items: [A, B],
    buttons: [{
        text: 'Switch to A',
        handler: function() {
            C.getLayout().setActiveItem(0);
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.getLayout().setActiveItem(1);
        }
    }]
});
C.show();

I assume the problem with your code is that you're reusing the same instance again. Ext internally sets a rendered-flag on a component once it has been written to the DOM tree. As the rendered flag is still true after you removed the component from C it won't be redrawn when you add the component again.

A simple modification will make your code work: add A.rendered = false; and B.rendered = false respectively before you call C.doLayout() in your button handlers.

But still the card-layout approach would be best-practice.

避讳 2024-11-04 19:15:05

我找到了一个简单的解决方案,但它更像是一个黑客。删除组件(您的 A 或 B 面板)后,您必须将其添加到必须渲染的另一个容器中。下面是一个示例,其中面板被移动到隐藏容器:

var A = new Ext.Panel({
    title: 'A'
});

var B = new Ext.Panel({
    title: 'B'
});

var C = new Ext.Window({
    layout: 'fit',
    width: 300,
    height: 300,
    items: A,
    buttons: [
    {
        text: 'Switch to A',
        handler: function() {
            C.remove(B, false);
            T.add(B);
            C.add(A);
            C.doLayout();
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.remove(A, false);
            T.add(A);
            C.add(B);
            C.doLayout();
        }
    }]
});

var T = new Ext.Container({
    renderTo: "temporaryContainer",
    renderHidden: true
});

C.show();

在页面主体的某个位置,您需要这样:

<div id="temporaryContainer" class="x-hidden"></div>

使用 ExtJs 4.0.2a 进行测试。

I've found a simple solution, but it's more of a hack. After removing the component (your A or B panel), you have to add it to another container which must to be rendered. Here is an example in which the panels get moved to a hidden container:

var A = new Ext.Panel({
    title: 'A'
});

var B = new Ext.Panel({
    title: 'B'
});

var C = new Ext.Window({
    layout: 'fit',
    width: 300,
    height: 300,
    items: A,
    buttons: [
    {
        text: 'Switch to A',
        handler: function() {
            C.remove(B, false);
            T.add(B);
            C.add(A);
            C.doLayout();
        }
    }, {
        text: 'Switch to B',
        handler: function() {
            C.remove(A, false);
            T.add(A);
            C.add(B);
            C.doLayout();
        }
    }]
});

var T = new Ext.Container({
    renderTo: "temporaryContainer",
    renderHidden: true
});

C.show();

And somewhere in the page's body you need to have this:

<div id="temporaryContainer" class="x-hidden"></div>

Tested with ExtJs 4.0.2a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文