-
7 Dec 2008 11:41 PM #1
Link Button or Hyper link
Link Button or Hyper link
Hi All,
I want to know how to add a link button (Hyper link) using Ext JS framework?
Can anybody please help me?
-
7 Dec 2008 11:48 PM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
Ext doesn't have a link component, but you could simply use:
Code:{ xtype: 'box', autoEl: {tag: 'a', href: 'http://www.google.com', html: 'Google'} }
-
8 Dec 2008 12:33 AM #3
Thanks It works.
Also how can I style to the hyper link created using above method?
-
8 Dec 2008 12:36 AM #4Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
Using cls:'my-class' or style:{color: 'red'} in the autoEl config.
-
8 Dec 2008 3:40 AM #5
Here's a LinkButton class for 2.2
You will need to upgrade it when you move to 3.0:Code:Ext.LinkButton = Ext.extend(Ext.Button, { template: new Ext.Template( '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>', '<td class="x-btn-left"><i>&#160;</i></td><td class="x-btn-center"><a class="x-btn-text" href="{1}" target="{2}">{0}</a></td><td class="x-btn-right"><i>&#160;</i></td>', "</tr></tbody></table>"), onRender: function(ct, position){ var btn, targs = [this.text || ' ', this.href, this.target || "_self"]; if(position){ btn = this.template.insertBefore(position, targs, true); }else{ btn = this.template.append(ct, targs, true); } var btnEl = btn.child("a:first"); btnEl.on('focus', this.onFocus, this); btnEl.on('blur', this.onBlur, this); this.initButtonEl(btn, btnEl); btn.un(this.clickEvent, this.onClick, this); Ext.ButtonToggleMgr.register(this); } });
The ampersand messes are ampersand hash 160 semicolon. The forum does not let us post correctly.Code:Ext.LinkButton = Ext.extend(Ext.Button, { template: new Ext.Template( '<table cellspacing="0" class="x-btn {3}"><tbody class="{4}">', '<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="{5}" unselectable="on"><a href="{6}" target="{7}" class="x-btn-text {2}"><button>{0}</button></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>').compile(), buttonSelector : 'a:first', getTemplateArgs: function() { return Ext.Button.prototype.getTemplateArgs.apply(this).concat([this.href, this.target]); }, onClick : function(e){ if(e.button != 0){ return; } if(!this.disabled){ this.fireEvent("click", this, e); if(this.handler){ this.handler.call(this.scope || this, this, e); } } } });Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
9 Feb 2009 11:53 AM #6
nice !
but what about if we need to add a params to the url ?.. the href can't be changed dynamically after the button is rendered...
T.
-
9 Feb 2009 12:06 PM #7
I had that requirement too.
I have the code somewhere. Bump this thread tomorrow, and I'll try to find it.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
10 Feb 2009 12:37 AM #8
If you are using Ext 2.2.1, then change your class to use getTemplateArgs instead of just assigning targs.
then add:
Code:params: {}, baseParams: {}, getTemplateArgs: function() { return [this.text || ' ', this.href, this.target || "_self", this.getHref(), this.target]); }, getHref: function() { var result = this.href; var p = Ext.urlEncode(Ext.apply(Ext.apply({}, this.baseParams), this.params)); if (p.length) { result += ((this.href.indexOf('?') == -1) ? '?' : '&') + p; } return result; }, setParams: function(p) { this.params = p; this.el.child(this.buttonSelector, true).href = this.getHref(); }
Then calling setParams at any time will update the href in the <a> element.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
10 Feb 2009 2:26 AM #9
I understand the idea. clever..
But what do you mean by not using the targets anymore in the class and using getTemplateArgs method ?.. to render the button you will still need to use the template ?..
T.
-
10 Feb 2009 2:33 AM #10
Yes, but pull the data for the template from a call to getTemplateArg() instead of just creating the targs array.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642


Reply With Quote