treeDatastore

a set of treeDatastore methods

object treeDatastore;
Details

This is an experimental API that might be changed in the future.

Note, that Tasks and Links should be modified using the common API of Gantt. Modifying tasks or links directly in the datastore can produce unexpected results. Datastores are expected to be used for resources or other custom objects.

A new datastore can be created using createDatastore method.

TreeDatastore extends Datastore and has all of its methods. The extended API of the treeDatastore object provides the following methods and events:

Methods

  • move(sid, tindex, parent) - moves an item to the new position or to a new parent
    • sid - (string|number) the id of the item to move
    • tindex - (number) the index of the position that the item will be moved to (the index within a branch)
    • parent - (string|number) the parent id. If specified, the tindex will refer to the index in the 'parent' branch
    Returns false if the action has been canceled using onBeforeItemMove, returns true otherwise.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.move(6, -1, 2);// move 'John' from 'QA' to 'Development'


    The twin of treeDatastore.move() is gantt.moveTask().
    Calls the onBeforeItemMove, onAfterItemMove events, and all events of the refresh method.
  • getBranchIndex(id) - returns the index of an item in the branch
    • id - (string|number) the id of the item
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getBranchIndex(8);
    // -> 1


    The twin of treeDatastore.getBranchIndex() is gantt.getTaskIndex()
  • hasChild(id) - checks whether the specified item has child items
    • id - (string|number) the id of the item
    Returns true if the item has some child item(s). Otherwise, false.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.hasChild(1);
    // -> true
     
    store.hasChild(9);
    // -> false


    The twin of treeDatastore.hasChild() is gantt.hasChild().
  • getChildren(id) - returns the 1st-level child items of the specified parent branch
    • id - (string|number) the id of the parent branch
    Returns an array of children's ids.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getChildren(3);
    // -> [9, 10]
     
    store.getChildren(9);
    // -> [0]


    The twin of treeDatastore.getChildren() is gantt.getChildren().
  • isChildOf(childId, parentId) - checks whether an item is a child of a different item
    • childId - (string|number) the id of an item that you want to check as a child
    • parentId - (string|number) the id of an item that you want to check as a parent
    Returns true, if the item is a child of the specified parent item. Otherwise, false.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.isChildOf(9, 3);
    // -> true
     
    store.getChildren(9, 2);
    // -> false


    The twin of treeDatastore.isChildOf() is gantt.isChildOf().
  • getSiblings(id) - returns siblings of the specified item (including itself)
    • id - (string|number) the id of the item
    Returns an array with the ids of the item's siblings.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getSiblings(1);
    // -> [1,2,3,4]
     
    store.getSiblings(6);
    // -> [6]


    The twin of treeDatastore.getSiblings() is gantt.getSiblings().
  • getNextSibling(id) - returns the id of the next item of the same level
    • id - (string|number) the id of the item
    Returns the id of the next sibling.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getNextSibling(9);
    // -> 10
     
    store.getNextSibling(10);
    // -> null


    The twin of treeDatastore.getNextSibling() is gantt.getNextSibling().
  • getPrevSibling(id) - returns the id of the previous item of the same level
    • id - (string|number) the id of the item
    Returns the id of the previous sibling.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getPrevSibling(9);
    // -> null
     
    store.getPrevSibling(10);
    // -> 9


    The twin of treeDatastore.getPrevSibling() is gantt.getPrevSibling().
  • getParent(id) - returns the id of the parent item
    • id - (string|number) the id of the item
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.getParent(9);
    // -> 3
     
    store.getParent(1);
    // -> 0


    The twin of treeDatastore.getParent() is gantt.getParent().
  • calculateItemLevel(item) - calculates the level of nesting of an item
    • item - (object) the item's object
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.calculateItemLevel(store.getItem(9));
    // -> 1
     
    store.calculateItemLevel(store.getItem(1));
    // -> 0


    The twin of treeDatastore.calculateItemLevel() is gantt.calculateTaskLevel().
  • setParent(item, newParentId) - sets the parent for an item. The parent id will be writen to the property specified by `parentProperty` config, "item.parent" by default.
    • item - (object) the item's object
    • newParentId - (string|number) the id of the parent

    Use treeDatastore.move() in order to move task to a different parent. The setParent() method only writes the value to the property specified by the config, it won't update the internal state of the tree.

    gantt.createDatastore({
        name: gantt.config.resource_store,
        type: "treeDatastore",
        parentProperty: "parent", //
        initItem: function (item) {
            item.parent = item.parent || gantt.config.root_id;
            item[gantt.config.resource_property] = item.parent;
            item.open = true;
            return item;
        }
    });
     
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.setParent(9, 4);
    // -> 3


    The twin of treeDatastore.setParent() is gantt.setParent().
  • eachItem(callback, parentId) - iterates over all children of a specific item
    • callback - (function) the callback function
    • parentId - (string|number) the id of the parent
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.eachItem(function(item){
        console.log(item.text);
    });


    The twin of treeDatastore.eachItem() is gantt.eachTask().
  • eachParent(callback, startItem) - iterates over all parent items of the specified item
    • callback - (function) the callback function
    • startItem - (string|number) the id of the item the parent item of which should be iterated over
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.eachParent(function(item){
        console.log(item.text);
    }, 10);
    // -> "Sales"


    The twin of treeDatastore.eachParent() is gantt.eachParent().
  • open(id) - opens the branch with the specified id
    • id - (string|number) the branch id
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.open(1);


    The twin of treeDatastore.open() is gantt.open(). Calls the onItemOpen event.
  • close(id) - closes the branch with the specified id
    • id - (string|number) the branch id
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.parse([
        {id: 1, text: "QA", parent:null},
        {id: 2, text: "Development", parent:null},
        {id: 3, text: "Sales", parent:null},
        {id: 4, text: "Other", parent:null},
        {id: 5, text: "Unassigned", parent:4},
        {id: 6, text: "John", parent:1},
        {id: 7, text: "Mike", parent:2},
        {id: 8, text: "Anna", parent:2},
        {id: 9, text: "Bill", parent:3},
        {id: 10, text: "Floe", parent:3}
    ]);
     
    store.close(1);


    The twin of treeDatastore.close() is gantt.close(). Calls the onItemClose event.

Events

  • onBeforeItemMove(id, parent, tindex) - fires before an item is moved to a new position
    • id - (string|number) the id of the item to move
    • parent - (string|number) the parent id
    • tindex - (number) the index of the position in the parent branch that the item will be moved to
    Return false to prevent the default action of the event, otherwise true.
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeItemMove", function(id, parent, tindex){
        // your code here
        return true;
    });


    The twin of the onBeforeItemMove event of treeDatastore is the onBeforeTaskMove event of Gantt.
  • onAfterItemMove(id, parent, tindex) - fires after an item was moved to a new position
    • id - (string|number) the id of the item to move
    • parent - (string|number) the parent id
    • tindex - (number) the index of the position in the parent branch that the item will be moved to
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onAfterItemMove", function(id, parent, tindex){
        // your code here
    });


    The twin of the onAfterItemMove event of treeDatastore is the onAfterTaskMove event of Gantt.
  • onItemOpen(id) - fires on opening a branch
    • id - (string|number) the id of the branch
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onItemOpen", function(id){
        // your code here
    });


    The twin of the onItemOpen event of treeDatastore is the onTaskOpened event of Gantt.
  • onItemClose(id) - fires on closing a branch
    • id - (string|number) the id of the branch
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onItemClose", function(id){
        // your code here
    });


    The twin of the onItemClose event of treeDatastore is the onTaskClosed event of Gantt.
See also
Back to top