I want to color node based on certain condition

i am having id of nodes (key) , i want to highlight those node, for empty node the same condition works fine. ex :

    checkEmptyNodes() {
           const emptyNodes = [];
           const diagDetails =  this.myserv.getDiagramData();
           if (!!diagDetails.nodeDataArray) {
           diagram.startTransaction('checking empty nodes');
           diagram.nodes.each(n => {
                       if (!n.part.data.answer) {
                           emptyNodes.push(n);
                   }
           });
           diagram.highlightCollection(emptyNodes);
           console.log(emptyNodes);
          }   else {
           // data.answer = [];
          }
           diagram.commitTransaction('checking empty nodes');
           return emptyNodes;
         }  

the above code works but i have a similar code which is not giving any error but nodes are not getting highlighted

          changeNodeColor(broken){
            const brokenNodes = [];
            diagram.nodes.each(n => {
                     if (broken.includes(n.Zd.key)) {
                     diagram.startTransaction('checking hanging nodes');
                    brokenNodes.push(n);
                         }
                     });
                 console.log("data here nk data");
                 console.log(brokenNodes);
                 diagram.highlightCollection(brokenNodes);
                 diagram.commitTransaction('checking hanging nodes');
                 return brokenNodes;
              }

i have really no clue how the previous one work and why the second work does not . any gojs expert.

No knowledge of GoJS is needed to see improvements you must make.

Don’t use Zd. That’s a minified property name, and it is repeatedly stated in the documentation and in this forum that you should not use those internal names. Use only documented API property and method names: https://gojs.net/api.

Why are you calling startTransaction inside an iteration or loop? You didn’t before. There should always be a pairing of calls to startTransaction and commitTransaction (or rollbackTransaction). Those pairings are normally sequential, but they may nest.

im not able to follow , will be able to replace zk part with const node = diagram.findNodeForKey(broken[i]); even then either i dont see highlight effect or when i do it i dont know how to store prev stroke value to get back to normal when error is removed.

             changeNodeColor(broken){
           const brokenNodes = [];
           for(var i = 0;i < broken.length; i++ ){
                    const node = diagram.findNodeForKey(broken[i]);
                    console.log(node.data.stroke);
                   brokenNodes.push(node);
                // node.data.stroke = "red";
               }

               diagram.startTransaction("highlight");
             diagram.highlightCollection(brokenNodes);
             diagram.commitTransaction("highlight");

           return brokenNodes;
       }

That looks OK. What’s the problem? Have you read https://gojs.net/latest/intro/highlighting.html ?

BTW, good coding practice always checks the result of a “find…” method to make sure it didn’t return null.

i have checked the console output , it is not undefined but color is not changing , i have

                 highlightOnShape(status: boolean, shp: any) {
                       return status ? 'red' : shp.part.data.stroke;
                     }

in my create node function.
can you let me know how to make it work .

If that is your Binding conversion function on Shape.stroke, that looks good too.

A post was split to a new topic: Changing text in node