Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

angular - How to count nodes with property enabled?

I have a tree with Node object:

class Node implements ITreeNode {
  id?: any;
  name: string;
  children:? Node[],
  enabledState = new Subject<boolean>();

  toggle() {
    this.enabled = !this.enabled;
    this.enabledState.next(this.enabled);
  }
}

I want to know how much Nodes were enabled(selected). After each selection I push state. But how to count all enabled nodes in the tree? I think it is bad to idea to subscribe to all nodes.

question from:https://stackoverflow.com/questions/66060897/how-to-count-nodes-with-property-enabled

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What it′s "ITreeNode" in your code?

You can do something similar to this code:

public getEnableds(): number {
     let count = this.enabled ? 1 : 0;
     this.children(s).map(x => count += x.getEnableds());
     return count;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...