KS Gantt甘特图控件通过递归加载无限层级的数据

Posted 帅刺猬之家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KS Gantt甘特图控件通过递归加载无限层级的数据相关的知识,希望对你有一定的参考价值。

从服务器拉下来的数据,是反序列后的对象数据,通过id和parentid可以组织成对象树,然后将对象树绑定到甘特图控件上。

    public class KSGanttHelper
    {
        #region 往界面添加项
        public static void FillDataToGanttControl(List<ConstructionTaskItemTree> taskItemTreeList, Gantt gantt)
        {
            gantt.SuspendItemLayout();
            taskItemTreeList.ForEach((taskItemTree) =>
            {
                if (taskItemTree.Children.Any())
                {
                    GroupItem rootGroup = gantt.AddGroup(taskItemTree.Item.name);
                    SetGroupProperty(rootGroup, taskItemTree.Item);//设置其他属性值
                    GetChildTaskItem(taskItemTree, rootGroup, gantt);
                }
                else
                {
                    TaskItem taskItem = gantt.AddTask(DateTime.Parse(taskItemTree.Item.startDate), DateTime.Parse(taskItemTree.Item.endDate), taskItemTree.Item.name);
                    SetTaskProperty(taskItem, taskItemTree.Item);
                }
            });
            gantt.ResumeItemLayout();
        }

        private static void GetChildTaskItem(ConstructionTaskItemTree taskTree, GroupItem parentGroup, Gantt gantt)
        {
            foreach (ConstructionTaskItemTree childItem in taskTree.Children)
            {
                if (childItem.Children.Any())
                {
                    GroupItem group = gantt.AddGroup(childItem.Item.name);
                    SetGroupProperty(group, childItem.Item);//设置其他属性
                    GetChildTaskItem(childItem, group, gantt);
                    parentGroup.AddChild(group);
                }
                else
                {
                    TaskItem childTaskItem = gantt.AddTask(DateTime.Parse(childItem.Item.startDate), DateTime.Parse(childItem.Item.endDate), childItem.Item.name);
                    SetTaskProperty(childTaskItem, childItem.Item);
                    parentGroup.AddChild(childTaskItem);
                }
            }
        }

        /// <summary>
        /// 绑定实体的属性到界面分组项上
        /// </summary>
        /// <param name="groupItem"></param>
        /// <param name="constructionTaskItem"></param>
        private static void SetGroupProperty(GroupItem groupItem, ConstructionTaskItem constructionTaskItem)
        {
            groupItem.SetProperty("wbs", constructionTaskItem.wbs);
            groupItem.SetProperty("id", constructionTaskItem.id);
            groupItem.SetProperty("hasModel", constructionTaskItem.hasModel);
        }

        private static void SetTaskProperty(TaskItem taskItem, ConstructionTaskItem constructionTaskItem)
        {
            taskItem.SetProperty("wbs", constructionTaskItem.wbs);
            taskItem.SetProperty("id", constructionTaskItem.id);
            taskItem.SetProperty("hasModel", constructionTaskItem.hasModel);
        }
        #endregion 往界面添加项

        #region 转换数据
        /// <summary>
        /// 将返回的数据对象转换成树结构的对象
        /// </summary>
        /// <param name="constructionTaskRootModel"></param>
        /// <returns></returns>
        public static List<ConstructionTaskItemTree> TaskRootModel2ConstructionTaskItemTree(ConstructionTaskRootModel constructionTaskRootModel)
        {
            List<ConstructionTaskItemTree> taskUITreeList = new List<ConstructionTaskItemTree>();
            if (constructionTaskRootModel == null)
            {
                return taskUITreeList;
            }

            if (constructionTaskRootModel.data.Any())
            {
                var rootTaskItemList = constructionTaskRootModel.data.Where(h => h.parentId == "0").ToList();
                rootTaskItemList.ForEach((h) =>
                {
                    ConstructionTaskItemTree rootTreeItem = new ConstructionTaskItemTree();
                    rootTreeItem.Item = h;
                    rootTreeItem.Parent = null;
                    var childItemList = constructionTaskRootModel.data.Where(r => r.parentId.Equals(h.id)).ToList();
                    childItemList.ForEach((k) =>
                    {
                        ConstructionTaskItemTree childTreeItem = new ConstructionTaskItemTree();
                        childTreeItem.Item = k;
                        childTreeItem.Parent = rootTreeItem;
                        rootTreeItem.Children.Add(childTreeItem);
                        GetChildTreeItem(childTreeItem, k, constructionTaskRootModel);
                    });

                    taskUITreeList.Add(rootTreeItem);
                });
            }
            return taskUITreeList;
        }

        private static void GetChildTreeItem(ConstructionTaskItemTree parentTreeItem, ConstructionTaskItem taskItem, ConstructionTaskRootModel constructionTaskRootModel)
        {
            var childItemList = constructionTaskRootModel.data.Where(r => r.parentId.Equals(taskItem.id)).ToList();
            childItemList.ForEach((k) =>
            {
                ConstructionTaskItemTree childTreeItem = new ConstructionTaskItemTree();
                childTreeItem.Item = k;
                childTreeItem.Parent = parentTreeItem;
                parentTreeItem.Children.Add(childTreeItem);
                GetChildTreeItem(childTreeItem, k, constructionTaskRootModel);
            });
        }
        #endregion 转换数据
    }

    public class ConstructionTaskItemTree
    {
        public ConstructionTaskItemTree Parent { get; set; }
        public ConstructionTaskItem Item { get; set; }
        public List<ConstructionTaskItemTree> Children { get; set; } = new List<ConstructionTaskItemTree>();
    }
public class ConstructionTaskItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 石围塘站
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string createUserId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string createDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string editUserId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string editDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string deleteFlag { get; set; }
        /// <summary>
        /// 地下三层车站
        /// </summary>
        public string memo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string projectId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string parentId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string taskId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string beforeTask { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string wbs { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string organizationId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string startDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string endDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string duration { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string modelList { get; set; }
        /// <summary>
        /// 是否完成 1.未完成,2.完成
        /// </summary>
        public int isComplete { get; set; }
        /// <summary>
        /// 是否关联模型1.未关联,2.关联
        /// </summary>
        public int hasModel { get; set; }
    }

    public class ConstructionTaskRootModel
    {
        /// <summary>
        /// 
        /// </summary>
        public StateModel state { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<ConstructionTaskItem> data { get; set; }
    }

 

以上是关于KS Gantt甘特图控件通过递归加载无限层级的数据的主要内容,如果未能解决你的问题,请参考以下文章

bootstrap和jQuery.Gantt的css冲突问题

Gantt/BTS 生产计划电子看板甘特图

Twproject Gantt开源甘特图功能扩展

甘特图Gantt入门

计划-甘特图 dhtmlx-gantt使用方式与踩坑记

计划-甘特图 dhtmlx-gantt使用方式与踩坑记