treeDatastore
Description
A set of treeDatastore methods
treeDatastore: TreeDatastoreMethods
Details
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): boolean | void - 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) - optional, 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 undefined 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): number - 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): number | void - checks whether the specified item has child items
- id - (string | number) - the id of the item
- Returns the number of child tasks (if exist), or undefined 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.hasChild(1);
// -> true
store.hasChild(9);
// -> false
The twin of treeDatastore.hasChild() is gantt.hasChild().
- getChildren (id): Array<number | string | object> - 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): boolean - 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): Array<number | string | object> - 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): number | string | null - 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 or null.
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): number | string | null - 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 or null.
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): number | string - returns the id of the parent item or 0
- 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): number - 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): void - sets the parent for an item. The parent id will be written to the property specified by
parentPropertyconfig, "item.parent" by default- item - (object) - the item's object
- newParentId - (string | number | null) - 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(store.getItem(9), 4);
// -> 3
The twin of treeDatastore.setParent() is gantt.setParent().
- eachItem (callback, parentId): void - iterates over all children of a specific item
- callback - (Function) - the callback function
- parentId? - (string | number) - optional, 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): void - 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): void - 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): void - 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.
- sort (field, desc, parent, silent): void - sorts items in the resource grid
- field - (string | Function) - the name of the column that the resource grid will be sorted by or a custom sorting function
- desc? - (boolean) - optional, specifies the sorting direction: true - descending sort and false - ascending sort. By default, false
- parent? - (string | number) - optional, the id of the parent item. Specify to sort only in that branch
- silent? - (boolean) - optional, specifies whether rendering should be invoked after reordering items
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}
]);
// sort the resource grid by the column
var resourceSortDirection = false;
function sortResources(){
resourceSortDirection = !resourceSortDirection;
gantt.getDatastore("resource").sort("text", resourceSortDirection)
gantt.render();
}
or you can define a custom function for sorting:
var resourceSortDirection = false;
function sortResources(){
resourceSortDirection = !resourceSortDirection;
gantt.getDatastore("resource").sort(function (resource1, resource2){
return resource1.id - resource2.id;
}, resourceSortDirection)
gantt.render();
}
The twin of treeDatastore.sort() is gantt.sort().
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.