Extjs自定义组件

Extjs中的组件一般是继承自Ext.Component,如果想了解组件是如何实现的,具体可以参照Button的源代码,其是直接继承自Ext.Component的。

其中的关键属性 renderTpl 和 renderData,分别是设置模板和模板数据的属性,但是这样设置的话不灵活,不能像Button一样设置一个Text属性

就能自动设置模板中的数据,下面是参照Button实现的,在onRender回调函数中,动态的设置renderData,即将对象中的部分属性动态的添加到

renderData中。

监听Click事件的方法:

me.mon(me.el, ‘click, me.onClick, me);

所有的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Ext.onReady ( function () {
 
     
     Ext.define ( 'MydesktopIcon' , {
             /* Begin Definitions */
             alias: 'widget.desktopIcon' ,
             extend: 'Ext.Component' ,
             
             width: 76,
             //height: 84,
             overCls: 'x-view-over' ,
  beforeRender: function () {
                    var me = this;
                    me.callParent();


                    Ext.applyIf(me.renderData, me.getTemplateArgs());
                },
             renderTpl:
                 '<div class="ux-desktop-shortcut" id="{module}-shortcut">' +
                 '<div class="ux-desktop-shortcut-icon">' +
                     '<img src="{iconName}" title="{name}">' +
                 '</div>' +
                 '<span class="ux-desktop-shortcut-text">{name}</span>' +
                 '</div>' +
                 '<div class="x-clear"></div>' ,
             
             // private
             onRender: function (ct, position) {
                 // classNames for the button
                 var me = this ;
                 // Apply the renderData to the template args
                 Ext.applyIf(me.renderData, me.getTemplateArgs());
                  // Render internal structure
                 me.callParent(arguments);
                 //添加单击事件
                 me.mon(me.el, 'click' , me.onClick, me);
             },
             getTemplateArgs: function () {
                 var me = this ;
                 return {
                    name: me.name || ' ' ,
                    module: me.module || ' ' ,
                    iconName: me.iconName || Ext.BLANK_IMAGE_URL,
                 }
             },
             onClick: function (e) {
                     var me = this ;
                     me.ownerCt.fireEvent ( 'itemClick' , this );
                 },
 
             // inherit docs
                 initComponent: function () {
                     var me = this ;
                     me.callParent(arguments);
                 
             },
         });
 
 
     Ext.create ( 'Ext.panel.Panel' , {
             title: 'test' ,
             frame: true ,
             height:800,
             renderTo: Ext.getBody(),
             items: [{
                         xtype: 'desktopIcon' ,
                         name: '首页' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/problems.png'
                     },{
                         xtype: 'desktopIcon' ,
                         name: '个人信息' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/myself.png'
                 },{
                         xtype: 'desktopIcon' ,
                         name: '首页' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/firstpage.png'
                     },{
                         xtype: 'desktopIcon' ,
                         name: '首页' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/contest.png'
                 },
                 {
                         xtype: 'desktopIcon' ,
                         name: '首页' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/college.png'
                     },{
                         xtype: 'desktopIcon' ,
                         name: '首页' ,
                         module: 'firstPage' ,
                         iconName: '../SDOJ/images/letter.png'
                 }],
             listeners: {
                 itemClick: function (item) {
                     alert (item.name)
                 }
             }
                 
         });
 
 
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ExtJS 中自定义一个 textarea 组件并显示行号,可以通过下面两种方式实现: 1. 使用 Ext.ux.form.field.CodeMirror 组件 使用 Ext.ux.form.field.CodeMirror 组件可以非常方便地实现 textarea 组件显示行号的功能。该组件基于 CodeMirror 编辑器实现,支持语法高亮、自动补全等功能。 示例代码: ``` Ext.define('MyApp.view.MyTextArea', { extend: 'Ext.ux.form.field.CodeMirror', alias: 'widget.mytextarea', initComponent: function() { this.callParent(); this.editor.setOption('lineNumbers', true); } }); ``` 在组件的 `initComponent` 方法中,设置 `lineNumbers` 选项为 `true` 即可显示行号。 2. 使用 Ext.form.field.TextArea 组件 如果不想使用 CodeMirror 编辑器,也可以通过自定义渲染函数来实现 textarea 组件显示行号的功能。具体实现方式如下: 示例代码: ``` Ext.define('MyApp.view.MyTextArea', { extend: 'Ext.form.field.TextArea', alias: 'widget.mytextarea', afterRender: function() { this.callParent(); this.mon(this.inputEl, 'scroll', this.onScroll, this); this.mon(this.inputEl, 'resize', this.onResize, this); this.mon(this.inputEl, 'keydown', this.onKeyDown, this); this.mon(this.inputEl, 'keyup', this.onKeyUp, this); this.mon(this.inputEl, 'mousedown', this.onMouseDown, this); this.mon(this.inputEl, 'mouseup', this.onMouseUp, this); this.mon(this.inputEl, 'dblclick', this.onDblClick, this); this.mon(this.inputEl, 'contextmenu', this.onContextMenu, this); this.updateLineNumber(); }, onScroll: function() { this.updateLineNumber(); }, onResize: function() { this.updateLineNumber(); }, onKeyDown: function() { this.updateLineNumber(); }, onKeyUp: function() { this.updateLineNumber(); }, onMouseDown: function() { this.updateLineNumber(); }, onMouseUp: function() { this.updateLineNumber(); }, onDblClick: function() { this.updateLineNumber(); }, onContextMenu: function() { this.updateLineNumber(); }, updateLineNumber: function() { var text = this.getValue(); var lineNumber = text.split(/\r?\n/).length; var lineNumberHtml = ''; for (var i = 1; i <= lineNumber; i++) { lineNumberHtml += i + '<br/>'; } this.getEl().down('.line-number').update(lineNumberHtml); }, getSubTplMarkup: function(values) { return this.callParent([Ext.apply({}, values, { inputCls: 'x-form-textarea', value: values.value && Ext.util.Format.htmlEncode(values.value), lineNumber: true })]); }, getSubTplData: function(fieldData) { var data = this.callParent([fieldData]); if (fieldData.lineNumber) { data.inputWrapCls = (data.inputWrapCls || '') + ' x-form-textarea-with-number'; data.afterInputEl = '<div class="line-number"></div>'; } return data; } }); ``` 在组件的 `afterRender` 方法中,绑定 `scroll`、`resize`、`keydown`、`keyup`、`mousedown`、`mouseup`、`dblclick` 和 `contextmenu` 事件,当组件内容发生变化时更新行号。在 `updateLineNumber` 方法中,计算组件内容的行数,并生成对应的 HTML 代码插入到组件 DOM 中。在 `getSubTplData` 和 `getSubTplMarkup` 方法中,通过添加 CSS 类和 HTML 代码实现行号的显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值