Welcome

首页 / 网页编程 / JSP / 改写dhtmlxgrid的calender

改写dhtmlxgrid的calender2007-05-30 javaresearch 作者:rautinee
email:rautinee@yahoo.com.cn 关键字: UI dhtmlxgrid
dhtmlxgrid是我在javaeye上面发现的一个不错的东东,并决定把dhtmlxgrid引入到我们的项目中来,但是在看calender部分的时候发现1.2 standard 版本的有问题,就是在选择日期的时候,点击翻月的小图标,日期选择界面会消失。但是1.0的pro版本没有这个问题,我看了一下代码,这应当与程序当中对detach的处理那块有关,无奈dhtmlxgrid的代码太长了,看的俺是头昏眼花,腿抽筋,几次乱改都未成功,只好想想看看有什么旁门左道可以替代他的功能。

我原先项目中的日期选择组件用的是popcalender.js,对他比较熟悉就从他下手,修改的方案如下:抛弃yahoo UI的calender组件,使用popcalender来替代,需要更改dhtmlxgrid_excell_calender.js和popcalender.js这两个文件。在原先的代码中,作者应该是通过调用detach来实现对cell的赋值的,但是calender有个特殊性,就是他需要翻月操作的,而据我观察,在双击调用出日期选择界面后,只要点击鼠标,即调用detach,执行里面的代码,这就是为什么点击翻月按钮,选择界面会关闭,而且detach只会执行一次。我采用的解决方案是修改pop当中的closecalender函数,他是在用户选择日期的时候调用的,在他里面对当前操作的cell进行赋值操作,这样,问题就会迎刃而解了。

先看dhtmlxgrid_excell_calender.js

js 代码
/*
Copyright Scand LLC http://www.scbr.com
This version of Software is free for using in GNU GPL applications. For other use or to get
Professional Edition please contact info@scbr.com to obtain license
*/

function eXcell_calendar(cell){
try{
this.cell = cell;
this.grid = this.cell.parentNode.grid;
}catch(er){}
this.edit = function(){
var arPos = this.grid.getPosition(this.cell);
popUpCalendar(this.cell, this.cell, "yyyy-mm-dd",-1,-1);
this.cell._cediton=true;
this.val=this.cell.val;

}
this.getValue = function(){
if(this.cell.val)return this.cell.val;
return this.cell.innerHTML.toString()._dhx_trim()
}


}
eXcell_calendar.prototype = new eXcell;
eXcell_calendar.prototype.setValue = function(val){


if(!val || val.toString()._dhx_trim()=="") val="";

this.cell.val=val.toString();
if(this.cell.val=="NaN"){
this.cell.val="";
this.cell.innerHTML=" ";

}
else if(this.cell.val=="")
{
this.cell.val="";
this.cell.innerHTML=" ";

}
else
{
this.cell.innerHTML = this.cell.val;
}

}

在popcalender当中主要是修改closeCalender这个函数,在他里面添加一下两段代码:

js 代码

//ctlToPlaceValue在这个地方就是grid当中的cell
ctlToPlaceValue.val = constructDate(dateSelected,monthSelected,yearSelected);
ctlToPlaceValue.innerHTML = ctlToPlaceValue.val;

还有就是在popUpCalendar函数中,在

crossobj.left = fixedX==-1 ? ctl.offsetLeft + leftpos : fixedX

前面加一句

js代码

leftpos<0?leftpos=0:leftpos=leftpos;

ok,get it!

ie6和firefox1.5测试通过,修改后的代码我打包了,希望能够对使用dhtmlxgrid的朋友提供些帮助,例子可以看samples目录下面的 复件 calendar_grid.html文件