Return link data based on some key

Hello, I want to select/find links based on some key and then modify those link colors accordingly. To find related links, Is there any example codes to return link data based on some key? Thank you.

Are you using a TreeModel or a GraphLinksModel?

If you are using a TreeModel, calling Diagram.findLinkForKey should work if you give it the key of the child node.

If you are using a GraphLinksModel, you first need to have turned on automatically providing keys to link data by setting GraphLinksModel.linkKeyProperty to the name of the property on the link data object that you want to hold the key value.

Once you have the Link that you want, if you have Bindings on the relevant Shape.stroke or Shape.fill properties in the link template, then you can set their source property (or properties) on the Link.data object.

But remember to always make changes by calling model methods such as Model.set. And make all of the changes within a single Transaction per user-initiated action, for example by calling Diagram.commit or Model.commit.

There are lots of examples of all of this throughout the documentation and the samples. You might want to read
https://gojs.net/latest/intro/dataBinding.html#ChangingDataValues, and probably the rest of that page too. Here’s another useful page: https://gojs.net/latest/learn/graphObject.html

Thank you so much.

I can modify node color with this sets:
var node1 = myDiagram.model.findNodeDataForKey(“User”);
if (node1 !== null) myDiagram.model.setDataProperty(node1, “color”, “blue”);

but this one to change link color does not work:
var link1 = myDiagram.findLinksByExample({color: “red”}).first();
if (link1 !== null) myDiagram.model.setDataProperty(link1, {color: “green”});

This is the error I have:
Error: GraphLinksModel.setDataProperty:propname value is not an instance of string: [object Object]

You are confusing the Parts of a Diagram, including Nodes and Links, with the data in a Model.

Diagram.findLinksByExample returns a collection of Links.

But Model.setDataProperty takes a data object of a model, not a Node or a Link. And it takes three arguments, anyway. https://gojs.net/latest/api/symbols/Model.html#setDataProperty

So you need to change your code to say:

if (link1 !== null) myDiagram.model.setDataProperty(link1.data, "color", “green”);

And I would rename node1 to data1, just to avoid confusion.

See the diagram showing the separation between a Diagram and its Parts and the Model and its data at: https://gojs.net/latest/intro/dataBinding.html#RelationshipsOfPartsAndDataAndBinding

Thank you so much, have a great day!