createTreeView

Supported from HBuilderX 2.7.12+

Introduce

Create a view with the specified viewId, and create a tab item at the same level as the Project Manager in the left area of the form. The content area of the tab is a tree control, which can load nodes by itself.

The viewId needs to be declared in the configuration extension point [views] (/ExtensionDocs/ContributionPoints/README.md#views) in the package.json file. The complete extended view process reference to How to register a new view? .

Parameter:

Name Type Description
viewId String Id of the view contributed using the extension point views.
options TreeViewOptions Options for creating the TreeView

Returns:No

Example

  1. class DemoTreeDataProvider extends hx.TreeDataProvider{
  2. constructor(demoData) {
  3. super();
  4. this._demoData = demoData;
  5. }
  6. getChildren(element) {
  7. let demoData = this._demoData;
  8. return new Promise(resolve => {
  9. if (!element) {
  10. resolve(demoData);
  11. } else {
  12. resolve(element.children);
  13. }
  14. });
  15. }
  16. getTreeItem(element) {
  17. return {
  18. label:element.name,
  19. collapsibleState:element.children ? 1 : 0,
  20. command:{
  21. command:element.children ? "":"extension.helloWorld",
  22. arguments:[
  23. element.name
  24. ]
  25. }
  26. }
  27. }
  28. }
  29. let demoData = [
  30. {
  31. name:"Root1",
  32. children:[
  33. {
  34. name:"child1"
  35. },
  36. {
  37. name:"child2"
  38. }
  39. ]
  40. },
  41. {
  42. name:"Root2",
  43. children:[
  44. {
  45. name:"child3",
  46. },
  47. {
  48. name:"child4"
  49. }
  50. ]
  51. }
  52. ]
  53. hx.commands.registerCommand("extension.helloWorld",function(param){
  54. hx.window.showInformationMessage("Selected TreeItem:" + param[0]);
  55. });
  56. hx.window.createTreeView("extensions.treedemo",{
  57. showCollapseAll:true,
  58. treeDataProvider:new DemoTreeDataProvider(demoData);
  59. });

TreeViewOptions

Options for creating a TreeView

Attribute list

Attribute name Type Description
showCollapseAll Boolean Whether to show collapse all action or not.
treeDataProvider TreeDataProvider A data provider that provides tree data.

TreeDataProvider

A data provider that provides tree data. You cannot instantiate the object directly, you need to write a subclass to implement this interface, and each custom treeDataProvider needs to implement the methods listed under this interface.

getChildren

Get the children of element or root if no element is passed.

Parameter

Name Type Description
element Any? The element from which the provider gets children. Can be undefined.

Returns

Type Description
Promise<Any[]> Children of element or root if no element is passed.

getTreeItem

Get TreeItem representation of the element

Parameter

Name Type Description
element Any? The element for which TreeItem representation is asked for.

Returns

Type Description
TreeItem TreeItem representation of the element.

onDidChangeTreeData

An optional event to signal that an element or root has changed. This will trigger the view to update the changed element/root and its children recursively (if shown). This property needs to be created by the developer when constructing the TreeDataProvider

Example:

  1. var hx = require("hbuilderx");
  2. class MyTreeViewProvider extends hx.TreeDataProvider {
  3. constructor() {
  4. super();
  5. this.dataChangeEmitter = new hx.EventEmitter();
  6. this.onDidChangeTreeData = this.dataChangeEmitter.event;
  7. }
  8. ... // other function
  9. }
  10. // Proactive notification of data changes
  11. provider.dataChangeEmitter.fire();

TreeItem

Store the display information of the node

Attribute list

Attribute name Type Description
collapsibleState Number Collapsible state of the tree item, 0: unexpandable; 1: expandable
label String A human-readable string describing this item.
contextValue String Context value of the tree item, when registering the right-click menu through the view/item/context of the menus extension point, use the viewItem variable in the when expression to control the menu display. Example: viewItem =='test'
command CommandInfo The Command that should be executed when the tree item is selected.
tooltip String The tooltip text when you hover over this item.

CommandInfo

Configure an information object required by a command

Attribute list

Attribute name Type Description
command String The id of the command to be executed
arguments any[] Parameters passed when executing the command