步驟二:添加甘特標(biāo)記和JS
轉(zhuǎn)到wwwroot并創(chuàng)建一個index.html文件。
在新創(chuàng)建的文件中創(chuàng)建一個簡單的甘特圖頁面。
注意,在這個演示中g(shù)antt文件是從CDN添加的,如果您擁有該組件的專業(yè)版則需要手動向項目中添加gantt文件。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link 
rel="stylesheet" type="text/css" />
<script src="http://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
// specifying the date format
gantt.config.date_format = "%Y-%m-%d %H:%i";
// initializing gantt
gantt.init("gantt_here");
// initiating data loading
gantt.load("/api/data");
// initializing dataProcessor
var dp = new gantt.dataProcessor("/api/");
// and attaching it to gantt
dp.init(gantt);
// setting the REST mode for dataProcessor
dp.setTransactionMode("REST");
});
</script>
</head>
<body>
<div id="gantt_here" style="width: 100%; height: 100vh;"></div>
</body>
</html>
當(dāng)加載頁面時除了初始化甘特圖數(shù)據(jù)外,還會立即調(diào)用數(shù)據(jù)加載并設(shè)置dataProcessor,因此用戶對甘特圖所做的所有更改都將保存到后端。目前后端還沒有實現(xiàn),所以以后會更有意義。
接下來轉(zhuǎn)到Program.cs并告訴應(yīng)用程序使用index.html頁面,為了做到這一點您需要配置應(yīng)用程序來提供來自wwwroot文件夾的靜態(tài)文件。為此,您需要添加app.UseDefaultFiles()方法。
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. 
// You may want to change this for production scenarios, 
// see //aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseDefaultFiles(); 
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
app.UseDefaultFiles()方法允許提供默認(rèn)文件,它將在wwwroot文件夾中搜索以下文件:
- index.html
- index.htm
- default.html
- default.htm
因此,您可以選擇其中的任何一個,而在本教程中使用“index.html”。UseDefaultFiles()只是一個url重寫器,它實際上并不服務(wù)于文件,為此您還需要添加UseStaticFiles()文件。
完成后,在運行應(yīng)用程序時頁面上應(yīng)該出現(xiàn)一個空的甘特。注意,右上角出現(xiàn)“無效數(shù)據(jù)”標(biāo)簽是因為調(diào)用了gantt.load(),因為仍然沒有適當(dāng)?shù)暮蠖藖硖峁?shù)據(jù)。當(dāng)控制器被實現(xiàn)時,gantt將能夠顯示任務(wù)和鏈接。
	 
現(xiàn)在基本部分已經(jīng)完成,是時候?qū)崿F(xiàn)后端了,讓我們從實現(xiàn)模型類開始然后繼續(xù)到WebAPI控制器。

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線