ExtJS4如何自動生成控制grid的列顯示、隱藏的checkbox
更新時間:2014年05月02日 11:50:42 作者:
需要做一個控制grid列顯示的checkboxgroup,雖然EXTJS4中的gridpanel自帶列表可以來控制列的顯示隱藏,需要的朋友可以參考下
由于某種原因,需要做一個控制grid列顯示的checkboxgroup,雖然EXTJS4中的gridpanel自帶列表可以來控制列的顯示隱藏,但是有這樣的需求(需要一目了然)
下面先上圖
接著前幾天做的工作,今天上午完成了定制字段,思路是在上面的普通查詢或者高級查詢結(jié)束以后,獲得了列的fields,columns等信息,然后交給一個處理函數(shù) makeCustomMadePanel,該函數(shù)用來生成checkboxgroup,生成的時候給它加上一個事件,原本以為checkbox會有類似于check的事件,結(jié)果API看了看貌似只有個change事件可以用,MD。。
下面貼下自己寫的 makeCustomMadePanel函數(shù)。。用來根據(jù)grid的列自動生成checkboxgroup(整個grid的標頭內(nèi)容等信息均從后臺得到,不管后臺發(fā)來一個什么表,都能生成一個checkboxgroup來控制列的隱藏顯示)
參數(shù)分別是gridpanel在reconfigure的時候用到的fields和columns,期中的var t=grid_a.columnManager.headerCt.items.get(th.itemId);是關(guān)鍵。。這句用來獲得grid_a的列信息。。貌似在api中查不到。網(wǎng)上找了幾中方法都不適合。又不想給每個列一個ID。這是在stackoverflow.com/上找到的。。http://stackoverflow.com/questions/20791685/extjs-4-how-do-i-hide-show-grid-columns-on-the-fly
function makeCustomMadePanel(fields,cl)
{
var x=cusMadePanel.getComponent('custom');
//console.log(cusMadePanel.getComponent('custom'));
for(var i=0;i<fields.length;i++)
{
x.add(
{
xtype : 'checkboxfield',
boxLabel : cl[i].header,
inputValue : fields[i].name,
checked:true,
itemId:i,
name : 'custom',
listeners : {
change : function(th, value, oldValue,eop) {
var t=grid_a.columnManager.headerCt.items.get(th.itemId);
if(t.isVisible()){
t.setVisible(false);
}
else{
t.setVisible(true);
}
//grid_a.columns[3].setVisible(false);
}}
}
);
}
}
在給出customMadePanel
Ext.define('customMadePanel', {
extend : 'Ext.form.Panel',
title : '定制字段',
collapsible : true,
items : [ {
itemId:'custom',
xtype : 'checkboxgroup',
fieldLabel : '選擇字段',
columns : 6,
items : []
}]
//collapsed:true,
});
var cusMadePanel=new customMadePanel();
我這種做法的不足也很明顯,makeCustomMadePanel函數(shù)中的循環(huán)生成checkbox組件太耗時了,38個組件足足花了好幾秒。。用戶體驗肯定不好。。
并且目前是在每次查詢完之后都根據(jù)查詢的結(jié)果生成一遍。。。我再想想好的解決辦法
今天對makeCustomMadePanel做了優(yōu)化,生成組件的速度與先前相比提升非常明顯!
function makeCustomMadePanel(fields,cl)
cusMade=1;
var x=cusMadePanel.getComponent('custom');
//console.log(cusMadePanel.getComponent('custom'));
var fie=[];
for(var i=0;i<fields.length;i++)
{
//x.add(
var temp=
{
xtype : 'checkboxfield',
boxLabel : cl[i].header,
//inputValue : fields[i].name,
checked:true,
itemId:i,
name : 'custom',
listeners : {
change : function(th, value, oldValue,eop) {
var t=grid_a.columnManager.headerCt.items.get(th.itemId);
//console.log(t.isVisible());
//console.log('break');
if(t.isVisible()){
t.setVisible(false);
}
else{
t.setVisible(true);
}
//console.log(t.isVisible());
//var t1=grid_a.columnManager.headerCt.items.get(th.itemId);
//console.log(t1);
//grid_a.columns[3].setVisible(false);
}}
};
//console.log(temp);
fie.push(temp);
}
//console.log(fie);
x.add(fie);
思路就是先循環(huán)組好需要生成的組件對象,然后一次add,每一次add的開銷非常大,變?yōu)橐淮嗡俣日娴奶嵘撕芏嗪芏鄜
下面先上圖

