datastore
Description
一组 datastore 方法的集合
datastore: DatastoreMethods
Details
注意,任务(Tasks)和链接(Links)应通过标准的 Gantt API 进行管理。直接在 datastore 中修改任务或链接可能会引发意外问题。datastore 主要用于资源或其他自定义对象的管理。
您可以使用 createDatastore 方法创建一个新的 datastore。
datastore 对象提供以下 方法 和 事件:
方法
parse (data): void - 从数组加载数据
- data - (Array<object>) - 要加载的数据
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}
]);
sample Resource load diagram
datastore.parse() 补充了 gantt.parse()。
会触发 onBeforeParse、onItemLoading、onParse 和 onStoreUpdated 事件。
getItem (id): object | void - 通过 id 获取项目
- id - (string | number) - 项目的 id
var store = gantt.getDatastore(gantt.config.resource_store);
var resource = store.getItem(resourceId);
sample Resource load diagram
datastore.getItem() 类似于 gantt.getTask() 和 gantt.getLink()。
updateItem (id, item): void - 更新指定项目
- id - (string | number) - 项目的 id
- item? - (object) - 包含更新属性的对象
var store = gantt.getDatastore(gantt.config.resource_store);
var resource = store.getItem(resourceId);
resource.text = "modified";
store.updateItem(resourceId);
// or
store.updateItem(resourceId, { text: "modified" });
datastore.updateItem() 类似于 gantt.updateTask() 和 gantt.updateLink()。
会触发 onBeforeUpdate、onAfterUpdate 和 onStoreUpdated 事件。
removeItem (id): void - 删除指定项目
- id - (string | number) - 项目的 id
var store = gantt.getDatastore(gantt.config.resource_store);
store.removeItem(resourceId);
datastore.removeItem() 类似于 gantt.deleteTask() 和 gantt.deleteLink()。
会触发 onBeforeDelete、onAfterDelete 和 onStoreUpdated 事件。
isVisible (id): boolean - 判断指定项目是否可见(未被过滤器 隐藏)
- id - (string | number) - 项目的 id
返回 true 表示项目可见,否则为 false。
var store = gantt.getDatastore(gantt.config.resource_store);
if(store.isVisible(resourceId)){
console.log(resourceId);
}
datastore.isVisible() 类似于 gantt.isTaskVisible()。
getVisibleItems (): Array<object> - 获取所有可见项目的数组
var store = gantt.getDatastore(gantt.config.resource_store);
var items = store.getVisibleItems();
addItem (item, index): number | string - 向 datastore 添加新项目
- item - (object) - 项目对象
- index? - (number) - 插入位置(0 或更大)
var store = gantt.getDatastore(gantt.config.resource_store);
var itemId = store.addItem({
text: "Unassigned",
parent:4
});
datastore.addItem() 类似于 gantt.addTask() 和 gantt.addLink()。
会触发 onBeforeAdd、onAfterAdd 和 onStoreUpdated 事件。
changeId (oldId, newId): void - 更新项目的 id
- oldId - (string | number) - 当前 id
- newId - (string | number) - 新 id
var store = gantt.getDatastore(gantt.config.resource_store);
var itemId = store.addItem({
text: "Unassigned",
parent:4
});
// itemId 是新项目的临时客户端 id
// 保存到数据库后,更新客户端的 id:
store.changeId(itemId, "databaseId");
datastore.changeId() 类似于 gantt.changeTaskId() 和 gantt.changeLinkId()。
会触发 onIdChange 事件。
exists (id): boolean - 检查指定项目是否存在于 datastore 中
- id - (string | number) - 项目的 id
返回 true 表示存在,否则 false。
var store = gantt.getDatastore(gantt.config.resource_store);
if(store.exists(resourceId)){
console.log(resourceId);
}
datastore.exists() 类似于 gantt.isTaskExists() 和 gantt.isLinkExists()。
move (sindex, tindex): void - 将项目移动到新位置
- sindex - (number) - 当前索引
- tindex - (number) - 目标索引
var store = gantt.getDatastore(gantt.config.resource_store);
// 交换两个项目
var idA = 1;
var idB = 5;
var indexA = store.getIndexById(idA);
var indexB = store.getIndexById(idB);
store.move(indexB, indexA);
indexA = store.getIndexById(idA);
store.move(indexA, indexB);
datastore.move() 对应 gantt.moveTask()。
会触发 onStoreUpdated 事件。
clearAll (): void - 清空 datastore
var store = gantt.getDatastore(gantt.config.resource_store);
store.clearAll();
datastore.clearAll() 对应 gantt.clearAll()。
会触发 onClearAll、onBeforeStoreUpdate 和 onStoreUpdated 事件。
silent (callback): void - 执行代码时不触发 datastore API 事件
- callback - (Function) - 回调函数
var 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): void - 触发指定记录的事件重绘并应用过滤器
- id? - (string | number) - 可选,记录的 id
var store = gantt.getDatastore(gantt.config.resource_store);
store.refresh(itemId); // 重绘指定项目
store.refresh(); // 重绘所有项目
datastore.refresh() 对应 gantt.refreshTask() 和 gantt.refreshLink()。
会触发 onBeforeStoreUpdate、onBeforeFilter、onFilterItem、onFilter 和 onStoreUpdated 事件。
count (): number - 返回当前加载到 datastore 中的项目总数
var store = gantt.getDatastore(gantt.config.resource_store);
store.attachEvent("onParse", function(){
alert(store.count() + " items loaded");
});
datastore.count() 对应 gantt.getTaskCount() 和 gantt.getLinkCount()。
countVisible (): number - 返回当前可见项目的数量
var store = gantt.getDatastore(gantt.config.resource_store);
alert(store.countVisible() + " items are visible");
datastore.countVisible() 对应 gantt.getVisibleTaskCount()。
eachItem (callback): void - 遍历 datastore 中的所有项目
- callback - (Function) - 回调函数
var store = gantt.getDatastore(gantt.config.resource_store);
var searchItems = [];
store.eachItem(function(item){
if(!item.value){
searchItems.push(item);
}
});
datastore.eachItem() 对应 gantt.eachTask()。
filter (): void - 应用过滤器并更新可见项目列表
通常,此方法由 store.refresh() 自动调用。
var store = gantt.getDatastore(gantt.config.resource_store);
store.filter();
sort (field, desc, parent, silent): void - 对资源 grid 中的项目进行排序
- field - (string | Function) - 用于排序的列名或自定义排序函数
- desc? - (boolean) - 排序方向:true 为降序,false 为升序(默认 false)
- parent? - (string | number) - 限制排序到指定父项的分支
- silent? - (boolean) - 是否跳过排序后的渲染
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}
]);
// 切换列的排序方向
var resourceSortDirection = false;
function sortResources(){
resourceSortDirection = !resourceSortDirection;
gantt.getDatastore("resource").sort("text", resourceSortDirection)
gantt.render();
}
或者,您可以提供自定义排序函数:
var 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): Array<object> - 返回指定索引范围内的项目
- from - (number) - 起始位置
- to - (number) - 结束位置
var store = gantt.getDatastore(gantt.config.resource_store);
var itemsInViewPort = store.getIndexRange(5, 10); // 第5到第10个项目
getItems (): Array<object> - 返回 datastore 中的所有项目
var store = gantt.getDatastore(gantt.config.resource_store);
var items = store.getItems();
datastore.getItems() 对应 gantt.getTaskByTime() 和 gantt.getLinks()。
getIdByIndex (index): string | number | void - 返回指定索引处项目的 id,若无则返回
undefined- index - (number) - 项目位置
var store = gantt.getDatastore(gantt.config.resource_store);
var firstItem = store.getIdByIndex(0);
datastore.getIdByIndex() 对应 gantt.getTaskByIndex()。
getIndexById (id): number - 返回指定 id 项目的索引,找不到返回
-1- id - (string | number) - 项目 id
var store = gantt.getDatastore(gantt.config.resource_store);
var itemIndex = store.getIndexById(5);
datastore.getIndexById() 对应 gantt.getTaskIndex()。
getFirst (): string | number | null - 返回 datastore 中第一个项目的 id
var store = gantt.getDatastore(gantt.config.resource_store);
var firstId = store.getFirst();
getLast (): string | number | null - 返回 datastore 中最后一个项目的 id
var store = gantt.getDatastore(gantt.config.resource_store);
var lastId = store.getLast();
getNext (id): string | number | null - 返回指定项目之后的项目 id
- id - (string | number) - 当前项目 id
var store = gantt.getDatastore(gantt.config.resource_store);
var firstId = store.getFirst();
var secondId = store.getNext(firstId);
datastore.getNext() 对应 gantt.getNext()。
getPrev (id): string | number | null - 返回指定项目之前的项目 id
- id - (string | number) - 当前项目 id
var store = gantt.getDatastore(gantt.config.resource_store);
var prevId = store.getPrev(itemId);
datastore.getPrev() 对应 gantt.getPrev()。
destructor (): void - 清空 datastore 并移除所有事件处理器;调用后 datastore 不可再用
var store = gantt.getDatastore(gantt.config.resource_store);
store.destructor();
datastore.destructor() 对应 gantt.destructor()。
attachEvent (name, handler, settings): string - 绑定内部 datastore 事件处理器
- name - (string) - 事件名称,不区分大小写
- handler - (Function) - 处理函数
- settings? - (object) - 事件处理器的可选设置对象
var store = gantt.getDatastore(gantt.config.resource_store);
store.attachEvent("onAfterSelect", function(id){
gantt.refreshData();
});
datastore.attachEvent() 对应 gantt.attachEvent()。
callEvent (name, params): boolean - 触发内部事件
- name - (string) - 事件名称,不区分大小写
- params - (Array<any>) - 事件相关数据数组
var store = gantt.getDatastore(gantt.config.resource_store);
store.callEvent("CustomEvent", [param1, param2]);
datastore.callEvent() 对应 gantt.callEvent()。
detachEvent (id): void - 移除之前绑定的事件处理器
- id - (string) - 事件处理器的 id
var store = gantt.getDatastore(gantt.config.resource_store);
var handlerId = store.attachEvent("onAfterSelect", function(id){
gantt.refreshData();
});
// 移除事件处理器
store.detachEvent(handlerId);
datastore.detachEvent() 对应 gantt.detachEvent()。