datastore

一组 datastore 方法的集合

object datastore;
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}
    ]);

    Related sample:  Resource load diagram

    datastore.parse() 补充了 gantt.parse()
    会触发 onBeforeParseonItemLoadingonParseonStoreUpdated 事件。
  • getItem (id): object | void - 通过 id 获取项目
    • id - (string | number) - 项目的 id
  • updateItem (id, item): void - 更新指定项目
    • id - (string | number) - 项目的 id
    • item? - (object) - 包含更新属性的对象
  • removeItem (id): void - 删除指定项目
    • id - (string | number) - 项目的 id
  • 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 或更大)
    返回新添加项目的 id。
  • 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
  • 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
  • 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 中的所有项目
  • 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()

事件

  • onItemLoading (item) - 从数据源加载项目时触发
    • item - (object) - 项目对象
    返回 false 可阻止默认事件行为,否则返回 true
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onItemLoading", function(item){
        if(item.valid){ // 根据自定义属性在加载时过滤项目
            return true;
        }
        return false;
    });


    datastore 的 onItemLoading 事件对应 Gantt 的 onTaskLoading 事件。
  • onBeforeParse (data) - 数据解析开始前触发
    • data - (Array <any>) - 加载的数据数组
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeParse", function(item){
        console.time("StoreParse");
    });
    store.attachEvent("onParse", function(item){
        console.timeEnd("StoreParse");
    });


    datastore 的 onBeforeParse 事件对应 Gantt 的 onBeforeParse 事件。
  • onParse (data) - 解析完成但在甘特图渲染前触发
    • data - (Array <any>) - 加载的数据数组
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeParse", function(item){
        console.time("StoreParse");
    });
    store.attachEvent("onParse", function(item){
        console.timeEnd("StoreParse");
    });


    datastore 的 onParse 事件对应 Gantt 的 onParse 事件。
  • onBeforeUpdate (id, item) - 项目更新前触发
    • id - (string | number) - 项目 id
    • item - (object) - 更新后的项目对象
    返回 false 可阻止默认事件动作,否则返回 true
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeUpdate", function(id, item){
        // 你的代码
        return true;
    });


    datastore 的 onBeforeUpdate 事件对应 Gantt 的 onBeforeTaskUpdateonBeforeLinkUpdate 事件。
  • onAfterUpdate (id, item) - 项目更新后触发
    • id - (string | number) - 项目 id
    • item - (object) - 更新后的项目对象
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onAfterUpdate", function(id, item){
        // 你的代码
    });


    datastore 的 onAfterUpdate 事件对应 Gantt 的 onAfterTaskUpdateonAfterLinkUpdate 事件。
  • onBeforeDelete (id, item) - 项目删除前触发
    • id - (string | number) - 项目 id
    • item - (object) - 项目对象
    返回 false 可阻止默认事件动作,否则返回 true
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeDelete", function(id, item){
        // 你的代码
        return true;
    });


    datastore 的 onBeforeDelete 事件对应 Gantt 的 onBeforeTaskDeleteonBeforeLinkDelete 事件。
  • onAfterDelete (id, item) - 项目删除后触发
    • id - (string | number) - 项目 id
    • item - (object) - 项目对象
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onAfterDelete", function(id, item){
        // 你的代码
    });


    datastore 的 onAfterDelete 事件对应 Gantt 的 onAfterTaskDeleteonAfterLinkDelete 事件。
  • onBeforeAdd (id, item) - 新项目添加到 datastore 前触发
    • id - (string | number) - 项目 id
    • item - (object) - 项目对象
    返回 false 可阻止默认事件动作,否则返回 true
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeAdd", function(id, item){
        // 你的代码
        return true;
    });


    datastore 的 onBeforeAdd 事件对应 Gantt 的 onBeforeTaskAddonBeforeLinkAdd 事件。
  • onAfterAdd (id, item) - 新项目添加到 datastore 后触发
    • id - (string | number) - 项目 id
    • item - (object) - 项目对象
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onAfterAdd", function(id, item){
        // 你的代码
    });


    datastore 的 onAfterAdd 事件对应 Gantt 的 onAfterTaskAddonAfterLinkAdd 事件。
  • onIdChange (id, newId) - 项目 id 变更时触发
    • id - (string | number) - 旧 id
    • newId - (string | number) - 新 id
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onIdChange", function(oldId, newId){
        // 你的代码
    });


    datastore 的 onIdChange 事件对应 Gantt 的 onTaskIdChange 事件。
  • onClearAll () - datastore 中所有项目移除后触发
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onClearAll", function(){
        // 你的代码
    });

    datastore 的 onClearAll 事件对应 Gantt 的 onClear 事件。
  • onBeforeStoreUpdate (id, item, action) - datastore 刷新前触发
    • id - (string | number | null) - 项目 id 或 null
    • item - (object | null) - 项目对象或 null
    • action - (string | null) - 动作类型 ("paint", "move", "add", "delete", null)
    返回 false 可阻止默认事件动作,否则返回 true
    此事件表示 datastore 项目需要重绘。`null` 表示整个 datastore 被更新。
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeStoreUpdate", function(id, item, action){
        // 你的代码
        return true;
    });

  • onStoreUpdated (id, item, action) - datastore 刷新后触发
    • id - (string | number | null) - 项目 id 或 null
    • item - (object | null) - 项目对象或 null
    • action - (string | null) - 动作类型 ("paint", "move", "add", "delete", null)
    此事件表示 datastore 项目需要重绘。`null` 表示整个 datastore 被更新。
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onStoreUpdated", function(id, item, action){
        // 你的代码
    });

  • onBeforeFilter () - 过滤器应用前触发
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeFilter", function(){
        console.time("filtering");
    });
    store.attachEvent("onFilter", function(){
        console.timeEnd("filtering");
    });

  • onFilter () - 过滤后更新可见项目时触发
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onBeforeFilter", function(){
        console.time("filtering");
    });
    store.attachEvent("onFilter", function(){
        console.timeEnd("filtering");
    });

  • onFilterItem (id, item) - 过滤过程中针对每个项目触发;返回 `false` 隐藏该项目
    • id - (string | number) - 项目 id
    • item - (object) - 项目对象
    返回 false 隐藏项目,否则返回 true
    var store = gantt.getDatastore(gantt.config.resource_store);
    store.attachEvent("onFilterItem", function(id, item){
        // 你的代码
        return true;
    });

    datastore 的 onFilterItem 事件对应 Gantt 的 onBeforeTaskDisplay 事件。
  • onDestroy () - 调用 datastore 的 destructor() 方法后触发
    var datastore = gantt.createDatastore({
        name: gantt.config.resource_store,
        type: "treeDatastore",
        initItem: function (item) {
            item.parent = item.parent || gantt.config.root_id;
            item[gantt.config.resource_property] = item.parent;
            item.open = true;
            return item;
        }
    });
     
    datastore.attachEvent("onDestroy", function(){
        alert("free custom resources");
    });
     
    datastore.destructor();

See also
Back to top