插件代码

 /*
*list tpl模版加入按钮监控
*<div class="x-button-normal x-button x-iconalign-center x-layout-box-item x-stretched btn"><span class="x-button-icon x-shown lower" fire="showWeibo"></span></div>
*fire="showWeibo" 作用是激活指定事件
*有两个参数cmp:视图本身以及doit
*只要是以上格式的模板都可以被监控到
*其中btn、lower为自定义样式,其他都是st自带样式
*/
Ext.define('ux.ListTpl', {
alias: 'plugin.ListTpl',
xtype: 'listTpl',
config: {
list: null,
//按下时添加css
pressedCls: 'pressing',
//监控对象选择器
delegate: 'div.x-button',
//是否监听input控件
isInput: false
},
constructor: function (config) {
this.initConfig(config);
this.callParent([config]);
},
//初始化
init: function (list) {
this.setList(list);
},
//更新配置
updateList: function (newList, oldList) {
if (newList) {
//为自定义按钮注册点击事件
newList.container.element.on({
tap: 'onTap',
touchstart: 'onPress',
touchend: 'onRelease',
delegate: this.getDelegate(),
scope: this
});
if (this.getIsInput()) {
//为自定义按钮注册点击事件
newList.container.element.on({
blur: 'onBlur',
delegate: 'input[type="text"]',
scope: this
});
} }
},
//执行动作
onTap: function (e) {
var me = this.getList(),
item = Ext.getCmp(Ext.get(e.getTarget()).up('.x-list-item').id),
index = item.$dataIndex,
record = me.getStore().getAt(index),
el = e.getTarget(this.getDelegate(), null, true),
fire = el.getAttribute('fire'),
action = 'do' + fire;
me.fireAction(fire, [me, record, item, index, el], action);
},
//按钮按下时,添加css
onPress: function (e, node) {
var el = e.getTarget(this.getDelegate(), null, true);
el.addCls(this.getPressedCls());
},
//按钮松开时,移除css
onRelease: function (e, node) {
var el = e.getTarget(this.getDelegate(), null, true);
el.removeCls(this.getPressedCls());
},
//焦点离开时,将值填充到store中
onBlur: function (e) {
var me = this.getList(),
item = Ext.getCmp(Ext.get(e.getTarget()).up('.x-list-item').id),
index = item.$dataIndex,
record = me.getStore().getAt(index),
el = e.getTarget('input', null, true),
value = el.getValue(),
name = el.getAttribute('name');
record.data[name] = value;
}
});

使用代码:

 Ext.define('app.view.eatery.Shop', {
alternateClassName: 'eateryShop',
extend: 'Ext.List',
xtype: 'eateryShop',
requires: ['ux.ListTpl'],
config: {
cls: 'list',
plugins: [{
xtype: 'listTpl',
isInput: true
}],
title: '购物车',
btmBar: 'eateryBar',
isNoHide: true,
scrollToTopOnRefresh: false,
itemTpl: new Ext.XTemplate(
'<div class="bh">',
'<div class="bone">{name}</div>',
'<div class="bh">',
'<div class="x-button-normal x-button x-iconalign-center x-layout-box-item x-stretched btn" style="visibility:{visibility}" fire="onTasteUp" value="-1"><span class="x-button-icon x-shown lower"></span></div>',
'{taste}',
'<div class="x-button-normal x-button x-iconalign-center x-layout-box-item x-stretched btn" fire="onTasteUp" value="1"><span class="x-button-icon x-shown add"></span></div>',
'</div>',
'</div>',
'<div>{price}</div>',
'<div>备注:<input type="text" name="description" value="{description}"/></div>'),
store: 'shopList',
selectedCls: '',
pressedCls: ''
}
});

监听代码:

  eateryList: {
onTasteUp: function (list, index, record, btn) {
var visibility = 'visible',
value = +btn.getAttribute("value"),
taste = record.data.taste + value;
if (taste == 0) {
visibility = 'hidden';
}
record.set({ taste: taste, visibility: visibility });
}
}

效果图:

2013.9.15

优化代码,参考list源码书写。为控件添加点击事件和点击方法,不再触发list默认单击事件

添加了对输入框的支持,可自动将输入框中的值填充到数据源中

最新文章

  1. Tcl
  2. VS快速生成JSON数据格式对应的实体
  3. Handlebars.js循环中索引(@index)使用技巧(访问父级索引)
  4. 循序渐进 Jprofiler
  5. python从Microsoft Excel文件中导入数据
  6. Redis一些基本的操作
  7. centos chrome
  8. linux复制多个文件到文件夹
  9. AdaBoostRegressor
  10. 第一次c语言上机
  11. linux小计
  12. 根据request获取请求客户端的外网ip
  13. 第十二章 NIO
  14. 光速搭lvs + keepalived + nginx
  15. Chrome默认搜索引擎被窜改
  16. C++编程命名规则
  17. 算法提高 11-1实现strcmp函数
  18. 类似openDialog的弹窗
  19. 一对一Socket简单聊天的实现
  20. 【数组】Missing Number

热门文章

  1. linux中chown命令
  2. NSNull, Nil, nil, NULL区别(OC)
  3. Oracle:oratop 第一栏中的 {n}er 的含义,及如何清除这个er
  4. [原]C# 常用函数统计
  5. 一个基于jquery的智能提示控件intellSeach.js
  6. Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等
  7. UML类图关系(转,添加了实例)
  8. Oracle分析関数
  9. Maven 多项目依赖,需要验证artifact的output root中是否包含其他项目输出
  10. jquery easyui datagrid 无滚动条,datagrid 没垂直滚动条