asp.net gridview中用checkbox全选的几种实现的区别
作者:
这几天为了改变客户端grid的全选效率问题,详细研究了ext中grid的全选和gridview中通过脚本实现的全选效率,总结一下,供大家参考,有错误的地方,希望大侠指正,小弟献丑了。
1、ext的grid
Ext.grid.CheckboxColumn = function(config){
config.id = config.id || 'ck';
config.columnId = config.id || 'ck';
return Ext.applyIf(config||{},{
init:function(grid){
grid.on('cellclick', this.onCellClick, this);
grid.on('headerclick',this.onHeaderClick,this);
}
,dataIndex:''
,header:'<div class="x-grid3-check-col"></div>'
,enableHeaderControl:true
,masterValue:false
,width:40
,align:'center'
,fixed:true
,headerUnchecked:'<div class="x-grid3-check-col"></div>'
,headerChecked:'<div class="x-grid3-check-col x-grid3-check-col-on"></div>'
,onHeaderClick:function(grid,columnIndex,event){
var cIndex = grid.getColumnModel().getIndexById(this.columnId);
var column = grid.getColumnModel().getColumnById(this.columnId);
if(cIndex == columnIndex && this.enableHeaderControl!==false){
var newValue = (typeof column.masterValue == "undefined")?this.masterValue:!column.masterValue;
column.masterValue = newValue;
var newHeader = newValue==true?this.headerChecked:this.headerUnchecked;
if(column.header != newHeader){
column.header = newValue==true?this.headerChecked:this.headerUnchecked;
grid.getColumnModel().fireEvent("headerchange",cIndex,newHeader);
}
grid.getView().updateHeaders();
if(this.dataIndex != ''){
var ct = grid.getStore().getCount();
for(var i=0;i<ct;i++){
this.toggleCheck(grid,i,columnIndex,newValue);
}
}
}
}
,onCellClick:function(grid,rowIndex,columnIndex,event){
var cIndex = grid.getColumnModel().getIndexById(this.columnId);
if(cIndex == columnIndex) this.toggleCheck(grid,rowIndex,columnIndex);
}
,toggleCheck:function(grid,rowIndex,columnIndex,newValue){
var td = grid.getView().getCell(rowIndex,columnIndex);
var record = grid.getStore().getAt(rowIndex);
var startValue = record.data[this.dataIndex];
if(this.dataIndex != ''){
var newValue = newValue||!Ext.fly(td).hasClass('x-grid3-check-col-on');
var e = {
grid: grid,
record: record,
field: this.dataIndex,
value: newValue,
originalValue: startValue,
row: rowIndex,
column: columnIndex,
cancel: false
};
if( (grid.fireEvent("beforeedit",e)!==false && !e.cancel)&&
(grid.fireEvent("validateedit",e)!==false && !e.cancel)){
record.set(this.dataIndex,newValue);
delete e.cancel;
grid.fireEvent("afteredit",e);
};
//计算选择列
//SetTransportWAndB()
//SetTransportInfo(grid);
}
}
,renderer:function(value,meta,record){
meta.css = 'x-grid3-check-col-td x-grid3-check-col';
if(value==true || value=='true' || value=='on' || value==1 || value=='1'|| value=="True"|| value=="true") meta.css += ' x-grid3-check-col-on';
return '<div class="x-grid3-check-col-inner"> </div>';
}
});
}
这是ext的grid单写checkbox框的实现,后面说明几种情况的效率
2、gridview的两种实现
a、
function CA1(){
var frm=document.Form1;
for (var i=0;i<frm.elements.length;i++)
{
var e=frm.elements[i];
if ((e.name!='Checkbox2') && (e.type=='checkbox'))
{
e.checked=frm.Checkbox2.checked;
if (frm.Checkbox2.checked)
{
hL(e);
}//endif
else
{
dL(e);
}//endelse
}//endif
}//endfor
}
b、
function selectAll(oCheckbox)
{
for(i=1;i<document.all.GridViewGoodsInfo.rows.length;i++)
{
//document.all.GridView1.rows(i).cell(0).children(0).checked=oCheckbox.checked;
GridViewGoodsInfo.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked; }
}
加上ext的grid自带选择框的四种方案中,ext自带方式效率最低,显示500多条记录全选需要时间最长,需要2分钟多钟,才能完成,修改为第一种方案后,1000条数据用时12秒多,在gridview的两种方法里,a的用时最少1000条5秒钟完成,b的稍慢些,6-7秒完成,以上是测试结果,环境不同,效果也不同,这仅是我个人测试的结果,供大家参考
Ext.grid.CheckboxColumn = function(config){
config.id = config.id || 'ck';
config.columnId = config.id || 'ck';
return Ext.applyIf(config||{},{
init:function(grid){
grid.on('cellclick', this.onCellClick, this);
grid.on('headerclick',this.onHeaderClick,this);
}
,dataIndex:''
,header:'<div class="x-grid3-check-col"></div>'
,enableHeaderControl:true
,masterValue:false
,width:40
,align:'center'
,fixed:true
,headerUnchecked:'<div class="x-grid3-check-col"></div>'
,headerChecked:'<div class="x-grid3-check-col x-grid3-check-col-on"></div>'
,onHeaderClick:function(grid,columnIndex,event){
var cIndex = grid.getColumnModel().getIndexById(this.columnId);
var column = grid.getColumnModel().getColumnById(this.columnId);
if(cIndex == columnIndex && this.enableHeaderControl!==false){
var newValue = (typeof column.masterValue == "undefined")?this.masterValue:!column.masterValue;
column.masterValue = newValue;
var newHeader = newValue==true?this.headerChecked:this.headerUnchecked;
if(column.header != newHeader){
column.header = newValue==true?this.headerChecked:this.headerUnchecked;
grid.getColumnModel().fireEvent("headerchange",cIndex,newHeader);
}
grid.getView().updateHeaders();
if(this.dataIndex != ''){
var ct = grid.getStore().getCount();
for(var i=0;i<ct;i++){
this.toggleCheck(grid,i,columnIndex,newValue);
}
}
}
}
,onCellClick:function(grid,rowIndex,columnIndex,event){
var cIndex = grid.getColumnModel().getIndexById(this.columnId);
if(cIndex == columnIndex) this.toggleCheck(grid,rowIndex,columnIndex);
}
,toggleCheck:function(grid,rowIndex,columnIndex,newValue){
var td = grid.getView().getCell(rowIndex,columnIndex);
var record = grid.getStore().getAt(rowIndex);
var startValue = record.data[this.dataIndex];
if(this.dataIndex != ''){
var newValue = newValue||!Ext.fly(td).hasClass('x-grid3-check-col-on');
var e = {
grid: grid,
record: record,
field: this.dataIndex,
value: newValue,
originalValue: startValue,
row: rowIndex,
column: columnIndex,
cancel: false
};
if( (grid.fireEvent("beforeedit",e)!==false && !e.cancel)&&
(grid.fireEvent("validateedit",e)!==false && !e.cancel)){
record.set(this.dataIndex,newValue);
delete e.cancel;
grid.fireEvent("afteredit",e);
};
//计算选择列
//SetTransportWAndB()
//SetTransportInfo(grid);
}
}
,renderer:function(value,meta,record){
meta.css = 'x-grid3-check-col-td x-grid3-check-col';
if(value==true || value=='true' || value=='on' || value==1 || value=='1'|| value=="True"|| value=="true") meta.css += ' x-grid3-check-col-on';
return '<div class="x-grid3-check-col-inner"> </div>';
}
});
}
这是ext的grid单写checkbox框的实现,后面说明几种情况的效率
2、gridview的两种实现
a、
function CA1(){
var frm=document.Form1;
for (var i=0;i<frm.elements.length;i++)
{
var e=frm.elements[i];
if ((e.name!='Checkbox2') && (e.type=='checkbox'))
{
e.checked=frm.Checkbox2.checked;
if (frm.Checkbox2.checked)
{
hL(e);
}//endif
else
{
dL(e);
}//endelse
}//endif
}//endfor
}
b、
function selectAll(oCheckbox)
{
for(i=1;i<document.all.GridViewGoodsInfo.rows.length;i++)
{
//document.all.GridView1.rows(i).cell(0).children(0).checked=oCheckbox.checked;
GridViewGoodsInfo.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked; }
}
加上ext的grid自带选择框的四种方案中,ext自带方式效率最低,显示500多条记录全选需要时间最长,需要2分钟多钟,才能完成,修改为第一种方案后,1000条数据用时12秒多,在gridview的两种方法里,a的用时最少1000条5秒钟完成,b的稍慢些,6-7秒完成,以上是测试结果,环境不同,效果也不同,这仅是我个人测试的结果,供大家参考