sencha touch Button Select(点击按钮进行选择)扩展
此扩展基于官方selectfield控件修改而来,变动并不大,使用方法类似。
代码如下:
Ext.define('ux.SelectBtn', {
extend: 'Ext.Button',
xtype: 'selectBtn',
alternateClassName: 'selectBtn',
requires: [
'Ext.Panel',
'Ext.picker.Picker',
'Ext.data.Store',
'Ext.data.StoreManager',
'Ext.dataview.List'
],
config: {
/**
* @cfg
* @inheritdoc
*/
ui: 'select',
/**
* @cfg {String/Number} valueField The underlying {@link Ext.data.Field#name data value name} (or numeric Array index) to bind to this
* Select control.
* @accessor
*/
valueField: 'value',
/**
* @cfg {String/Number} displayField The underlying {@link Ext.data.Field#name data value name} (or numeric Array index) to bind to this
* Select control. This resolved value is the visibly rendered value of the available selection options.
* @accessor
*/
displayField: 'text',
/**
* @cfg {Ext.data.Store/Object/String} store The store to provide selection options data.
* Either a Store instance, configuration object or store ID.
* @accessor
*/
store: null,
/**
* @cfg {Array} options An array of select options.
*
* [
* {text: 'First Option', value: 'first'},
* {text: 'Second Option', value: 'second'},
* {text: 'Third Option', value: 'third'}
* ]
*
* __Note:__ Option object member names should correspond with defined {@link #valueField valueField} and {@link #displayField displayField} values.
* This config will be ignored if a {@link #store store} instance is provided.
* @accessor
*/
options: null,
/**
* @cfg {String} hiddenName Specify a `hiddenName` if you're using the {@link Ext.form.Panel#standardSubmit standardSubmit} option.
* This name will be used to post the underlying value of the select to the server.
* @accessor
*/
hiddenName: null,
/**
* @cfg {Object} component
* @accessor
* @hide
*/
component: {
useMask: true
},
/**
* @cfg {String/Boolean} usePicker
* `true` if you want this component to always use a {@link Ext.picker.Picker}.
* `false` if you want it to use a popup overlay {@link Ext.List}.
* `auto` if you want to show a {@link Ext.picker.Picker} only on phones.
*/
usePicker: 'auto',
/**
* @cfg {Boolean} autoSelect
* `true` to auto select the first value in the {@link #store} or {@link #options} when they are changed. Only happens when
* the {@link #value} is set to `null`.
*/
autoSelect: true,
/**
* @cfg {Object} defaultPhonePickerConfig
* The default configuration for the picker component when you are on a phone.
*/
defaultPhonePickerConfig: null,
/**
* @cfg {Object} defaultTabletPickerConfig
* The default configuration for the picker component when you are on a tablet.
*/
defaultTabletPickerConfig: null,
/**
* @cfg
* @inheritdoc
*/
name: 'picker',
/**
* @cfg {String} pickerSlotAlign
* The alignment of text in the picker created by this Select
* @private
*/
pickerSlotAlign: 'center',
value:null
},
platformConfig: [
{
theme: ['Windows'],
pickerSlotAlign: 'left'
},
{
theme: ['Tizen'],
usePicker: false
}
],
// @private
initialize: function () {
var me = this;
me.callParent();
//监听按钮点击事件
this.on({
scope: me,
tap: "showPicker"
});
},
/**
* @private
*/
updateDefaultPhonePickerConfig: function (newConfig) {
var picker = this.picker;
if (picker) {
picker.setConfig(newConfig);
}
},
/**
* @private
*/
updateDefaultTabletPickerConfig: function (newConfig) {
var listPanel = this.listPanel;
if (listPanel) {
listPanel.setConfig(newConfig);
}
},
/**
* @private
* Checks if the value is `auto`. If it is, it only uses the picker if the current device type
* is a phone.
*/
applyUsePicker: function (usePicker) {
if (usePicker == "auto") {
usePicker = (Ext.os.deviceType == 'Phone');
}
return Boolean(usePicker);
},
/**
* @private
*/
applyValue: function (value) {
var record = value,
index, store;
//we call this so that the options configruation gets intiailized, so that a store exists, and we can
//find the correct value
this.getOptions();
store = this.getStore();
if ((value != undefined && !value.isModel) && store) {
index = store.find(this.getValueField(), value, null, null, null, true);
if (index == -1) {
index = store.find(this.getDisplayField(), value, null, null, null, true);
}
record = store.getAt(index);
}
return record;
},
updateValue: function (newValue, oldValue) {
this.record = newValue;
},
getValue: function () {
var record = this.record;
return (record && record.isModel) ? record.get(this.getValueField()) : null;
},
/**
* Returns the current selected {@link Ext.data.Model record} instance selected in this field.
* @return {Ext.data.Model} the record.
*/
getRecord: function () {
return this.record;
},
// @private
getPhonePicker: function () {
var config = this.getDefaultPhonePickerConfig();
if (!this.picker) {
this.picker = Ext.create('Ext.picker.Picker', Ext.apply({
slots: [
{
align: this.getPickerSlotAlign(),
name: this.getName(),
valueField: this.getValueField(),
displayField: this.getDisplayField(),
value: this.getValue(),
store: this.getStore()
}
],
listeners: {
change: this.onPickerChange,
scope: this
}
}, config));
}
return this.picker;
},
// @private
getTabletPicker: function () {
var config = this.getDefaultTabletPickerConfig();
if (!this.listPanel) {
this.listPanel = Ext.create('Ext.Panel', Ext.apply({
left: 0,
top: 0,
modal: true,
cls: Ext.baseCSSPrefix + 'select-overlay',
layout: 'fit',
hideOnMaskTap: true,
width: Ext.os.is.Phone ? '14em' : '18em',
height: (Ext.os.is.BlackBerry && Ext.os.version.getMajor() === 10) ? '12em' : (Ext.os.is.Phone ? '12.5em' : '22em'),
items: {
xtype: 'list',
store: this.getStore(),
itemTpl: '<span class="x-list-label">{' + this.getDisplayField() + ':htmlEncode}</span>',
listeners: {
select: this.onListSelect,
itemtap: this.onListTap,
scope: this
}
}
}, config));
}
return this.listPanel;
},
/**
* 显示选择器, whether that is a {@link Ext.picker.Picker} or a simple
* {@link Ext.List list}.
*/
showPicker: function () {
var me = this,
store = me.getStore(),
value = me.getValue();
//check if the store is empty, if it is, return
if (!store || store.getCount() === 0) {
return;
}
if (me.getUsePicker()) {
var picker = me.getPhonePicker(),
name = me.getName(),
pickerValue = {};
pickerValue[name] = value;
picker.setValue(pickerValue);
if (!picker.getParent()) {
Ext.Viewport.add(picker);
}
picker.show();
} else {
var listPanel = me.getTabletPicker(),
list = listPanel.down('list'),
index, record;
if (!listPanel.getParent()) {
Ext.Viewport.add(listPanel);
}
//基于按钮显示
listPanel.showBy(me);
if (value || me.getAutoSelect()) {
store = list.getStore();
index = store.find(me.getValueField(), value, null, null, null, true);
record = store.getAt(index);
if (record) {
list.select(record, null, true);
}
}
}
},
// @private
onListSelect: function (item, record) {
var me = this;
if (record) {
me.setValue(record);
//选择成功触发事件
this.fireEvent('selecSuccess', this, this.getValue(), record);
}
},
onListTap: function () {
this.listPanel.hide({
type: 'fade',
out: true,
scope: this
});
},
// @private
onPickerChange: function (picker, value) {
var me = this,
newValue = value[me.getName()],
store = me.getStore(),
index = store.find(me.getValueField(), newValue, null, null, null, true),
record = store.getAt(index);
me.setValue(record);
//选择成功触发事件
this.fireEvent('selecSuccess', this, this.getValue(), record);
},
onChange: function (component, newValue, oldValue) {
var me = this,
store = me.getStore(),
index = (store) ? store.find(me.getDisplayField(), oldValue, null, null, null, true) : -1,
valueField = me.getValueField(),
record = (store) ? store.getAt(index) : null;
oldValue = (record) ? record.get(valueField) : null;
me.fireEvent('change', me, me.getValue(), oldValue);
},
/**
* Updates the underlying `<options>` list with new values.
*
* @param {Array} newOptions An array of options configurations to insert or append.
*
* selectBox.setOptions([
* {text: 'First Option', value: 'first'},
* {text: 'Second Option', value: 'second'},
* {text: 'Third Option', value: 'third'}
* ]).setValue('third');
*
* __Note:__ option object member names should correspond with defined {@link #valueField valueField} and
* {@link #displayField displayField} values.
*
* @return {Ext.field.Select} this
*/
updateOptions: function (newOptions) {
var store = this.getStore();
if (!store) {
this.setStore(true);
store = this._store;
}
if (!newOptions) {
store.clearData();
}
else {
store.setData(newOptions);
this.onStoreDataChanged(store);
}
return this;
},
applyStore: function (store) {
if (store === true) {
store = Ext.create('Ext.data.Store', {
fields: [this.getValueField(), this.getDisplayField()],
autoDestroy: true
});
}
if (store) {
store = Ext.data.StoreManager.lookup(store);
store.on({
scope: this,
addrecords: 'onStoreDataChanged',
removerecords: 'onStoreDataChanged',
updaterecord: 'onStoreDataChanged',
refresh: 'onStoreDataChanged'
});
}
return store;
},
updateStore: function (newStore) {
if (newStore) {
this.onStoreDataChanged(newStore);
}
if (this.getUsePicker() && this.picker) {
this.picker.down('pickerslot').setStore(newStore);
} else if (this.listPanel) {
this.listPanel.down('dataview').setStore(newStore);
}
},
/**
* Called when the internal {@link #store}'s data has changed.
*/
onStoreDataChanged: function (store) {
var initialConfig = this.getInitialConfig(),
value = this.getValue();
if (value || value == 0) {
this.updateValue(this.applyValue(value));
}
if (this.getValue() === null) {
if (initialConfig.hasOwnProperty('value')) {
this.setValue(initialConfig.value);
}
if (this.getValue() === null && this.getAutoSelect()) {
if (store.getCount() > 0) {
this.setRecord(store.getAt(0));
}
}
}
},
/**
* Resets the Select field to the value of the first record in the store.
* @return {Ext.field.Select} this
* @chainable
*/
reset: function () {
var store = this.getStore(),
record = (this.originalValue) ? this.originalValue : store.getAt(0);
if (store && record) {
this.setValue(record);
}
return this;
},
destroy: function () {
this.callParent(arguments);
var store = this.getStore();
if (store && store.getAutoDestroy()) {
Ext.destroy(store);
}
Ext.destroy(this.listPanel, this.picker);
}
});
使用示例
引用:
requires: ['ux.SelectBtn'],
使用(可以参考官方selectfield控件用法):
{
xtype: 'selectBtn',
text: '分享',
align: 'right',
ui: 'decline',
valueField: 'name',
displayField: 'name',
action:'share',
store: 'shareList'
}
控制层监听
引用:
refs: {
shareBtn: 'button[action=share]'
}
监听(选择成功):
control: {
shareBtn: {
selecSuccess: function (t, value, record) {
console.log(value,record);
}
}
}
效果图(点击按钮后):

