在 extjs 的多个选项卡中使用相同的项目
如何在多个选项卡中重用同一项目,以便当该项目更改时,其他选项卡将反映
我尝试此代码的更改,但第一个选项卡中的标签未显示:
var label = Ext.create('Ext.form.Label', {
text : 'mylabel'
});
Ext.onReady(function() {
Ext.create('Ext.tab.Panel', {
width : 200,
height : 200,
renderTo : Ext.getBody(),
items : [{
title : 'tab1',
items : [label, {
xtype : 'button',
handler : function() {
label.setText('changed from tab1');
}
}]
}, {
title : 'tab2',
items : [label, {
xtype : 'button',
handler : function() {
label.setText('changed from tab2');
}
}]
}]
});
});
抱歉,我的意思是全局使用该标签(像全局变量),以便可以从每个选项卡显示和更改相同的标签实例
how to reuse the same item in multiple tab so that when that item change, other tab will reflect the changes
i try this code but the label in first tab not shown:
var label = Ext.create('Ext.form.Label', {
text : 'mylabel'
});
Ext.onReady(function() {
Ext.create('Ext.tab.Panel', {
width : 200,
height : 200,
renderTo : Ext.getBody(),
items : [{
title : 'tab1',
items : [label, {
xtype : 'button',
handler : function() {
label.setText('changed from tab1');
}
}]
}, {
title : 'tab2',
items : [label, {
xtype : 'button',
handler : function() {
label.setText('changed from tab2');
}
}]
}]
});
});
i'm sorry, what i mean is to use the label globally(like global variable) so that the same instance of label can be displayed and changed from every tab
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义标签组件:
alias 属性是类名(在本例中为 MyLabel)的别名,这就是为什么您可以使用“mylabel”作为 xtype,
这样您就可以重用该组件,如下所示
you can define your label component:
the alias property is an alias for the class name (in this case MyLabel) and that is why you can use "mylabel" as an xtype
in this way you can reuse the component, like this
你不能在这里完全做你想做的事。您会看到,当您创建标签时,它具有底层 DOM,并且自然 DOM 只能存在于一个位置(因此它不能在两个选项卡上显示相同的内容)。
如果您希望在两个选项卡上显示某个组件,那么从数据层次结构的角度来看,它似乎位于“更高层”。也许它属于选项卡面板之外?
如果标签确实属于两个选项卡并且应该“相同”,那么您要么需要伪造它,要么在选项卡之间手动移动它。
选项 1:Fake It
您可以通过创建自定义 Label 类(如 laurac 发布的那样)在此处获得最大程度的代码重用。您仍然需要保持标签文本同步,因此当另一个标签文本发生更改时,您将需要更新一个标签文本:
显然,这感觉不太“干净”。
选项 2:手动控制
这可能有点老套,但比选项 1 稍微简单一些。基本想法是在激活两个选项卡时将标签在它们之间移动:
注意:我还没有开始使用 Ext4然而,所以我添加的一些代码可能需要针对 Ext4 进行更改(我想也许
doLayout
消失了?)。无论如何,这是我能想到的解决你的问题的唯一两种方法。
祝你好运!
You can't do exactly what you want here. You see, when you create a label, it has underlying DOM, and naturally that DOM can only exist in one place (so it can't show the same thing on both tabs).
If there is a component that you are wanting to show on both tabs, it seems like it is "higher up" from a data hierarchical perspective. Perhaps it belongs outside the tab panel?
If the label truly belongs in both tabs and should be "the same", you are either going to need to fake it or manually move it between the tabs.
Option 1: Fake It
You can get the most code reuse here by creating a custom Label class, like laurac posted. You still need to keep the label text in sync, so you are going to need to update one when the other's text is changed:
Clearly that doesn't feel to "clean".
Option 2: Manual Control
This might be hacky, but slightly less hacky than option 1. Basic idea is to move the label between to two tabs when they are activated:
Note: I haven't started using Ext4 yet, so some of the code I added might need to be changed for Ext4 (I think maybe
doLayout
went away?).Anyway, those are the only two ways I can think of to solve your problem.
Good luck!