rappid / jointjs - 如何更改rect.clone()上的属性

时间:2017-12-15 12:24:58

标签: jointjs rappid

我是Rappid和Jointjs的新手,我正在努力学习语法/语境。 对于我的第一个实验,我只想循环一个计数器并创建一些矩形。在每个新的矩形上,我想改变文字,但无论我尝试什么,我似乎无法改变文本。 我的代码如下 - 感谢您的帮助。



My Rectangle test
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="joint.css" />
    <script src="../node_modules/jquery/dist/jquery.js"></script>
    <script src="../node_modules/lodash/index.js"></script>
    <script src="../node_modules/backbone/backbone.js"></script>
    <script src="../node_modules/graphlib/dist/graphlib.core.js"></script>
    <script src="../node_modules/dagre/dist/dagre.core.js"></script>

    <script src="../build/rappid.min.js"></script>
    <!-- <script src="joint.js"></script>-->
</head>
<body>
  <div id="myholder"></div>
  <div id="myholder-small"></div>
  <script type="text/javascript">

    var graph = new joint.dia.Graph;

    var paper = new joint.dia.Paper({
        el: $('#myholder'),
        width: 600,
        height: 800,
        model: graph,
        gridSize: 1
    });
   

    var rect = new joint.shapes.basic.Rect({
        position: { x: 10, y: 30 },
        size: { width: 100, height: 30 },
        attrs: { rect: { fill: 'blue' }, 
                 text: { text: 'my box', fill: 'white' } 
               }
    });
    
    
    
    for (i = 0; i < 5; i++) {

    	var rect2 = rect.clone();
    	rect2.translate(10,  (50 * i) );
    	rect2.attrs = { rect: {fill:'red'},
    		            text: { text: 'Node ' + i } };

		graph.addCells([rect2]);
    
    };

  </script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您应该通过element.attr()方法更改属性。

&#13;
&#13;
My Rectangle test
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="joint.css" />
    <script src="../node_modules/jquery/dist/jquery.js"></script>
    <script src="../node_modules/lodash/index.js"></script>
    <script src="../node_modules/backbone/backbone.js"></script>
    <script src="../node_modules/graphlib/dist/graphlib.core.js"></script>
    <script src="../node_modules/dagre/dist/dagre.core.js"></script>

    <script src="../build/rappid.min.js"></script>
    <!-- <script src="joint.js"></script>-->
</head>
<body>
  <div id="myholder"></div>
  <div id="myholder-small"></div>
  <script type="text/javascript">

    var graph = new joint.dia.Graph;

    var paper = new joint.dia.Paper({
        el: $('#myholder'),
        width: 600,
        height: 800,
        model: graph,
        gridSize: 1
    });
   

    var rect = new joint.shapes.basic.Rect({
        position: { x: 10, y: 30 },
        size: { width: 100, height: 30 },
        attrs: { rect: { fill: 'blue' }, 
                 text: { text: 'my box', fill: 'white' } 
               }
    });
    
    
    
    for (i = 0; i < 5; i++) {

    	var rect2 = rect.clone();
    	rect2.translate(10,  (50 * i) );
        rect2.attr({
          rect: { fill:'red' },
          text: { text: 'Node ' + i }
        });

        graph.addCells([rect2]);
    
    };

  </script>
</body>
</html>
&#13;
&#13;
&#13;