Extjs:如何理解这个概念:添加、删除、添加实例
概念: 考虑有两个面板 A
和B
,以及一个窗口 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许
card
布局正是您所需要的:我认为您的代码的问题是您再次重用同一个实例。一旦组件被写入 DOM 树,Ext 就会在内部设置一个
rendered
标志。由于从C
中删除组件后,rendered
标志仍然为 true,因此当您再次添加组件时,不会重新绘制。简单的修改即可使您的代码正常工作:在调用
C.doLayout( 之前分别添加
在按钮处理程序中。A.rendered = false;
和B.rendered = false
)但是卡片布局方法仍然是最佳实践。
Perhaps a
card
layout is exactly what you need: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 therendered
flag is still true after you removed the component fromC
it won't be redrawn when you add the component again.A simple modification will make your code work: add
A.rendered = false;
andB.rendered = false
respectively before you callC.doLayout()
in your button handlers.But still the card-layout approach would be best-practice.
我找到了一个简单的解决方案,但它更像是一个黑客。删除组件(您的 A 或 B 面板)后,您必须将其添加到必须渲染的另一个容器中。下面是一个示例,其中面板被移动到隐藏容器:
在页面主体的某个位置,您需要这样:
使用 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:
And somewhere in the page's body you need to have this:
Tested with ExtJs 4.0.2a.