最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ext Js: make button a link - Stack Overflow

programmeradmin4浏览0评论

In Ext Js I want some of my buttons to work like links (i.e. <a href...).

How can I do that.

Right now I am adding a handler that does window.location.href=http://.....

But I thought there should be an easier way - something like adding an href attribute like in menu item.

Some ideas?

In Ext Js I want some of my buttons to work like links (i.e. <a href...).

How can I do that.

Right now I am adding a handler that does window.location.href=http://.....

But I thought there should be an easier way - something like adding an href attribute like in menu item.

Some ideas?

Share Improve this question asked Dec 28, 2010 at 8:19 flybywireflybywire 274k200 gold badges405 silver badges509 bronze badges 2
  • As far as I know, that's actually the way it's supposed to be done. – Mchl Commented Dec 28, 2010 at 8:23
  • For ExtJS Modern 6.x (6.5) see stackoverflow./questions/47494458/… – Tony J Watson Commented Nov 28, 2017 at 1:39
Add a ment  | 

2 Answers 2

Reset to default 7

That's the way it's done... To be more portable you could extend Ext.Button into Ext.ux.LinkButton (or whatever) and implement the property and the required behavior in this extended class (just a quick-&-dirty example):

Ext.ux.LinkButton = Ext.extend(Ext.Button, {
    href: null,
    handler: function() {
        if (this.href) {
            window.location.href = this.href;
        }
    } 
}); 
Ext.reg( "ux-linkbutton", Ext.ux.LinkButton );

var btn = new Ext.ux.LinkButton({
    text: "Link to Google",
    href: "http://www.google."
});

EDIT:

Simple change of appearance:

Ext.ux.LinkButton = Ext.extend(Ext.Button, {
    href: null,
    template: new Ext.Template(
        ['<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
        '<tr><td class="x-btn-tl"><i>&#160;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&#160;</i></td></tr>',
        '<tr><td class="x-btn-ml"><i>&#160;</i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><a href="{0}"></a></em></td><td class="x-btn-mr"><i>&#160;</i></td></tr>',
        '<tr><td class="x-btn-bl"><i>&#160;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&#160;</i></td></tr>',
        '</tbody></table>'], {piled: true}),
    buttonSelector : 'a:first-child',
    getTemplateArgs : function(){
        return [this.href, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
    },
    handler: function(b, e) {
        if (this.href) {
            e.stopEvent();
            window.location.href = this.href;
        }
    } 
}); 

There's also an existing user extension that does exactly this.

发布评论

评论列表(0)

  1. 暂无评论