该文档特别指出getSelection()
返回一个包含所选节点 ID 的数组。如果没有选择节点,则返回一个空数组。选择没有排序。
由于目前选择多个节点的方式是长按,你可以自己保持选择顺序,通过监听选择事件:
var mySelectionOrder = [];
var previouslySelected = {};
network.on('select', function(p) {
var selected = {};
// add newly-selected nodes at end of mySelectionOrder
p.nodes.forEach(function(n) {
if ( ! previouslySelected[n]) { mySelectionOrder.push(n); }
selected[n] = true;
});
// remove newly-unselected entries from mySelectionOrder
mySelectionOrder = mySelectionOrder.filter(
function(e, i, a) { return selected[e]; });
// prepare for next run
previouslySelected = selected;
// output to console to make sure it works
console.log("Selection updated", mySelectionOrder);
});
(已编辑以修复 JS 代码;经测试可与此示例一起使用)