Example #1
0
        public ActionResult Index()
        {
            // Get the model (setup) of the grid defined in the /Models folder.
            var gridModel = new FileJqGridModel();
            var grid      = gridModel.FileDataGrid;

            // Setting the DataUrl to an action (method) in the controller is required.
            // This action will return the data needed by the grid
            grid.DataUrl = Url.Action("DataRequested");
            grid.TreeGridSettings.Enabled = true;

            // set up the grid. make sure you call this in both the init controller (this controller)
            // and in the DataRequested controller specified by DataUrl
            SetUpGrid(grid);


            // Pass the custmomized grid model to the View
            return(View(gridModel));
        }
Example #2
0
        public JsonResult DataRequested()
        {
            string userName    = WebConfigurationManager.AppSettings["NetworkCredentialsUsername"];
            string password    = WebConfigurationManager.AppSettings["NetworkCredentialsPassword"];
            string domain      = WebConfigurationManager.AppSettings["NetworkDomain"];
            string networkPath = WebConfigurationManager.AppSettings["NetworkPath"];

            // Get both the grid Model and the data Model
            // The data model in our case is an autogenerated linq2sql database based on Northwind.
            var gridModel = new FileJqGridModel();
            var grid      = gridModel.FileDataGrid;
            //SetUpGrid(grid);

            string startingPath             = networkPath;
            JQGridTreeExpandData expandData = grid.GetTreeExpandData();

            if (expandData.ParentID != null)
            {
                // GetDirectoryFrom hash returns the full path of the folder per its key (in the ID field of the row)
                startingPath = GetDirectoryFromHash(expandData.ParentID);
            }

            // Take a snapshot of the file system.
            System.IO.DirectoryInfo dir;


            IEnumerable <DirectoryInfo> dirList;
            IEnumerable <FileInfo>      fileList;

            using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
            {
                if (unc.NetUseWithCredentials(startingPath, userName, domain, password))
                {
                    dir     = new System.IO.DirectoryInfo(startingPath);
                    dirList = dir.GetDirectories("*.*",
                                                 System.IO.SearchOption.TopDirectoryOnly);

                    fileList = dir.GetFiles("*.*",
                                            System.IO.SearchOption.TopDirectoryOnly);
                }
                else
                {
                    throw new Exception("what happened?");
                }
            }
            // ********************************************************************************************
            // when selecting rows, you need to specify some or all of the following reserved fields
            // tree_level - the level of the row in the tree hierarchy. 0 is for root row.
            // tree_leaf - if the row is leaf (no child rows). This will make it non-expandable and will have an alternative icon
            // tree_expanded - if the row is expanded by default or not
            // tree_parent - the primary key (ID) of the parent row (only applicable for child rows)
            // tree_loaded - true/false - if the tree nodes are already loaded. Can be used to show several levels expanded by default.
            // ********************************************************************************************
            var dirs = from directory in dirList
                       orderby directory.Name
                       select new DirectoryEntry
            {
                // Use Hashtable to make sure there is unique key per folder fullname
                ID            = AddDirectoryToHash(directory.FullName),
                IsDir         = true,
                Name          = "" + directory.Name + "",
                Size          = "[dir]",
                DateCreated   = directory.CreationTime.ToString("d"),
                LastModified  = directory.LastWriteTime.ToString("d"),
                tree_parent   = expandData.ParentID,
                tree_expanded = false,
                tree_leaf     = false,
                tree_level    = expandData.ParentLevel + 1
            };
            var files = from file in fileList
                        orderby file.Name
                        select new DirectoryEntry
            {
                // ID is the primary key and must be set to an unique key
                // GUIDs can be used without the "-" - not allowed for IDs in HTML
                ID           = Guid.NewGuid().ToString().Replace("-", ""),
                IsDir        = false,
                Name         = file.Name,
                Size         = file.Length.ToString(),
                DateCreated  = file.CreationTime.ToString("d"),
                LastModified = file.LastWriteTime.ToString("d"),
                tree_parent  = expandData.ParentID,
                tree_leaf    = true,
                tree_level   = expandData.ParentLevel + 1
            };

            var allFilesAndFolders = files.Concat(dirs)
                                     .AsQueryable()
                                     .OrderByDescending(f => f.Name)
                                     .OrderBy(f => f.IsDir == false);

            return(grid.DataBind(allFilesAndFolders));
        }