本教程为您提供有关如何在服务器端使用ASP.NET Core 2 创建Gantt的分步说明的实施Web API部分。
dhtmlxGantt是用于跨浏览器和跨平台应用程序的功能齐全的Gantt图表。可满足项目管理应用程序的所有需求,是最完善的甘特图图表库。它允许你创建动态甘特图,并以一个方便的图形化方式可视化项目进度。有了dhtmlxGantt,你可以显示活动之间的依赖关系,显示具有完成百分比阴影的当前任务状态以及组织活动到树结构。
dhtmlxGantt试用版
步骤4.实施Web API
现在该进行实际的REST API实施了。转到Startup.cs并启用MVC路由(如果尚未启用):
启动文件
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddDbContext<GanttContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));}//The method is called by the runtime. Use it to configure HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc();}
添加控制器
创建Controllers文件夹并创建三个空的API Controller:一个用于Tasks,另一个用于Links,另一个用于整个数据集:

任务控制器
让我们为Tasks创建一个控制器。它将为甘特任务定义基本的CRUD操作。
这个怎么运作:
- 在GET请求中,任务是从数据库中加载的,输出是任务的数据传输对象;
- 在PUT / POST请求中,任务作为WebAPITask类来自客户端。它们在dhtmlxGantt中以这种方式表示。因此,您应该将它们转换为EntityFramework(任务类)的数据模型的格式。之后,可以将更改保存在DatabaseContext中。
控制器/ TaskController.csusing System.Collections.Generic;using System.Linq;using Microsoft.EntityFrameworkCore;using Microsoft.AspNetCore.Mvc;using DHX.Gantt.Models;namespace DHX.Gantt.Controllers{ [Produces("application/json")] [Route("api/task")] public class TaskController : Controller { private readonly GanttContext _context; public TaskController(GanttContext context) { _context = context; } // GET api/task [HttpGet] public IEnumerable<WebApiTask> Get() { return _context.Tasks .ToList() .Select(t => (WebApiTask)t); } // GET api/task/5 [HttpGet("{id}")] public WebApiTask Get(int id) { return (WebApiTask)_context .Tasks .Find(id); } // POST api/task [HttpPost] public ObjectResult Post(WebApiTask apiTask) { var newTask = (Task)apiTask; _context.Tasks.Add(newTask); _context.SaveChanges(); return Ok(new { tid = newTask.Id, action = "inserted" }); } // PUT api/task/5 [HttpPut("{id}")] public ObjectResult Put(int id, WebApiTask apiTask) { var updatedTask = (Task)apiTask; var dbTask = _context.Tasks.Find(id); dbTask.Text = updatedTask.Text; dbTask.StartDate = updatedTask.StartDate; dbTask.Duration = updatedTask.Duration; dbTask.ParentId = updatedTask.ParentId; dbTask.Progress = updatedTask.Progress; dbTask.Type = updatedTask.Type; _context.SaveChanges(); return Ok(new { action = "updated" }); } // DELETE api/task/5 [HttpDelete("{id}")] public ObjectResult DeleteTask(int id) { var task = _context.Tasks.Find(id); if (task != null) { _context.Tasks.Remove(task); _context.SaveChanges(); } return Ok(new { action = "deleted" }); } }}
链接控制器
接下来,您应该为Links创建一个控制器:
控制器/LinkController.csusing System.Collections.Generic;using System.Linq;using Microsoft.EntityFrameworkCore;using Microsoft.AspNetCore.Mvc;using DHX.Gantt.Models;namespace DHX.Gantt.Controllers{ [Produces("application/json")] [Route("api/link")] public class LinkController : Controller { private readonly GanttContext _context; public LinkController(GanttContext context) { _context = context; } // GET api/Link [HttpGet] public IEnumerable<WebApiLink> Get() { return _context.Links .ToList() .Select(t => (WebApiLink)t); } // GET api/Link/5 [HttpGet("{id}")] public WebApiLink Get(int id) { return (WebApiLink)_context .Links .Find(id); } // POST api/Link [HttpPost] public ObjectResult Post(WebApiLink apiLink) { var newLink = (Link)apiLink; _context.Links.Add(newLink); _context.SaveChanges(); return Ok(new { tid = newLink.Id, action = "inserted" }); } // PUT api/Link/5 [HttpPut("{id}")] public ObjectResult Put(int id, WebApiLink apiLink) { var updatedLink = (Link)apiLink; updatedLink.Id = id; _context.Entry(updatedLink).State = EntityState.Modified; _context.SaveChanges(); return Ok(new { action = "updated" }); } // DELETE api/Link/5 [HttpDelete("{id}")] public ObjectResult DeleteLink(int id) { var Link = _context.Links.Find(id); if (Link != null) { _context.Links.Remove(Link); _context.SaveChanges(); } return Ok(new { action = "deleted" }); } }}
数据控制器
最后,您需要为数据操作创建一个控制器:
控制器/DataController.csusing System.Collections.Generic;using System.Linq;using Microsoft.AspNetCore.Mvc;using DHX.Gantt.Models;namespace DHX.Gantt.Controllers{ [Produces("application/json")] [Route("api/data")] public class DataController : Controller { private readonly GanttContext _context; public DataController(GanttContext context) { _context = context; } // GET api/data [HttpGet] public object Get() { return new { data = _context.Tasks.ToList().Select(t => (WebApiTask)t), links = _context.Links.ToList().Select(l => (WebApiLink)l) }; } }}
都准备好了。您可以运行该应用程序,并查看完整的Gantt。

是否想尝试DHTMLX Gantt来构建自己的Salesforce应用问我们的GitHub存储库,您可以在其中找到Salesforce的Gantt组件的完整源代码,并按照我们的视频指南中的步骤进行操作。
关产品推荐:
VARCHART XGantt:支持ActiveX、.Net等平台的C#甘特图控件
AnyGantt:构建复杂且内容丰富的甘特图的理想工具
jQuery Gantt Package:基于HTML5 / jQuery的跨平台jQuery Gantt包
phGantt Time Package:对任务和时间的分配管理的甘特图
APS帮助提升企业生产效率,真正实现生产计划可视化呈现与控制,快速有效响应不同场景的生产计划,提高准时交货能力,提高产能和资源利用率
想要购买dhtmlxGantt正版授权,或了解更多产品信息请点击【咨询在线客服】
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!