jqGrid and ASP.NET MVC - TreeGrid
This is probably the last post about jqGrid. TreeGrid is quite cool and very easy to use feature. It supports both the Nested Set model and the Adjacency model. I'm going to show only the Adjacency model, but all the differences lies in data handling.
We will start with jqGrid initlization function:
We will start with jqGrid initlization function:
<script type="text/javascript">As you can see, the grid is configured to perform POST request. If the request is for child node, it will have three additional parameters: nodeid - the id of the currently expanded node, parentid - the parent id of the currently expanded node, n_level - the level value of the currently expanded node. We also need to add corresponding fields to every cell array in rows array. Here is a simple example:
$(document).ready(function() {
$('#jqgTreeGrid').jqGrid({
//enable TreeGrid
treeGrid: true,
//set TreeGrid model
treeGridModel: 'adjacency',
//set expand column
ExpandColumn: 'Name',
//url from wich data should be requested
url: '/Home/FilesAdjacencyTreeGridData/',
//type of data
datatype: 'json',
//url access method type
mtype: 'POST',
//columns names
colNames: ['Path', 'Name', 'CreationTime', 'LastAccessTime', 'LastWriteTime'],
//columns model
colModel: [
{ name: 'Path', index: 'Path', width: 1, hidden: true, key: true },
{ name: 'Name', index: 'Name', align: 'left' },
{ name: 'CreationTime', index: 'CreationTime', align: 'left' },
{ name: 'LastAccessTime', index: 'LastAccessTime', align: 'left' },
{ name: 'LastWriteTime', index: 'LastWriteTime', align: 'left' }
],
//pager for grid
pager: $('#jqgpTreeGrid'),
//grid width
width: 'auto',
//grid height
height: 'auto'
});
});
</script>
/// <summary>Screenshot of our sample at work (you can download source code here):
/// Provides json data for TreeGrid in adjacency mode
/// </summary>
/// <param name="postData">POST parameters collection</param>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FilesAdjacencyTreeGridData(FormCollection postData)
{
DirectoryInfo root = null;
//Checking if we are expanding existing node
if (postData.AllKeys.Contains("nodeid"))
root = new DirectoryInfo(postData["nodeid"]);
else
root = new DirectoryInfo(@"D:\Books\");
//Getting childrens
var children = from child in root.GetFileSystemInfos()
orderby child is DirectoryInfo descending
select child;
//Preparing result
var filesData = new
{
page = 1,
total = 1,
records = children.Count(),
rows = (from child in children
select new
{
//table of cells values
cell = new object[] {
child.FullName,
child.Name,
child.CreationTime.ToString(),
child.LastAccessTime.ToString(),
child.LastWriteTime.ToString(),
//Level - based on '\' count
((from backslash in child.FullName
where backslash == '\\'
select backslash).Count() - 2),
//Parent id
root.FullName == @"D:\Books\" ? null : root.FullName,
//Is not expandable
child is FileInfo,
//Is expanded
false
}
}
).ToArray()
};
//Returning json data
return Json(filesData);
}