接著前幾天做的工作,今天上午完成了定制字段,思路是在上面的普通查詢或者高級查詢結(jié)束以后,獲得了列的fields,columns等信息,然后交給一個處理函數(shù) makeCustomMadePanel,該函數(shù)用來生成checkboxgroup,生成的時候給它加上一個事件,原本以為checkbox會有類似于check的事件,結(jié)果API看了看貌似只有個change事件可以用,MD。。
下面貼下自己寫的 makeCustomMadePanel函數(shù)。。用來根據(jù)grid的列自動生成checkboxgroup(整個grid的標頭內(nèi)容等信息均從后臺得到,不管后臺發(fā)來一個什么表,都能生成一個checkboxgroup來控制列的隱藏顯示)
參數(shù)分別是gridpanel在reconfigure的時候用到的fields和columns,期中的var t=grid_a.columnManager.headerCt.items.get(th.itemId);是關(guān)鍵。。這句用來獲得grid_a的列信息。。貌似在api中查不到。網(wǎng)上找了幾中方法都不適合。又不想給每個列一個ID。這是在stackoverflow.com/上找到的。。http://stackoverflow.com/questions/20791685/extjs-4-how-do-i-hide-show-grid-columns-on-the-fly
復(fù)制代碼 代碼如下:
function makeCustomMadePanel(fields,cl)
{
var x=cusMadePanel.getComponent('custom');
//console.log(cusMadePanel.getComponent('custom'));
for(var i=0;i<fields.length;i++)
{
x.add(
{
xtype : 'checkboxfield',
boxLabel : cl[i].header,
inputValue : fields[i].name,
checked:true,
itemId:i,
name : 'custom',
listeners : {
change : function(th, value, oldValue,eop) {
var t=grid_a.columnManager.headerCt.items.get(th.itemId);
if(t.isVisible()){
t.setVisible(false);
}
else{
t.setVisible(true);
}
//grid_a.columns[3].setVisible(false);
}}
}
);
}
}
在給出customMadePanel
復(fù)制代碼 代碼如下:
Ext.define('customMadePanel', {
extend : 'Ext.form.Panel',
title : '定制字段',
collapsible : true,
items : [ {
itemId:'custom',
xtype : 'checkboxgroup',
fieldLabel : '選擇字段',
columns : 6,
items : []
}]
//collapsed:true,
});
var cusMadePanel=new customMadePanel();
我這種做法的不足也很明顯,makeCustomMadePanel函數(shù)中的循環(huán)生成checkbox組件太耗時了,38個組件足足花了好幾秒。。用戶體驗肯定不好。。
并且目前是在每次查詢完之后都根據(jù)查詢的結(jié)果生成一遍。。。我再想想好的解決辦法
今天對makeCustomMadePanel做了優(yōu)化,生成組件的速度與先前相比提升非常明顯!
復(fù)制代碼 代碼如下:
function makeCustomMadePanel(fields,cl)
cusMade=1;
var x=cusMadePanel.getComponent('custom');
//console.log(cusMadePanel.getComponent('custom'));
var fie=[];
for(var i=0;i<fields.length;i++)
{
//x.add(
var temp=
{
xtype : 'checkboxfield',
boxLabel : cl[i].header,
//inputValue : fields[i].name,
checked:true,
itemId:i,
name : 'custom',
listeners : {
change : function(th, value, oldValue,eop) {
var t=grid_a.columnManager.headerCt.items.get(th.itemId);
//console.log(t.isVisible());
//console.log('break');
if(t.isVisible()){
t.setVisible(false);
}
else{
t.setVisible(true);
}
//console.log(t.isVisible());
//var t1=grid_a.columnManager.headerCt.items.get(th.itemId);
//console.log(t1);
//grid_a.columns[3].setVisible(false);
}}
};
//console.log(temp);
fie.push(temp);
}
//console.log(fie);
x.add(fie);
思路就是先循環(huán)組好需要生成的組件對象,然后一次add,每一次add的開銷非常大,變?yōu)橐淮嗡俣日娴奶嵘撕芏嗪芏鄜
您可能感興趣的文章:
- Extjs grid添加一個圖片狀態(tài)或者按鈕的方法
- ExtJS[Desktop]實現(xiàn)圖標換行示例代碼
- 解決Extjs上傳圖片無法預(yù)覽的解決方法
- ExtJs之帶圖片的下拉列表框插件
- ExtJS 4.2 Grid組件單元格合并的方法
- ExtJS4的文本框(textField)使用正則表達式進行驗證(Regex)的方法
- ExtJS4給Combobox設(shè)置列表中的默認值示例
- ExtJS4 表格的嵌套 rowExpander應(yīng)用
- extJS中常用的4種Ajax異步提交方式
- extjs4 treepanel動態(tài)改變行高度示例
- ExtJS4中的requires使用方法示例介紹
- extjs4圖表繪制之折線圖實現(xiàn)方法分析
相關(guān)文章
ExtJs 學(xué)習(xí)筆記基礎(chǔ)篇 Ext組件的使用
昨天剛接觸到Extjs,簡單寫了篇學(xué)習(xí)筆記,今天繼續(xù)。2008-12-12Extjs TriggerField在彈出窗口顯示不出問題的解決方法
解決Extjs TriggerField在彈出窗口顯示不出問題2010-01-01javascript Ext JS 狀態(tài)默認存儲時間
通過ExtJS的源碼可以知道,ExtJS將Grid的自定義顯示列等自定義狀態(tài)信息存入Cookie中,默認時間為7天2009-02-02Ext4.2的Ext.grid.plugin.RowExpander無法觸發(fā)事件解決辦法
這篇文章主要介紹了Ext4.2的Ext.grid.plugin.RowExpander無法觸發(fā)事件解決辦法,本文中的事件指collapsebody和expandbody事件,需要的朋友可以參考下2014-08-08Extjs4 關(guān)于Store的一些操作(加載/回調(diào)/添加)
本文詳細介紹下關(guān)于加載和回調(diào)的問題、從一個store添加符合某條件記錄給另一個store中,感興趣的朋友可以參考下,希望對你有所幫助2013-04-04Extjs NumberField后面加單位實現(xiàn)思路
本文為大家介紹下在NumberField后面加單位,具體實現(xiàn)如下,感興趣的朋友可以參考下2013-07-07Extjs gridpanel 出現(xiàn)橫向滾動條問題的解決方法
Extjs gridpanel 出現(xiàn)橫向滾動條問題的解決方法,在gridpanel中加入以下代碼即可。2011-07-07