2010-04-16 72 views
1

我正在使用Google Closure Library和goog.ui.tree来构建树结构GUI组件。它工作得非常好,但我想为每个叶子添加一些额外的控件(特别是goog.ui.Checkboxes)。Google Closure Library - 将非TreeNode子项添加到TreeNode

的问题是,Component.addChild已BaseNode被覆盖,使得每个附加的孩子作为子树节点,而不是一个子部件处理。实际上,如果您尝试添加除了实际树节点之外的其他任何内容作为子项,则会引发大量错误,因为遍历了这些子项,并调用了BaseNode特定的函数。

我必须承认我是一个封闭newb,但我认为必须有一些解决方法,对吧?基本上,我想要做的是在树中的每一片叶子旁边都会出现一堆复选框。

感谢, 安德烈亚斯

+0

您好安德烈亚斯,只是好奇,如果你有想过子代股票TreeNode包括你需要的额外的用户界面?我在想你应该可以在没有问题的情况下将TreeNode子类添加到树中。 – 2010-04-16 16:01:33

回答

1

除了更多的一般性意见,我留在你的问题,我发现goog.ui.tree.BaseNode下列财产可以进行简单的工作需要:

/** 
* Html that can appear after the label (so not inside the anchor). 
* @type {string} 
* @private 
*/ 
goog.ui.tree.BaseNode.prototype.afterLabelHtml_ = ''; 

这可以使用设置: - goog.ui.tree.BaseNode -

/** 
* Sets the html that appears after the label. This is useful if you want to 
* put extra UI on the row of the label but not inside the anchor tag. 
* @param {string} html The html. 
*/ 
goog.ui.tree.BaseNode.prototype.setAfterLabelHtml = function(html) 
+0

感谢Adam,这真的很有用,它让我滑倒了!我只是好奇,如果我能够通过插入HTML充分利用Checkbox类。我之前尝试做的是重写TreeNode的createDom()方法来插入一些html div,然后使用这些div的ID来标识传递给Checkboxes的render()方法的元素。问题在于,因为树没有默认扩展,html没有被渲染,因此无法在render()中使用(直到展开父窗口并显示树叶,div才会存在)。 – 2010-04-16 23:22:38

0

看起来像树节点的父类实现违反合同的一些与祖先类组件有关。

很明显,goog.ui.tree.BaseNode.addChildAt方法覆盖更改了父规范,因为它忽略了渲染布尔属性。

解决方法是通过展开您需要立即使用的树节点来强制渲染。你可以再次折叠它们之后。

对于这个组件来说,实现是有点蹩脚的。

tree = new goog.ui.tree.TreeControl('dammed useless node if show root = false'); 
tree.setShowRootNode(false); 
tree.render(); // at doc body 

ref = new goog.ui.tree.TreeNode('click me'); 
tree.add(ref); 
tree.expandAll(); 
// now you can attach your checkbox 
cb = new goog.ui.Checkbox(true); 
cb.renderBefore(ref.getLabelElement()); 
tree.collapseAll();