如何关闭qx.ui.treevirtual.TreeVirtual树的所有分支?报错nodeSetOpened非函数
Fixing the 'nodeSetOpened is not a function' Error in Qooxdoo TreeVirtual
Got it, let's fix this issue right away! The error 'nodeSetOpened is not a function' happens because you're calling this method on the TreeVirtual instance itself—but nodeSetOpened isn't a method of qx.ui.treevirtual.TreeVirtual. Instead, all node state management (like opened/closed status) is handled by the tree's data model (qx.ui.treevirtual.SimpleTreeDataModel), which has the methods you need.
Here's the corrected code
We'll adjust the code to call the method on the data model, plus some small optimizations to avoid repeated calls to getDataModel():
this._tree.addListener("dblclick", function(e) { // Cache the data model to avoid repeated lookups const treeModel = this._tree.getDataModel(); const rowCount = treeModel.getRowCount(); for (let i = 0; i < rowCount; i++) { const child = treeModel.getNode(i); // Simplify the condition (no need to compare `== true`) if (child.level === 1 && child.bOpened) { // Option 1: Use row index to close the node treeModel.setOpened(i, false); // OR Option 2: Use the node object directly (if you prefer) // treeModel.nodeSetOpened(child, false); } } }, this);
Key Explanations
- Why this works:
TreeVirtualis just the UI component—all node data and state (likebOpened) lives in its data model. The data model provides two valid methods to close nodes:setOpened(rowIndex, isOpened): Takes the row index of the node and a boolean to set its opened state.nodeSetOpened(node, isOpened): Takes the node object itself and a boolean to set its state.
- Optimization: Caching
treeModelandrowCountreduces redundant method calls, making your code a bit more efficient. - Cleaner condition:
child.bOpenedis already a boolean, so comparing it totrueis unnecessary.
内容的提问来源于stack exchange,提问作者Raymond
相关产品推荐
相关产品推荐

