我新增了一个类,继承自GC.Spread.Sheets.CellTypes.Text,用于展开/收缩某些行,定义如下
function TreeNodeCellType(startNo, endNo) {
this.startNo = startNo;
this.endNo = endNo;
this.typeName = "TreeNodeCellType";
this.parentMap = {};
}
TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text;
TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
var level = options.sheet.rowOutlines.getLevel(options.row);
var nlevel = -1;
if (options.row < options.sheet.getRowCount() - 1) {
nlevel = options.sheet.rowOutlines.getLevel(options.row + 1);
}
var __w = w;
var __h = h;
var __x = x;
var __y = y;
var hoffset = (level + 2) * 12;
x += hoffset;
w -= hoffset;
GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
if (options.row < this.startNo
|| options.row >= this.endNo) {
return;
}
if (options.row == options.sheet.getRowCount() - 1) return; //last row
ctx.save();
ctx.fillStyle = "#FFE699";
ctx.fillRect(__x,__y,hoffset,__h);
ctx.restore();
if (nlevel > level) {
var key = options.row + "_" + options.col;
this.parentMap[key] = "1";
var collapsed = options.sheet.rowOutlines.isCollapsed(options.row + 1);
x--;
y += h / 2 - 3;
ctx.save();
ctx.fillStyle = "black";
ctx.beginPath();
if (collapsed) {
ctx.moveTo(x - 5, y);
ctx.lineTo(x, y + 3);
ctx.lineTo(x - 5, y + 6);
} else {
ctx.moveTo(x, y);
ctx.lineTo(x, y + 5);
ctx.lineTo(x - 5, y + 5);
}
ctx.fill();
ctx.restore();
}
else {
x--;
y += h / 2 - 3;
ctx.save();
ctx.restore();
}
};
// override getHitInfo to allow cell type get mouse messages
TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
return {
x: x,
y: y,
row: context.row,
col: context.col,
cellStyle: cellStyle,
cellRect: cellRect,
sheetArea: context.sheetArea,
sheet: context.sheet
};
}
TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
var level = hitinfo.sheet.rowOutlines.getLevel(hitinfo.row);
var hoffset = (level + 2) * 12 + hitinfo.cellRect.x;
if (this.parentMap[hitinfo.row + "_" + hitinfo.col] != null
&& hitinfo.x < hoffset
&& hitinfo.x > hoffset - 10
&& hitinfo.y < hitinfo.cellRect.y + hitinfo.cellRect.height
&& hitinfo.y > hitinfo.cellRect.y) {
var collapsed = hitinfo.sheet.rowOutlines.isCollapsed(hitinfo.row + 1);
hitinfo.sheet.rowOutlines.setCollapsed(hitinfo.row, !collapsed);
hitinfo.sheet.invalidateLayout();
hitinfo.sheet.repaint();
}
return true;
};
后面绑定了GC.Spread.Sheets.Events.SelectionChanging事件,在事件处理中,用到了spread.getActiveSheet().getActiveRowIndex()和spread.getActiveSheet().getActiveColumnIndex(),但每次获取的都是上一次点击的坐标
|
|