sencha touch Button Select(点击按钮进行选择)扩展的更多相关文章
- input选中 和 select点击下拉选择获取选中选项的值
1.input选中$('#checkBox').find('input').each(function(i){ if($(this).prop('checked')){//获取是否选中 并判断 $(t ...
- JavaScript 弹出窗体点击按钮返回选择数据的实现
首先是父页面的代码: <head runat="server"> <title>无标题页</title> <%-- <script ...
- sencha touch 问题汇总
做sencha touch有一段时间了,目前而言,sencha touch在android上问题比较严重,在此对android中sencha touch的问题做一些汇总: 1.内存问题: 打包成安装程 ...
- 【翻译】在Ext JS和Sencha Touch中创建自己定义布局
原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...
- 【翻译】在Ext JS和Sencha Touch中创建自定义布局
原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...
- sencha touch 2.2.1 自定义彩色图标按钮(button+ico)
sencha touch 2.2.1 这个版本使用了新的按钮模式,不过他只提供了少部分的按钮样式.我们可以加一些自定义的ico样式 新加ico样式lower .x-button .x-button-i ...
- 关于用phonegap 3.0+ 打包后sencha touch按钮点击切换动画延迟接近一秒的以及界面闪烁的解决方案
android的webview对硬件加速的支持貌似很不理想,在开启硬件加速的情况下,css3这些需要调用硬件加速的样式会大幅拖慢html5的webapp,在htc的部分手机上还会因开启硬件加速而导致闪 ...
- sencha touch TabPanel 加入导航按钮(向左向右按钮) 以及Carousel插件(2014-11-7)
Carousel插件代码: /* * TabPanel的Carousel功能插件 * 取至 * https://github.com/VinylFox/Ext.ux.touch.SwipeTabs * ...
- VB.Net中点击按钮(Button)会重复提交两次表单
在VB.NET程序开发过程中遇到一个问题 提交一个表单时,button的html代码如下 <asp:Button ID="btnSubmit" OnClick="c ...
随机推荐
- linux中chown命令
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- iOS:DKLiveBlur
https://github.com/kronik/DKLiveBlur Sources of DKLiveBlur and Demo app to show live blur effect sim ...
- hibernate.cfg配置mysql方言
hibernate自动建表,mysql高版本不支持 type=InnoDB 中的type关键字. 应该使用engine关键字 而非type 所以需要在hibernate.cfg.xml中配置方言.否则 ...
- asp.net c# repeater或gridview导出EXCEL的详细代码。
Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Enco ...
- what's the help of "unnecessary" pointer comparison
引述自http://c-programming.itags.org/q_c-programming-language_191518.html 源代码中的宏min中使用了 (void) (&_x ...
- 【ML】数据集资源
http://www.kdnuggets.com/datasets/index.html http://kdd.ics.uci.edu/
- redhat enterprise edition 6.8:禁止ipv6后,nfs文件系统无法挂载:no such device
如题:谨记. 附注:如何禁止ipv6? 方法一 第一种方法是通过 /etc/sysctl.conf 文件对 /proc 进行永久修改. 换句话说,就是用文本编辑器打开 /etc/sysctl.conf ...
- teamviwer安装提示 Verification of your Teamviewer version failed!.
打开cmd输入 regsvr32 c:\windows\SysWOW64\wintrust.dll 就可以了.
- ios的两种界面跳转方式
1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...
- SpringBoot------Eclipce配置Spring Boot
步骤一: 步骤二: 点击左下角Eclipse图标下的“Popular”菜单,选择Spring安装(已安装的插件在Installed中显示),一直按步骤确定就好了,如果中途下载超时什么的,就看看自己的网 ...