【亚伟-EASCloud系列】之KDTable实现树型展示的两种方式
1、添加空间行 kdtEntry.addRow(0); 2、设置表格树列深度 kdtEntry.getTreeColumn().setDepth(3); 3、设置数据行极次 kdtEntry.getRow(0).setTreeLevel(0); for(int i=1,size=kdtEntry.getRowCount();i<size;i++){ if(i%6==1){ kdtEntry.getRow(i).setTreeLevel(1); }else{ kdtEntry.getRow(i).setTreeLevel(2); } }
1、以表格某一列为原型,创建树型数据结构类,如上图“序号”列
class NumberExpandInfo { private String id;//节点ID private String number;//节点编码 private String longNumber;//节点长编码 private boolean isExpandStatus;//是否已展开 private int level;//节点极次 private boolean isLeaf;//是否叶子节点 private boolean isEffective;//是否明细(用于控制行背景着色) protected NumberExpandInfo() { id = null; number = null; longNumber = null; isExpandStatus = false; level = 0; isLeaf = false; isEffective = true; } public boolean isExpandStatus() { return isExpandStatus; } public void setExpandStatus(boolean isExpandStatus) { this.isExpandStatus = isExpandStatus; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getLongNumber() { return longNumber; } public void setLongNumber(String longNumber) { this.longNumber = longNumber; } public String toString() { return number; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public boolean isLeaf() { return isLeaf; } public void setLeaf(boolean isLeaf) { this.isLeaf = isLeaf; } public boolean isEffective() { return isEffective; } public void setEffective(boolean isEffective) { this.isEffective = isEffective; } } 2、创建单元格渲染器,继承com.kingdee.bos.ctrl.kdf.util.render.IBasicRender接口,实现draw方法 class BgItemTreeRender implements IBasicRender { private int TABSIZE;//绘图离表格左边框间隔 private int ICONSIZE;//图标大小 private int margin;//图标与表格边框间距 protected SimpleTextRender simpleRender;//文本渲染器 protected HashMap acctIdToPos; protected BgItemTreeRender() { TABSIZE = 8; ICONSIZE = 10; margin = 2; simpleRender = new SimpleTextRender(); acctIdToPos = new HashMap(); } protected void drawExpanded(Graphics g, int x, int y) { g.drawRect(x, y, ICONSIZE, ICONSIZE); int lineSize = ICONSIZE - 2 * margin; g.drawLine(x + margin, y + ICONSIZE / 2, x + margin + lineSize, y + ICONSIZE / 2); } protected void drawCollapsed(Graphics g, int x, int y) { g.drawRect(x, y, ICONSIZE, ICONSIZE); int lineSize = ICONSIZE - 2 * margin; g.drawLine(x + margin, y + ICONSIZE / 2, x + margin + lineSize, y + ICONSIZE / 2); g.drawLine(x + ICONSIZE / 2, y + margin, x + ICONSIZE / 2, y + margin + lineSize); } protected void drawLeaf(Graphics g, int x, int y) { int lineSize = ICONSIZE - 2 * margin; } protected boolean inRect(NumberExpandInfo numberExpandInfo, int x, int y) { String acctId = numberExpandInfo.getNumber(); Rectangle rec = (Rectangle) acctIdToPos.get(acctId); if (rec != null) return rec.getX() < (double) x && rec.getX() + rec.getWidth() > (double) x; else return false; } /** * 接口方法,取出单元格value,绘置树型展示效果 */ public void draw(Graphics graphics, Shape clip, Object obj, Style style) { if (obj instanceof NumberExpandInfo) { NumberExpandInfo numberExpandInfo = (NumberExpandInfo) obj; int ident = numberExpandInfo.getLevel() * TABSIZE; Rectangle rect = clip.getBounds(); int x = rect.x + ident; int y = rect.y + (rect.height - ICONSIZE) / 2; Rectangle iconRect = new Rectangle(x, y, ICONSIZE, ICONSIZE); acctIdToPos.put(numberExpandInfo.getNumber(), iconRect); simpleRender.draw(graphics, new Rectangle(x + ICONSIZE + TABSIZE, rect.y, rect.width - x - ICONSIZE - TABSIZE, rect.height), numberExpandInfo.getNumber(), style); paintIcon(graphics, numberExpandInfo, iconRect); } else if (obj != null) simpleRender.draw(graphics, clip, obj.toString(), style); } private void paintIcon(Graphics graphics, NumberExpandInfo numberExpandInfo, Rectangle iconRect) { if (numberExpandInfo.isLeaf()) drawLeaf(graphics, iconRect.x, iconRect.y); else if (numberExpandInfo.isExpandStatus()) drawExpanded(graphics, iconRect.x, iconRect.y); else if (!numberExpandInfo.isExpandStatus()) drawCollapsed(graphics, iconRect.x, iconRect.y); } } 3、初始化表格渲染器 BgItemTreeRender treeRender = new BgItemTreeRender(); kdtEntry.getColumn("treeCell").setRenderer(treeRender); 4、初始化单元格数据结构,并渲染 setTreeDisplayStyle(0, kdtEntry.getRowCount()-1);//onload时调用 /** * 初始化树节点列单元格数据结构,并渲染 */ protected void setTreeDisplayStyle(int start, int end) { IRow row = null; for(int i = start,count = end; i <= count; i++) { row = kdtEntry.getRow(i); if(row == null || row.getCell("treeCell") == null) continue; String id = row.getCell("treeCell").getValue().toString(); Object numberValue = row.getCell("treeCell").getValue(); String number = numberValue.toString(); String longNumber = row.getCell("treeCell").getValue().toString(); int level = number.split("\\.").length<=1?1:2; boolean isLeaf = number.split("\\.").length<=1?false:true; boolean isEffective = number.split("\\.").length<=1?false:true; NumberExpandInfo expandInfo = new NumberExpandInfo(); expandInfo.setId(id); expandInfo.setLevel(level); expandInfo.setLeaf(isLeaf); expandInfo.setEffective(isEffective); expandInfo.setNumber(number); expandInfo.setLongNumber(longNumber); expandInfo.setExpandStatus(true); row.getCell("treeCell").setValue(expandInfo); if(isEffective) row.getStyleAttributes().setBackground(Color.white); else row.getStyleAttributes().setBackground(new Color(230, 230, 230)); } } 5、初始化表格点击事件 kdtEntry.addKDTMouseListener(new KDTMouseListener(){ public void tableClicked(KDTMouseEvent e) { try { tblMain_tableClicked(e); } catch (Exception e1) { e1.printStackTrace(); } } }); /** * 鼠标单击事件,触发树节点开/合 * @param e * @throws Exception */ public void tblMain_tableClicked(KDTMouseEvent e) throws Exception { if (e.getType() == 1) { int rowIndex = e.getRowIndex(); int clickCount = e.getClickCount(); if (clickCount == 1) { IRow row = kdtEntry.getRow(rowIndex); if (row != null) { NumberExpandInfo expandInfo = (NumberExpandInfo) row .getCell("treeCell").getValue(); if (treeRender.inRect(expandInfo, e.getX(), e.getY()))//点击在节点开合图标内 setTreeDisplayStyle(row, expandInfo); } } } } /** * 控制某一树节点开/合 * @param row * @param expandInfo * @throws Exception */ protected void setTreeDisplayStyle(IRow row, NumberExpandInfo expandInfo) throws Exception { boolean isLeaf = expandInfo.isLeaf; if (!isLeaf) { kdtEntry.setRefresh(false); expandTable(row, expandInfo); kdtEntry.setRefresh(true); kdtEntry.reLayoutAndPaint(); } } /** * 开/合某一树节点及子节点 * * @param row * @param parentExpandInfo * @throws Exception */ private void expandTable(IRow row, NumberExpandInfo parentExpandInfo) throws Exception { if (row == null) return; IRow child = null; int parentLevel = parentExpandInfo.getLevel(); String longNumber = null; String parentLongNumber = null; NumberExpandInfo expandInfo = null; parentLongNumber = parentExpandInfo.getLongNumber(); boolean isExpandStatus = parentExpandInfo.isExpandStatus(); if (isExpandStatus) { parentExpandInfo.setExpandStatus(false); int rowIndex = row.getRowIndex() + 1; int rowCount = kdtEntry.getRowCount3(); do { if (rowIndex >= rowCount) break; child = kdtEntry.getRow(rowIndex); expandInfo = (NumberExpandInfo) child.getCell("treeCell") .getValue(); longNumber = expandInfo.getLongNumber(); if (!longNumber.startsWith((new StringBuilder()).append( parentLongNumber).append(".").toString())) break; child.getStyleAttributes().setHided(true); rowIndex++; } while (true); } else { parentExpandInfo.setExpandStatus(true); int rowIndex = row.getRowIndex() + 1; for (int rowCount = kdtEntry.getRowCount3(); rowIndex < rowCount; rowIndex++) { child = kdtEntry.getRow(rowIndex); expandInfo = (NumberExpandInfo) child.getCell("treeCell") .getValue(); longNumber = expandInfo.getLongNumber(); if (!longNumber.startsWith((new StringBuilder()).append( parentLongNumber).append(".").toString())) break; if (expandInfo.getLevel() - parentLevel != 1) continue; child.getStyleAttributes().setHided(false); if (!expandInfo.isLeaf()) expandInfo.setExpandStatus(false); } } }
尝试,发现一个问题树形出来了,但是不显示值呢,不清楚什么原因。
【亚伟-EASCloud系列】之KDTable实现树型展示的两种方式
一、利用KDTable的TreeColumn列实现优缺点 优点:标准实现方式,实现简单 缺点:不够清晰美观,且须多加一后汇总行效果图 实现步...
点击下载文档
本文2024-09-16 22:02:32发表“eas cloud知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-eas-45559.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章