datastore
Description
一组 datastore 方法
datastore: DatastoreMethods
Details
Note,任务(Tasks)和链接(Links)应使用 Gantt 的通用 API of Gantt 进行修改。直接在 datastore 中修改任务或链接可能会产生意外结果。Datastores 预计用于资源或其他自定义对象。
可以使用 createDatastore 方法创建一个新的 datastore。 datastore 对象具有以下 methods 与 events:
Methods
parse (data): void
从数组加载数据
Parameters:
data- (Array<object>) - 需要加载的数据
Example:
gantt.$resourcesStore.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 }
]);
示例 资源使用示意图
datastore.parse() 的另一种写法是 gantt.parse()。
调用 onBeforeParse 、 onItemLoading 、 onParse 和 onStoreUpdated 事件。
getItem (id)
按 id 返回项
Parameters:
id- (string | number) - 项的 id
Returns: object | void - 项对象
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const resource = store.getItem(resourceId);
示例 资源加载示意图
datastore.getItem() 的同义方法是 gantt.getTask() 与 gantt.getLink()。
updateItem (id, item)
更新指定的项
Parameters:
id- (string | number) - 项的 iditem- (object) - 项的新对象
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const resource = store.getItem(resourceId);
resource.text = "modified";
store.updateItem(resourceId);
// 或
store.updateItem(resourceId, { text: "modified" });
datastore.updateItem() 的同义方法是 gantt.updateTask() 与 gantt.updateLink()。
调用 onBeforeUpdate、 onAfterUpdate 与 onStoreUpdated 事件。
removeItem (id)
删除指定项
Parameters:
id- (string | number) - 项的 id
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.removeItem(resourceId);
datastore.removeItem() 的同义方法是 gantt.deleteTask() 与 gantt.deleteLink()。
调用 onBeforeDelete、 onAfterDelete 与 onStoreUpdated 事件。
isVisible (id)
检查指定项是否通过筛选可见
Parameters:
id- (string | number) - 项的 id
Returns: boolean - 若可见则返回 true,否则返回 false
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
if(store.isVisible(resourceId)){
console.log(resourceId);
}
datastore.isVisible() 的同义方法是 gantt.isTaskVisible()。
getVisibleItems ()
返回当前可见的项数组
Returns: Array<object> - 可见项的数组
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const items = store.getVisibleItems();
addItem (item, index)
向数据存储中添加新项
Parameters:
item- (object) - 项对象index- (number) - 项将被添加到的位置(0 或更大)
Returns: number | string - 项的 id
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const itemId = store.addItem({
text: "Unassigned",
parent:4
});
datastore.addItem() 的同义方法是 gantt.addTask() 与 gantt.addLink()。
调用 onBeforeAdd、 onAfterAdd 与 onStoreUpdated 事件。
changeId (oldId, newId)
更改项的 id
Parameters:
oldId- (string | number) - 当前项的 idnewId- (string | number) - 新的项的 id
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const itemId = store.addItem({
text: "Unassigned",
parent:4
});
// itemId - 新项的临时客户端 id
// 一旦项保存到数据库 —— 使用新 id 来更新客户端:
store.changeId(itemId, "databaseId");
datastore.changeId() 的同义方法是 gantt.changeTaskId() 与 gantt.changeLinkId()。
调用 onIdChange 事件。
exists (id)
检查数据存储中是否存在指定项
Parameters:
id- (string | number) - 项的 id
Returns: boolean - 若存在则返回 true,否则返回 false
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
if(store.exists(resourceId)){
console.log(resourceId);
}
datastore.exists() 的同义方法是 gantt.isTaskExists() 与 gantt.isLinkExists()。
move (sindex, tindex)
将项移动到新位置
Parameters:
sindex- (number) - 当前项的位置索引tindex- (number) - 将要移动到的位置的索引
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
// 交换两项
const idA = 1;
const idB = 5;
const indexA = store.getIndexById(idA);
const indexB = store.getIndexById(idB);
store.move(indexB, indexA);
indexA = store.getIndexById(idA);
store.move(indexA, indexB);
datastore.move() 的同义方法是 gantt.moveTask()。
调用 onStoreUpdated 事件。
clearAll ()
清空数据存储
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.clearAll();
datastore.clearAll() 的同义方法是 gantt.clearAll()。
调用 onClearAll、 onBeforeStoreUpdate 与 onStoreUpdated 事件。
silent (callback)
在不触发数据存储 API 事件的情况下执行代码
Parameters:
callback- (Function) - 回调函数
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.silent(function(){
store.eachItem(function(item){
item.text += " modified";
store.updateItem(item.id);
});
});
store.refresh();
datastore.silent() 的同义方法是 gantt.silent()。
refresh (id)
触发指定记录的事件重绘,并运行筛选
Parameters:
id- (string | number) - 可选,记录的 id
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.refresh(itemId); // 重绘一个项
store.refresh(); // 重绘所有项
datastore.refresh() 的同义方法是 gantt.refreshTask() 与 gantt.refreshLink()。
调用 onBeforeStoreUpdate、 onBeforeFilter、 onFilterItem、 onFilter 与 onStoreUpdated 事件。
count ()
返回当前加载到数据存储中的项数量
Returns: number - 项的数量
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.attachEvent("onParse", function(){
alert(store.count() + " items loaded");
});
datastore.count() 的同义方法是 gantt.getTaskCount() 与 gantt.getLinkCount()。
countVisible ()
返回当前可见的项数量
Returns: number - 可见项的数量
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
alert(store.countVisible() + " items are visible");
datastore.countVisible() 的同义方法是 gantt.getVisibleTaskCount()。
eachItem (callback)
遍历数据存储中的所有项
Parameters:
callback- (Function) - 回调函数
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
let searchItems = [];
store.eachItem(function(item){
if(!item.value){
searchItems.push(item);
}
});
datastore.eachItem() 的同义方法是 gantt.eachTask()。
filter ()
运行筛选并更新可见项数组
通常,你不需要调用此方法,它会在 store.refresh() 时自动调用。
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
store.filter();
sort (field, desc, parent, silent)
在资源网格中对项进行排序
Parameters:
field- (string | Function) - 列名称或自定义排序函数desc- (boolean) - 指定排序方向:true - 降序,false - 升序parent- (string | number) - 父项的 idsilent- (boolean) - 指定排序后是否应调用渲染
Example:
const 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 }
]);
// 按列对资源网格进行排序
let resourceSortDirection = false;
function sortResources(){
resourceSortDirection = !resourceSortDirection;
gantt.getDatastore("resource").sort("text", resourceSortDirection)
gantt.render();
}
或者你也可以定义自定义排序函数:
let resourceSortDirection = false;
function sortResources(){
resourceSortDirection = !resourceSortDirection;
gantt.getDatastore("resource").sort(function (resource1, resource2){
return resource1.id - resource2.id;
}, resourceSortDirection)
gantt.render();
}
datastore.sort() 的同义方法是 gantt.sort()。
getIndexRange (from, to)
返回指定索引之间的记录
Parameters:
from- (number) - 开始记录的位置to- (number) - 结束记录的位置
Returns: Array<object> - 项的数组
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const itemsInViewPort = store.getIndexRange(5, 10);// 取得第5到第10项
getItems ()
返回数据存储的所有记录
Returns: Array<object> - 所有项的数组
Example:
const store = gantt.getDatastore(gantt.config.resource_store);
const items = store.getItems();
datastore.getItems() 的同义方法是 gantt.getTaskByTime() 与 gantt.getLinks()。