简体   繁体   English

ExtJS面板附加html

[英]ExtJS panel append html

I have an extjs4 panel created using this config object. 我有一个使用此配置对象创建的extjs4面板。

{ 
    title : 'Messages',
    region : 'north',
    height : 200,
    minSize : 75,
    maxSize : 250,
    cmargins : '0 0 5 0',
    html : '<i>Chat started on ' +  Ext.Date.format(dt, EI.datePattern)  + '</i>'
}

Now I want to append more html on it. 现在,我想在上面附加更多的html。 For example I want to append 例如我要附加

<p><span class="user">me:</span><span class="how are you?"></span></p>

Sometimes I could append using 有时我可以附加使用

 thePanel.body.insertHtml("beforeEnd", chatHtml);

But I see sometimes .body is not set!! 但是我有时看到.body没有设置! So I can not execute the above code. 所以我不能执行上面的代码。 What is the proper way to do it? 正确的做法是什么?

The body element of a panel isn't available until after the panel's render event has fired. 面板的body元素要等到面板的render事件触发后才能使用。 This is the same for all elements that are listed under the childEls config of any component. 对于任何组件的childEls配置下列出的所有元素,这都是相同的。

There are a few different ways an element can be rendered. 可以使用几种不同的方式渲染元素。 This isn't a comprehensive list. 这不是一个完整的列表。

  1. If a component is a child item of a container, then the component will be rendered when the parent container renders. 如果组件是容器的子项,则将在父容器渲染时渲染该组件。 The exception to this is containers using card layout, such as a tab panel, when using the deferredRender config. 例外是使用deferredRender配置时使用卡布局的容器,例如选项卡面板。

  2. If a component is not a child of a container, but it is using the renderTo config, it will be rendered upon construction, even if it is hidden. 如果组件不是容器的子组件,但使用的是renderTo配置,则即使已隐藏该renderTo也将在构造时呈现。

  3. If a non-floating component is using the autoRender config, that component will render the first time it is shown, such as with panel.show() . 如果非浮动组件正在使用autoRender配置,则该组件将在第一次显示该组件时呈现,例如panel.show() If it is using both autoRender and autoShow then it will be rendered upon construction. 如果同时使用autoRenderautoShow ,则将在构造时进行渲染。

  4. Floating components such as windows default to autoShow: false and autoRender: true , so they will not render until you call win.show() . 诸如Windows之类的浮动组件默认为autoShow: falseautoRender: true ,因此它们win.show()在您调用win.show()才会呈现。 If you set autoShow to true, it will render on creation. 如果将autoShow设置为true,它将在创建时呈现。

  5. Floating components using the renderTo config will render on creation and stay hidden, unless also using autoShow as mentioned above. 使用renderTo配置的浮动组件将在创建时呈现并保持隐藏,除非也如上所述使用autoShow

It sounds like you have a panel that is a child of a window, correct? 听起来您的面板是窗户的孩子,对吗? To get that panel to render immediately but stay hidden, set renderTo: Ext.getBody() on the parent window. 要使该面板立即呈现但保持隐藏状态,请在父窗口上设置renderTo: Ext.getBody()

Alternatively, you can still access the panel.html property before the panel is rendered, and any changes will be reflected when the window is shown. 或者,您仍然可以在呈现面板之前访问panel.html属性,并且显示窗口时,任何更改都会反映出来。 So if you need to access the inner content but you can't guarantee the panel has been rendered, you might do something like this: 因此,如果您需要访问内部内容,但不能保证已渲染面板,则可以执行以下操作:

if (panel.body) {
    panel.body.insertHtml("beforeEnd", chatHtml);
} else {
    panel.html += chatHtml;
}

One last thing. 最后一件事。 If you set the html config of a panel then render it, there will be an extra div called the clearEl after your original content. 如果您设置面板的html配置然后进行渲染,则在原始内容之后会有一个额外的div,称为clearEl Calling panel.body.insertHtml() after render will place the new HTML after this div. 渲染后调用panel.body.insertHtml()会将新的HTML放在该div之后。 This may or may not be a problem for you, but I thought I'd mention it anyway. 这可能对您来说不是问题,但我还是想提一提。

Try using thePanel.getTargetEl() 尝试使用thePanel.getTargetEl()

From the source: 从来源:

getTargetEl: function() {
    var me = this;
    return me.body || me.protoBody || me.frameBody || me.el;
}

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

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