2016-09-21 38 views
0

元素的name我有Rappid/jointJS问题图搜索的元素在jointJS

我在stencil.js 4种形状(2 basic.Circle和2 basic.Rect)与名称开头(basic.Circle ),结束(basic.Circle),活动(basic.Rect)和工作项目(basic.Rect)和我想在我的main.js 从我所有的图形获得基本形状的名称(我的意思是与attrs文本)“活动”。

这是模板描述为 “活动”:

new joint.shapes.basic.Rect({ size: { width: 5, height: 3 }, 
attrs: { 
    rect: { 
     rx: 2, ry: 2, width: 50, height: 30, 
     fill: '#0000FF' 
     }, 
     text: { text: 'Activity', fill: '#ffffff', 'font-size': 10, 
     stroke: '#000000', 'stroke-width': 0 } 
    } 
}), 

如何西港岛线我得到它?目前为止,我可以在图表中搜索的唯一方法是如果单元格具有basic.Circle类型(使用get('type') === 'basic.Circle'))。但使用Circle类型,我有两个项目:活动和工作项目。

是否很难搜索名称为“Activity”的图形元素?

预先感谢您

+0

你是什么意思按名字?你是否将它指定为模型的属性属性? –

+0

是@SajithDilshan我有属性“文本”。你能帮我访问活动吗?这是“活动”的模板描述:new joint.shapes.basic.Rect({width:5,height:3}, attrs:{ rect:{rx:2,ry:2, width:50,height:30, fill:'#0000FF' }, text:{text:'Activity',fill:'#ffffff','font-size':10,stroke:'#000000', 'stroke-width':0} } }), –

回答

1

我采取JSON格式元数据解决我的问题:

_.each(this.graph.getElements(), function(element) { 


      if(element.attributes.attrs["text"]["text"] == "Activity"){ 
      //alert("YEAHHHHHH"); 
      } 

      }); 
+1

JSON.stringify()和JSON.parse()行是多余的。 'element'变量已经是一个Javascript对象。 –

+0

谢谢@SajithDilshan,但如果(element.attrs [“text”] [“text”] ==“Activity”){ // alert(“YEAHHHHHH”); } 在我的情况下不起作用。你知道该怎么做吗? –

+1

它应该是if(element.attributes.attrs [“text”] [“text”] ==“Activity”)。 此外,您可以通过执行console.log(element)来检查元素对象的完整结构,并将此对象打印到浏览器控制台中。 –

1

您可以从下面的方法

var allElement = graph.getElements() 

下获得的所有元素(除了链接),如果你想获得与“活动”做如下

元素
var activityElements = []; 

allElement.forEach(elem => { 
    var textVal = elem.attributes.attrs.text.text; 
    if(textVal !== undefined && textVal === 'Activity') { 
     activityElements.push(elem); 
    } 
}); 

现在activityElements数组将包含您需要的所有元素。

1

,你可以使用API​​元素以及,element.attr('text')返回从造型text对象:{ text: 'Activity', fill: '#ffffff', 'font-size': 10, stroke: '#000000', 'stroke-width': 0 }

+0

自从我的解决方案运行以来,我没有对它进行测试,但感谢您的反馈。 –