Cytoscape.js通过编程将节点设置为活动状态

时间:2018-07-12 19:51:02

标签: javascript graph cytoscape.js

我需要在创建函数的地方设置选中的节点

cy.on('tap', function (e) {
    if (!e.target.id) {
        let x = e.position.x;
        let y = e.position.y;
        let new_node = {group: "nodes", data: {id: x + '_' + y}, position: {x: x, y: y}};
        cy.add(new_node);
        // make node selected (click on it)
    }
});

然后我将获得创建的节点数据,就像我从其他节点获取数据一样

selected = cy.$('node:selected').jsons();

我该怎么办?

2 个答案:

答案 0 :(得分:1)

解决方案很简单。我在初始化中添加了 selected:true

let new_node = {group: "nodes", data: {id: x + '_' + y}, position: {x: x, y: y}, selected: true};

答案 1 :(得分:0)

尝试一下

let new_node = {group: "nodes", data: {id: x + '_' + y}, position: {x: x, y: y},classes : 'selected'};

然后

cy.$('node.selected')