Extjs讓combobox寫起來簡潔又漂亮
也已經(jīng)寫了很久時間的extjs ,每次都用到很多的combobox,配置很多東西覺得實在是太麻煩,所以根據(jù)常用到的情況寫了一個簡便的combobox,再次記錄下來,以免放在某個地方忘記了找不到了。
定義一個基本的baseCombobox類,如下。
Ext.define('Admin.view.baseCmp.BaseCombobox', {
extend: 'Ext.form.field.ComboBox',
xtype: 'baseCombobox',
editable: false,
labelSeparator: ':',
labelWdith: 0,
triggerAction: 'all',
labelAlign: 'right',
//forceSelection: true,此屬性操作時,就算去掉文字后,失去焦點后還是會選擇上一次選擇的記錄
autoSelect: true,
selectOnfocus: true,
valueNotFoundText: '',
name:'',
queryMode: 'local',
url:'',
displayField: '',
valueField: '',
requires:['Admin.view.baseCmp.BaseComboboxController'],
controller: 'baseComboboxController',
emptyIndex:-1,//自定義屬性,空值所在下標,-1則不添加
selectIndex:0,//自定義屬性,自動選擇下標
params:null,//自定義屬性,數(shù)據(jù)參數(shù)
listeners: {
render: 'getComboData',
scope: 'controller'
},
});
Ext.define('Admin.view.baseCmp.BaseComboboxController', {
extend: 'Ext.app.ViewController',
alias: 'controller.baseComboboxController',
getComboData: function (combo) {
Ext.Ajax.request({
url: combo.url,
method :'POST',
params:combo.params,
success: function (response) {
var dataJson = Ext.decode(response.responseText);
if(dataJson.state != 200 || dataJson.data == null || dataJson.data.length == 0)
{
//服務(wù)器返回錯誤
return ;
}
var data = dataJson.data;
//插入“全部”選項
if(combo.emptyIndex >= 0)
{
var emp = {};
emp[combo.displayField] = "全部";
emp[combo.valueField] = "全部";
Ext.Array.insert(data,combo.emptyIndex,[emp]);
}
var store = Ext.create('Ext.data.Store', {
fields: Ext.Object.getKeys(data[0]),
data: data
});
combo.setStore(store);
//如果指定選中某個值
if(combo.selectValue != null)
{
combo.select(combo.selectValue);
}
else
{
//如果指定選中某個下標的值,-1為最后一個,> 0 則為第selectIndex個
if(combo.selectIndex == -1)
{
console.log(data.length - 1);
combo.select(data[data.length - 1][combo.valueField]);
}
else
{
combo.select(data[combo.selectIndex][combo.valueField]);
}
}
//觸發(fā)選中事件
//combo.fireEvent('select', combo,store.getAt(combo.selectIndex));
},
failure: function (response) {
//請求服務(wù)器失敗
}
});
}
});
調(diào)用實例:
{
xtype: 'baseCombobox',
name: "typeName",
fieldLabel: "類型",
displayField: 'typeName',
valueField: 'id',
emptyIndex:0,
multiSelect:false,
url:"/itemType/list",
listeners:{
select:'query'
}
},
這樣大大方便了我使用combobox,如果某種類型的combobox需要重復(fù)使用,建議還是直接定義好他,到需要用的時候一句:
xtype: 'itemTypeCombobox',就可以搞定了,代碼看起來簡潔又漂亮。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Extjs4.0 ComboBox如何實現(xiàn)三級聯(lián)動
- ExtJS4給Combobox設(shè)置列表中的默認值示例
- Extjs中ComboBoxTree實現(xiàn)的下拉框樹效果(自寫)
- extjs3 combobox取value和text案例詳解
- Extjs中ComboBox加載并賦初值的實現(xiàn)方法
- Extjs EditorGridPanel中ComboBox列的顯示問題
- extjs中g(shù)rid中嵌入動態(tài)combobox的應(yīng)用
- ExtJS PropertyGrid中使用Combobox選擇值問題
- ExtJs使用總結(jié)(非常詳細)
- ExtJS 學習專題(一) 如何應(yīng)用ExtJS(附實例)
相關(guān)文章
Extjs 3.3切換tab隱藏相應(yīng)工具欄出現(xiàn)空白解決
在切換tabpanel的時候,把相應(yīng)的工具欄隱藏掉,結(jié)果出現(xiàn)空白,先熊板板的空白不過后來終于解決,接下來與大家分享下解決技巧,感興趣的朋友可以參考下哈2013-04-04
常用Extjs工具:Extjs.util.Format使用方法
常用Extjs工具:Extjs.util.Format使用方法,需要的朋友可以參考下2012-03-03
extjs grid設(shè)置某列背景顏色和字體顏色的方法
extjs grid設(shè)置某列背景顏色和字體顏色的方法,需要的朋友可以參考下。2010-09-09
Extjs4 GridPanel的主要配置參數(shù)詳細介紹
主要配置項:store:表格的數(shù)據(jù)集、columns:表格列模式的配置數(shù)組,可自動創(chuàng)建ColumnModel列模式等等2013-04-04
extjs 列表框(multiselect)的動態(tài)添加列表項的方法
最近公司一個項目,因為要使用div模擬的窗口,因為久聞extjs的大名,因此就想在項目中使用一下.首先下載了multiselect的extjs3.0 demo.看到的代碼這里我就不粘貼了.2009-07-07

