Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hello Community,
I tried finding a way to order my network entity in the network entities services but could not find anything.
Do you have any ideas on how to do it?
My issue is that I have a network where the user wants to be able to move nodes in the network around.
But doing so "ruins" the order of the network.
If I move a child node that was first in the parent node to a different parent, it will always end up at the end of the new parent node.
I realize I could probably do this after the fact, when pushing the data to my mashup, but I wouldn't want it to cause any performance issues as some of my networks will get quite big.
My preferred way would be to order it by name.
Thanks,
Jens
Solved! Go to Solution.
The sorting and moving elements in a network is not doable as of today directly via services as we don't have something similar to InsertAtIndex or MoveToIndex type of services.
It's worth noting that the Network order is generated by the order of adding the elements - there's no order or index attribute per node.
The moving we do in Composer is just a graphic client-side move,and once the network it's saved you'll observe the whole object being PUT to the platform with the nodes in a different order.
As you detected, you have couple options:
0. sort in services in composer before displaying - as my colleague suggested. I haven't tested this, but it is worth investigating before jumping in the other options.
1. remove a node then add all its children back in the order you want, one by one. Basically the resulting order is driven by the order in which you insert elements. This I believe can be done with the existing services.
2. considering the fact we don't have an order attribute and the fact we store in XML - network being very simple as structure, you can use the E4X syntax (methods like insertChildBefore and more) to insert an element at a specific location in the XML structure. This is a bit more complex, but effectively it's really easy to implement
I am aware the E4X syntax is typically not that used, so a good quickstart page is this in case it's needed.
The following code assumes two children at the same level.
let str_EntityFileName="Networks_netw(3).xml"; //Export To Source Control of the Network entity;
let str_FileRepositoryName = "SystemRepository";
//1. Load the file as XML
let xml = Things[str_FileRepositoryName].LoadXML({
path: str_EntityFileName /* STRING */
});
//2. Retrieving network Connections
let connections = xml.*::Networks.*::Network.*::Connections;
//3. Locate the node I want to move
let nodeIWantToMove = connections.Connection.(@to=="asdasda");
//4. Duplicate this node as a new object because it's going to be deleted in the next step and it will become null
let newNode=new XML(nodeIWantToMove.toXMLString());
//5. Delete the node I want to move
delete nodeIWantToMove[0];
//6. Locate the target node
let nodeTarget = connections.Connection.(@to=="AuditPurgeScheduler")[0]
//7. Insert the node I want to move above the target node
connections.insertChildBefore(nodeTarget, newNode);
//8. Save the Network entity.Ready for Import.
Things[str_FileRepositoryName].SaveXML({
path: str_EntityFileName /* STRING */,
content: xml.toXMLString() /* XML */
});
Hope this helps
Hi @jensc.
What version of ThingWorx are you running? The most recent versions add the child at the beginning of the parent node.
Are you trying to implement your sort in the Composer or via services? In your mashup, you can sort within the hierarchy without having to change the order in which it is stored.
Regards.
--Sharon
Hello @slangley,
We are generally on 9.3.4, but also have some newer platforms.
I think we'll most likely go with sorting in a service as that seems to be the easiest way of ensuring that the structure looks the way the user expects.
Thanks,
Jens
The sorting and moving elements in a network is not doable as of today directly via services as we don't have something similar to InsertAtIndex or MoveToIndex type of services.
It's worth noting that the Network order is generated by the order of adding the elements - there's no order or index attribute per node.
The moving we do in Composer is just a graphic client-side move,and once the network it's saved you'll observe the whole object being PUT to the platform with the nodes in a different order.
As you detected, you have couple options:
0. sort in services in composer before displaying - as my colleague suggested. I haven't tested this, but it is worth investigating before jumping in the other options.
1. remove a node then add all its children back in the order you want, one by one. Basically the resulting order is driven by the order in which you insert elements. This I believe can be done with the existing services.
2. considering the fact we don't have an order attribute and the fact we store in XML - network being very simple as structure, you can use the E4X syntax (methods like insertChildBefore and more) to insert an element at a specific location in the XML structure. This is a bit more complex, but effectively it's really easy to implement
I am aware the E4X syntax is typically not that used, so a good quickstart page is this in case it's needed.
The following code assumes two children at the same level.
let str_EntityFileName="Networks_netw(3).xml"; //Export To Source Control of the Network entity;
let str_FileRepositoryName = "SystemRepository";
//1. Load the file as XML
let xml = Things[str_FileRepositoryName].LoadXML({
path: str_EntityFileName /* STRING */
});
//2. Retrieving network Connections
let connections = xml.*::Networks.*::Network.*::Connections;
//3. Locate the node I want to move
let nodeIWantToMove = connections.Connection.(@to=="asdasda");
//4. Duplicate this node as a new object because it's going to be deleted in the next step and it will become null
let newNode=new XML(nodeIWantToMove.toXMLString());
//5. Delete the node I want to move
delete nodeIWantToMove[0];
//6. Locate the target node
let nodeTarget = connections.Connection.(@to=="AuditPurgeScheduler")[0]
//7. Insert the node I want to move above the target node
connections.insertChildBefore(nodeTarget, newNode);
//8. Save the Network entity.Ready for Import.
Things[str_FileRepositoryName].SaveXML({
path: str_EntityFileName /* STRING */,
content: xml.toXMLString() /* XML */
});
Hope this helps
Hello @VladimirRosu,
Thank you for explaining how the network works and giving some options as to how it could be solved.
For now I think we'll go with the easiest way and just doing an infotable sort on the data in the service before pushing it to the mashup.
But an interesting idea for sure is the XML manipulation.
Thanks,
Jens