项目管理工具----普加项目管理中间件(PlusProject )入门教程(6):如何自定义百分比计算

普加项目管理中间件提供了一个全面的Gantt图表解决方案,支持跨浏览器和跨平台应用。它内置了通用的百分比计算方法,同时允许用户通过特殊的接口进行自定义,以满足特定的百分比计算需求。文中提供的代码示例展示了如何同步和计算任务的完成百分比,包括对子任务和父任务的百分比完成状态的更新。
摘要由CSDN通过智能技术生成

普加项目管理中间件是用于跨浏览器和跨平台应用程序的功能齐全的 Gantt 图表,可满足项目管理应用程序的所有需求,是最完善的甘特图图表库。

普加甘特图内默认提供了一套通用的百分比汇总计算的方法,但是对于有特殊需求的用户,同样有特殊的接口可以来修改,从而满足用户的百分比计算的需求。

相关代码如下:

//自定义计算百分比模块
var MyPercentComplete = function (project) {
    this.project = project;
}
MyPercentComplete.prototype = {
    syncComplete: function (task) {
        this.syncParentComplete(task);
        this.syncChildrenComplete(task);
    },
    syncParentComplete: function (task) {
        var percentCompleteProperty = "Duration";
        var taskUID = task.UID;
        var parentTask = this.project.getParentTask(taskUID);
        if (parentTask != null && parentTask.UID != this.project.rootTaskUID) {
            var _PercentComplete = parentTask.PercentComplete;
            var cs = this.getChildrenAll(parentTask);
            var allDuration = 0, completeDuration = 0;
            for (var i = 0, l = cs.length; i < l; i++) {
                var c = cs[i];
                var duration = parseInt(c[percentCompleteProperty]);
                if (isNaN(duration)) duration = 0;
                var percentComplete = parseFloat(c["PercentComplete"]);
                if (isNaN(percentComplete)) percentComplete = 0;
                allDuration += duration;
                completeDuration += duration * percentComplete / 100;
            }
            parentTask["PercentComplete"] = parseInt(completeDuration / allDuration * 100);
            if (isNaN(parentTask["PercentComplete"])) parentTask["PercentComplete"] = 0;
            this.syncParentComplete(parentTask);
            if (_PercentComplete != parentTask.PercentComplete) {
                this.project.setTaskModified(parentTask, "PercentComplete");
            }
        }
    },
    syncChildrenComplete: function (task) {
        var percentCompleteProperty = "Duration";
        var children = this.getChildrenAll(task);
        var allDuration = 0, completeDuration = 0;
        for (var i = 0, l = children.length; i < l; i++) {
            var c = children[i];
            var duration = parseInt(c[percentCompleteProperty]);
            allDuration += duration;
        }
        completeDuration = allDuration * parseInt(task["PercentComplete"]) / 100;
        var isCompleteAll = allDuration == completeDuration;
        for (var i = 0, l = children.length; i < l; i++) {
            var c = children[i];
            var _PercentComplete = c.PercentComplete;
            var duration = parseInt(c[percentCompleteProperty]);
            if (isNaN(duration)) duration = 0;
            //if (isNaN(percentComplete)) percentComplete = 0;
            if (completeDuration <= 0) {
                c["PercentComplete"] = 0;
            }
            else {
                var d = completeDuration - duration;
                if (d >= 0) c["PercentComplete"] = 100;
                else {
                    c["PercentComplete"] = parseInt(completeDuration / duration * 100);

                    if (isNaN(c["PercentComplete"])) c["PercentComplete"] = 0;
                }
                completeDuration = d;
            }
            //工期为0的任务,特殊处理
            if (isCompleteAll) {
                c["PercentComplete"] = 100;
            }
            if (_PercentComplete != c.PercentComplete) {
                this.project.setTaskModified(c, "PercentComplete");
            }
        }
        //调节好之后,从子任务向上同步父任务
        for (var i = 0, l = children.length; i < l; i++) {
            var c = children[i];
            this.syncParentComplete(c);
        }
    },
    //百分比应该是纯子任务的工期计算, 摘要任务不参与    
    getChildrenAll: function (task) {
        var children = this.project.getChildTasks(task, true);
        var nodes = [];
        for (var i = 0, l = children.length; i < l; i++) {
            var c = children[i];
            if (c.Summary == 0) {
                nodes.push(c);
            }
        }
        return nodes;
    }
}
var pc = new MyPercentComplete(project);
project.setSchedulePercentComplete(pc);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值