Created
          June 13, 2013 08:13 
        
      - 
      
 - 
        
Save bumbu/5772040 to your computer and use it in GitHub Desktop.  
    Cytoscape.js - Adding compound element on the fly
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | $("#button").click(function(){ | |
| var list = cy2.filter(function (i, ele) { | |
| return (parseInt(ele.id().slice(1), 10) % 2 === 0); | |
| }); | |
| var nodes = []; | |
| // Create new parent | |
| nodes.push({group: "nodes", data: {id: "n0"}, position: {x: 0, y: 0}}); | |
| // Create copies of old nodes | |
| for (var i=list.size()-1; i >= 0; i--) { | |
| nodes.push({ | |
| group: "nodes", | |
| data: {id: "c1" + i, parent: "n0"}, | |
| position: {x: list[i].position('x'), y: list[i].position('y')} | |
| }); | |
| } | |
| // Remove old nodes | |
| list.remove(); | |
| // Add new nodes | |
| cy2.add(nodes); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Thanks!