asp.net gridview中用checkbox全选的几种实现的区别
程序员文章站
2024-03-09 12:39:29
1、ext的grid ext.grid.checkboxcolumn = function(config){ config.id = config.id || 'ck';...
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秒完成,以上是测试结果,环境不同,效果也不同,这仅是我个人测试的结果,供大家参考
上一篇: 个人2012年总结 博客分类: 个人
下一篇: asp.net 分页潜谈