[controller]
private TreeGridProperties ConvertTreeGridObject(string gridProperty)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
TreeGridProperties gridProp = new TreeGridProperties();
foreach (KeyValuePair<string, object> ds in div)
{
var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (property != null)
{
Type type = property.PropertyType;
string serialize = serializer.Serialize(ds.Value);
object value = serializer.Deserialize(serialize, type);
property.SetValue(gridProp, value, null);
}
}
TreeGridColumn column = new TreeGridColumn();
column.Field = "taskID";
column.HeaderText = "taskID";
gridProp.Columns.Add(column);
return gridProp;
}
[view]
@(Html.EJ().TreeGrid("container")
.Mappers(mp => mp.ExportToExcelAction("Treegrid/ExportToExcel"))
.Columns(co =>
{
co.Field("progress").HeaderText("Progress").Add();
co.Field("taskName").HeaderText("Task Name").Add();
co.Field("startDate").HeaderText("Start Date").Add();
co.Field("endDate").HeaderText("End Date").Add();
co.Field("duration").HeaderText("Duration").Add();
})
)@(Html.EJ().ScriptManager()) |
[client side]
$("#count").ejNumericTextbox({
maxValue: 7,
minValue: 1,
});
$("#TreeGridContainer").ejTreeGrid({
toolbarClick: toolbarclick,
});
});
function toolbarclick(args) {
this.exportGrid = this["export"];
if (args.itemName.indexOf("Export") > -1) {
//Add the extra paramater to send to server
this.model["recordCount"] = $("#count").ejNumericTextbox("model.value");
}
if (args.itemName == "Excel Export") {
this.exportGrid('api/TreeGridExcelExport/ExcelExport', "", false);
args.cancel = true;
}
}
[server side]
public int RecordCount { get; set; }
[AcceptVerbs("Post")]
public void ExcelExport()
{
string gridModel = HttpContext.Current.Request.Params["TreeGridModel"];
TreeGridProperties gridProperty = ConvertGridObject(gridModel);
ExcelExport exp = new ExcelExport();
IEnumerable data = db.Table1.Take(RecordCount);
exp.Export(gridProperty,(IEnumerable)data, "ExcelExport.xlsx", ExcelVersion.Excel2010, new TreeGridExportSettings() { Theme = ExportTheme.FlatAzure });
}
private TreeGridProperties ConvertGridObject(string gridProperty)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
TreeGridProperties gridProp = new TreeGridProperties();
foreach (KeyValuePair<string, object> ds in div)
{
//Check and retrieve additional property here
if (ds.Key == "recordCount")
{
if (ds.Value == null)
{
RecordCount = 7;
}
else
{
RecordCount = (int)ds.Value;
}
continue; }
var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (property != null)
{
Type type = property.PropertyType;
string serialize = serializer.Serialize(ds.Value);
object value = serializer.Deserialize(serialize, type);
property.SetValue(gridProp, value, null);
}
}
return gridProp;
}
}
} |