关键字: JavaScript javascript
dhtmlGrid是一个功能比较齐全的html Grid控件,在其profesonal版中生成支持公式编辑到cell级别,我只想动态求任意列的和 ,又不想花钱,就自己扩展了一下dhtmlgrid,使其支持在任意cell上添加求和公式,同时如果没个cell的值发生了改变,那么关联的 公式项会自动变化,跟excell有点像了,当然他的功能远不及excell强大。 1:扩展eXcell eXcell是dhmtlGrid所有的cell的基类,所有的cell都是扩展自eXcell,dhtml中自带的类型有readOnly(eXcell_ro),editable(eXcell_ed) 图片,link,等等可以自己看它的doc有介绍,我扩展的类就叫eXcell_formulas 2:扩展eXcell步骤:
代码
- function eXcell_formulas(cell){
- try{
- this.cell = cell;
- this.grid = this.cell.parentNode.grid;
- }catch(er){}
-
-
-
- this.edit = function(){
-
- }
-
-
-
- this.getValue = function(){
- return "";
- }
-
-
-
- this.setValue = function(val){
- if(val.toString().trim()=="")
- val = " ";
- this.cell.innerHTML = val;
- }
-
-
-
- this.detach = function(){
- this.setValue(this.obj.value);
- return this.val!=this.getValue();
- }
- }
- eXcell_formulas.prototype = new eXcell;
<script>render_code();</script> 这是扩展的骨架,要实现setValue,getValue,edit,detach四个方法,也可以继承已有的类 看看我的代码吧
代码
<script>render_code();</script> 3:修改grid部分
代码
-
- this.calcFormulas = function()
- {
- for(i = 0; i<this.getColumnCount();i++)
- {
- if("formulas" == this.getColType(i))
- {
- this._calcFormulasCol(i)
- }
- }
- };
- this._calcFormulasCol = function(colIdx)
- {
- for(j = 0; j < this.getRowsNum(); j++)
- {
- this.cells2(j, colIdx).calcValue();
- }
- };
- this.notifyCellChange = function(rowId,cellInd)
- {
-
- formulasCells = this.formulasObserver.getValue(rowId+"^"+cellInd);
-
- for(i = 0; i<formulasCells.length; i++)
- {
-
-
- formulasCells[i].calcValue();
- }
- };
-
<script>render_code();</script> 有关dhtmlGrid可以自己到http://www.scbr.com/docs/products/dhtmlxGrid/index.shtml下载 |