BALKANGraph / FamilyTreeJS

Build family tree app with BALKAN FamilyTreeJS library. Family Tree also called a genealogy or a pedigree chart, is a chart representing family relationships in a conventional tree structure.
50 stars 16 forks source link

Assign a new ID to a node #41

Closed blavv closed 6 months ago

blavv commented 1 year ago

Is it possible to assign a new ID to a node that's already rendered on the family tree?

For instance, if "Bob" node has ID "_abc", I want to replace it with "12345" (generated on the backend server AFTER the node was created on the tree). Overriding generateID() doesn't work in my case.

ZornitsaPesheva commented 1 year ago

We have created this example: https://code.balkan.app/family-tree-js/change-id-on-update#JS

pandabytes commented 9 months ago

Hi @ZornitsaPesheva. This example you provide only shows how to override the generateID(). What @blavv is asking if it's possible to "replace" an existing node's id, and to propagate this id replacement to other nodes (i.e. updating partnerIds, fid, mid, etc...)?

My use case is similar to @blavv.

  1. Create a node Bob Smith in FamilyTreeJS, and its id is autogenerated to be _abc
  2. I save this node to my database
  3. Database autogenerates a new id and assign id 123 to the node John Smith
  4. My server responds to client with this mapping
    • {
      "ids": {
        "_abc": "123"
      }
      }
  5. Now I would like to update the node Bob Smith in FamilyTreeJS by changing its id from _abc to 123
plamen-peshev commented 8 months ago

There are two options:

Option 1. Use the finish argument from the latest version

family.onUpdateNode(function(args, finish){
    fetch('https://mydomain.com/generateId')
        .then(response => response.json())
        .then(id => {
            args.addNodesData[0].id = id;
            fetch('https://mydomain.com/update'); //post data from args
            finish();
        });
    return false;
});

Option 2. Update the server and return new ids, then updated the client side nodes

family.onUpdateNode(function(args){
    var that = this;
    fetch('https://mydomain.com/update')//post data from args
        .then(response => response.json())// return old id new id collection
        .then(old_new_ids => {
            for (var oldId in old_new_ids){
                that._get(oldId).id = old_new_ids[oldId];
            }
            that.draw();
        });
});
plamen-peshev commented 8 months ago

We have removed the "finish" parameter from the previous post

Created Server side code to demonstrate how to update the ids from auto generated ids on the server side

https://github.com/plamen-peshev/FamilyTreeJSGenerateNewNodesIdsFromServerSide