DevExtreme Pivot Grid Data Binding - Mvc() or WebApi()
Mvc() 이용 :
Index.cshtml
----------------------------------------------------------------------------------------------------------------------------
@using DevExtremeMvcApp1.Models
@(Html.DevExtreme().PivotGrid<PGT_FactSalesQuotaResult>()
.ID("pivotGridContainer")
.AllowSortingBySummary(true)
.AllowFiltering(true)
.ShowBorders(true)
.ShowColumnGrandTotals(false)
.ShowRowGrandTotals(false)
.ShowRowTotals(false)
.ShowColumnTotals(false)
.FieldChooser(c => c.Enabled(true).Height(400))
.DataSource(d => d
.Store(s => s.Mvc().Controller("PivotGrid").LoadAction("PivotGridList").LoadParams(new { calendarYear = 9999 }))
.Fields(fields =>
{
fields.AddFor(m => m.Title)
.Width(120)
.Area(PivotGridArea.Row);
fields.AddFor(m => m.Date)
.Area(PivotGridArea.Column);
fields.AddFor(m => m.SalesAmountQuota)
.SummaryType(SummaryType.Sum)
.Format(Format.Currency)
.Area(PivotGridArea.Data);
})
)
)
----------------------------------------------------------------------------------------------------------------------------
PivotGridController.cs
----------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
// using 추가 : Models, Newtonsoft.Json
using DevExtremeMvcApp1.Models;
using Newtonsoft.Json;
namespace DevExtremeMvcApp1.Controllers
{
public class PivotGridController : Controller
{
// GET: PivotGrid
public ActionResult PivotGridList(short? calendarYear)
{
if (calendarYear == 9999)
{
calendarYear = null;
}
List<PGT_FactSalesQuotaResult> result;
using (PGTDataContext PGT = new PGTDataContext())
{
// stored procedure call
result = PGT.PGT_FactSalesQuota(calendarYear).ToList();
}
var resultJson = JsonConvert.SerializeObject(result);
return Content(resultJson, "application/json");
}
}
}
----------------------------------------------------------------------------------------------------------------------------
WebApi() 이용 :
Index.cshtml
----------------------------------------------------------------------------------------------------------------------------
@using DevExtremeMvcApp1.Models
@(Html.DevExtreme().PivotGrid<PGT_FactSalesQuotaResult>()
.ID("pivotGridContainerApi")
.AllowSortingBySummary(true)
.AllowFiltering(true)
.ShowBorders(true)
.ShowColumnGrandTotals(false)
.ShowRowGrandTotals(false)
.ShowRowTotals(false)
.ShowColumnTotals(false)
.FieldChooser(c => c.Enabled(true).Height(400))
.DataSource(d => d
.Store(s => s.WebApi().Controller("PivotGridApi").LoadAction("PivotGridList").LoadParams(new { calendarYear = 9999 }))
.Fields(fields =>
{
fields.AddFor(m => m.Title)
.Width(120)
.Area(PivotGridArea.Row);
fields.AddFor(m => m.Date)
.Area(PivotGridArea.Column);
fields.AddFor(m => m.SalesAmountQuota)
.SummaryType(SummaryType.Sum)
.Format(Format.Currency)
.Area(PivotGridArea.Data);
})
)
)
----------------------------------------------------------------------------------------------------------------------------
PivotGridApiController.cs - api controller로 만들어야 함.
----------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
// using 추가 : Models, DevExtreme.AspNet.Data, DevExtreme.AspNet.Mvc
using DevExtreme.AspNet.Data;
using DevExtreme.AspNet.Mvc;
using DevExtremeMvcApp1.Models;
namespace DevExtremeMvcApp1.Controllers
{
public class PivotGridApiController : ApiController
{
[HttpGet]
public HttpResponseMessage PivotGridList(short? calendarYear, DataSourceLoadOptions loadOptions)
{
if (calendarYear == 9999)
{
calendarYear = null;
}
List<PGT_FactSalesQuotaResult> result;
using (PGTDataContext PGT = new PGTDataContext())
{
// stored procedure call
result = PGT.PGT_FactSalesQuota(calendarYear).ToList();
}
return Request.CreateResponse(DataSourceLoader.Load(result, loadOptions));
}
}
}
----------------------------------------------------------------------------------------------------------------------------