How should I add a tooltip to an ExtJS Component?

2019-01-25 02:10发布

I'm making an ExtJS Component, and I want it to use a QuickTips tooltip. If I make an element using DomHelper, I can set a tooltip, no sweat. If, however, I make a Component, like

new BoxComponent({
  qtip: "This is a tip"
});

nothing happens. I've also tried naming the property "tooltip", but no luck. Is there a right way to do this? The hack I have in place now that works is

new BoxComponent({
  qtip: "This is a tip",
  listeners: {
    rendered: function(c){
      Ext.QuickTips.register({
        target: c.getEl(),
        text: c.qtip
      }
    }
});

I feel like that can't be right. I guess I could just extend Component to do that automatically, but it seems like a common enough case that I should be able to do it without poking under the hood like this.

标签: Extjs tooltip
9条回答
倾城 Initia
2楼-- · 2019-01-25 02:54

I think this is the best way in Extjs 4:

you should add an afterrender listener, then when yor componenten is already created you can add the tooltip, this way:

listeners : {
    afterrender : function(obj) {
        if (this.max != null && this.ave != null && this.min != null) {
            obj.tip = Ext.create('Ext.tip.ToolTip', {
                        target : obj.getEl().getAttribute("id"),
                        trackMouse : true,
                        renderTo : document.body,
                        html : this.htmlTip,
                        title : this.title
                    });
        }
    }
}

I hope it helps.

查看更多
来,给爷笑一个
3楼-- · 2019-01-25 02:55

Hrm. I took a look at how Ext.Button does it, with passing tooltip to the configuration calling setTooltip under the hood.

Untested, but I think your best bet is something like:

Ext.Component.prototype._onRender = Ext.Component.prototype.onRender;
Ext.override(Ext.Component, {
  onRender: Ext.Component.prototype._onRender.createSequence(function(ct, position) {
    // setTooltip code adapted from Ext.Button, looking at tooltip property
  });
});
查看更多
Bombasti
4楼-- · 2019-01-25 02:58

It should work :

new BoxComponent({
 tooltip: new Ext.ToolTip({
        title: 'Example Tooltip title',
            text: 'Example Tooltip text'

    }),
  listeners: {
    rendered: function(c){
      Ext.QuickTips.register({
        target: c.getEl(),
        text: c.qtip
      }
    }
});
查看更多
登录 后发表回答