/// <summary> /// Method to build grid body model using deep copy. /// </summary> /// <typeparam name="T">Model entity.</typeparam> /// <param name="gridModelBuilderEntity">Contains all required config info for grid construction.</param> /// <param name="dataSource">List of data to be displayed in the grid.</param> /// <returns>GridBodyModel.</returns> private GridBodyModel BuildGridBodyModel <T>(GridModelBuilderEntity gridModelBuilderEntity, IEnumerable <T> dataSource) where T : class { var gridBodyModel = new GridBodyModel(); // Build body only if dataSource has data. if (dataSource != null && dataSource.Count() > 0) { // Iterate through dataSource to build rows. for (int rowNum = 0; rowNum < dataSource.Count(); rowNum++) { var gridRowModel = new GridRowModel(); // Iterate through list of columns to build cells. if (gridModelBuilderEntity.Columns != null && gridModelBuilderEntity.Columns.Count > 0) { foreach (var column in gridModelBuilderEntity.Columns) { gridRowModel.Cells.Add(this.BuildGridRowCellModel(column, dataSource.ElementAt(rowNum), rowNum)); } } // Add the above constructed row to body. gridBodyModel.Rows.Add(gridRowModel); } // Assign common row properties to body. if (!string.IsNullOrEmpty(gridModelBuilderEntity.GridBodyRowProperty.CssClass)) { gridBodyModel.CssClass = gridModelBuilderEntity.GridBodyRowProperty.CssClass; } } return(gridBodyModel); }
/// <summary> /// Method to build grid header model for the grid using deep copy. /// </summary> /// /// <typeparam name="T">Model entity.</typeparam> /// <param name="gridModelBuilderEntity">Contains all required config info for grid construction.</param> /// <param name="dataEntity">Model entity containing data to be displayed.</param> /// <returns>GridHeaderModel.</returns> private GridHeaderModel BuildGridHeaderModel <T>(GridModelBuilderEntity gridModelBuilderEntity, T dataEntity) { var gridHeaderModel = new GridHeaderModel(); // Build header only if dataSource has data. if (dataEntity != null) { var dataEntityType = dataEntity.GetType(); // Build list of header cells by iterating through list of columns. if (gridModelBuilderEntity.Columns != null && gridModelBuilderEntity.Columns.Count > 0) { foreach (var column in gridModelBuilderEntity.Columns) { var gridHeaderCellModel = new GridHeaderCellModel(); gridHeaderCellModel.CssClass = column.HeaderCell.CssClass; gridHeaderCellModel.BindingColumnName = column.HeaderCell.BindingColumnName; gridHeaderCellModel.ColumnType = column.HeaderCell.ColumnType; gridHeaderCellModel.IsDisabled = column.HeaderCell.IsDisabled; gridHeaderCellModel.Label = column.HeaderCell.Label; gridHeaderCellModel.Width = column.HeaderCell.Width; // Now set AllowSorting on the column only if the column type is defined as text, image or link. // A text column can represent boolean data as well. For boolean data we have standard text and it is so chosen that // sorting will yield as result as sorting based on the boolean data it represents. // For image type column sorting will be allowed only if it being binded to boolean type datasource. For boolean data we // have standard image and its name is so kept that sorting based on image path will be same as sorting based on boolean // data that it represents. This care should be taken when renaming boolean images. // For link there is no column binding hence allow sorting only if SortColumnName is mentioned. if (column.HeaderCell.ColumnType == GridColumnType.Text || (column.HeaderCell.ColumnType == GridColumnType.Image && (dataEntityType.GetProperty(column.HeaderCell.BindingColumnName).PropertyType.Name.Equals("Bool") || dataEntityType.GetProperty(column.HeaderCell.BindingColumnName).PropertyType.Name.Equals("Boolean"))) || (column.HeaderCell.ColumnType == GridColumnType.Link && !string.IsNullOrEmpty(column.HeaderCell.SortColumnName))) { gridHeaderCellModel.AllowSorting = column.HeaderCell.AllowSorting; gridHeaderCellModel.SortColumnName = column.HeaderCell.SortColumnName; } else { gridHeaderCellModel.AllowSorting = false; } gridHeaderModel.Cells.Add(gridHeaderCellModel); } } if (!string.IsNullOrEmpty(gridModelBuilderEntity.GridHeaderRowProperty.CssClass)) { gridHeaderModel.CssClass = gridModelBuilderEntity.GridHeaderRowProperty.CssClass; } } return(gridHeaderModel); }
/// <summary> /// Method to build model for grid. /// </summary> /// <typeparam name="T">Entity type whose list has to be displayed.</typeparam> /// <param name="gridModelBuilderEntity">Contains all required grid configuration info.</param> /// <param name="dataSource">List of entities that has to be displayed.</param> /// <param name="gridDefaultAction">The default action name for the grid.</param> /// <returns>GridModel containing grid config info and data.</returns> public GridModel BuildGridModel <T>(GridModelBuilderEntity gridModelBuilderEntity, IEnumerable <T> dataSource, string gridDefaultAction) where T : class { var gridModel = new GridModel(); gridModel.GridContext = this.BuildGridContext(gridModelBuilderEntity, gridDefaultAction); gridModel.GridHeader = this.BuildGridHeaderModel(gridModelBuilderEntity, dataSource.FirstOrDefault()); gridModel.GridBodyModel = this.BuildGridBodyModel(gridModelBuilderEntity, dataSource); return(gridModel); }
/// <summary> /// Method to build grid context for the grid using deep copy. /// </summary> /// <param name="gridModelBuilderEntity">Contains all required config info for grid construction.</param> /// <param name="gridDefaultAction">The default action name for the grid.</param> /// <returns>GridContext.</returns> private GridContext BuildGridContext(GridModelBuilderEntity gridModelBuilderEntity, string gridDefaultAction) { // Build grid pager objec var gridPagerModel = new GridPagerModel(); gridPagerModel.CssClass = gridModelBuilderEntity.GridContext.GridPager.CssClass; gridPagerModel.CurrPageId = gridModelBuilderEntity.GridContext.GridPager.CurrPageId; gridPagerModel.PageSize = gridModelBuilderEntity.GridContext.GridPager.PageSize; gridPagerModel.TotalRecord = gridModelBuilderEntity.GridContext.GridPager.TotalRecord; gridPagerModel.NumberOfPagerElements = gridModelBuilderEntity.GridContext.GridPager.NumberOfPagerElements; // Build grid search info object. var gridSearchInfo = new GridSearchInfo(); gridSearchInfo.SearchAgainstCriteria = gridModelBuilderEntity.GridContext.SearchInfo.SearchAgainstCriteria; gridSearchInfo.SearchCriteriaList = new List <KeyValuePair <string, string> >(gridModelBuilderEntity.GridContext.SearchInfo.SearchCriteriaList); gridSearchInfo.SearchWithOr = gridModelBuilderEntity.GridContext.SearchInfo.SearchWithOr; gridSearchInfo.TextSearchKey = gridModelBuilderEntity.GridContext.SearchInfo.TextSearchKey; // Build grid sort info object. var gridSortInfo = new GridSortInfo(); gridSortInfo.SortOn = gridModelBuilderEntity.GridContext.SortInfo.SortOn; gridSortInfo.SortOrder = gridModelBuilderEntity.GridContext.SortInfo.SortOrder; // Build grid context var gridContext = new GridContext(); gridContext.AlternateRowCss = gridModelBuilderEntity.GridContext.AlternateRowCss; gridContext.CssClass = gridModelBuilderEntity.GridContext.CssClass; gridContext.DefaultAction = gridDefaultAction; gridContext.DisableGridSearch = gridModelBuilderEntity.GridContext.DisableGridSearch; gridContext.RowCss = gridModelBuilderEntity.GridContext.RowCss; gridContext.GridPager = gridPagerModel; gridContext.SearchInfo = gridSearchInfo; gridContext.SortInfo = gridSortInfo; return(gridContext); }
/// <summary> /// Method to return GridModel for watchlist grid. /// </summary> /// <param name="gridContext">The grid context containing search, sort and paging info.</param> /// <returns>The grid model required for grid construction.</returns> private GridModel CreateWatchListGridModel(GridContext gridContext) { var gridModelBuilder = new GridModelBuilder(); // Create grid search criteria from the grid context and retrieve list of watch list entities from the DB. var gridSearchCriteria = this.CreateGridSearchCriteriaEntity(gridContext); IWatchListBusiness iWatchListBusiness = new WatchListBusiness(); var watchListEntities = iWatchListBusiness.SearchWatchList(gridSearchCriteria, 0); var watchListModels = watchListEntities.Select(WatchListModel.ConvertWatchListEntityToModel).ToList(); // Grid context is already available. Just set the number of records in it. gridContext.GridPager.TotalRecord = gridSearchCriteria.RecordCount; // Create list of columns in the grid. var columns = new List <GridColumnModel> { new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "Name", Label = "Name" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "BseSymbol", Label = "BSE Symbol" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "NseSymbol", Label = "NSE Symbol" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "AltNameOne", Label = "Alt Name One" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "AltNameTwo", Label = "Alt Name Two" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "AltNameThree", Label = "Alt Name Three" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "TempName", Label = "Temp Name" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Link, Label = "Is Active?", SortColumnName = "IsActive" }, Links = watchListModels.Select(x => new List <GridLinkModel> { new GridLinkModel { Action = "WatchList/ChangeActiveStatus/" + x.WatchListID, Behaviour = GridActionBehaviour.PostSilent, ImagePath = ConfigHelper.GetBooleanImage(x.IsActive) } }).ToList() }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Link, Label = "Is Alert Required?", SortColumnName = "AlertRequired" }, Links = watchListModels.Select(x => new List <GridLinkModel> { new GridLinkModel { Action = "WatchList/ChangeAlertStatus/" + x.WatchListID, Behaviour = GridActionBehaviour.PostSilent, ImagePath = ConfigHelper.GetBooleanImage(x.AlertRequired) } }).ToList() }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "ModifiedOn", Label = "Modified On" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { BindingColumnName = "CreatedOn", Label = "Created On" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Link }, Links = watchListModels.Select(x => new List <GridLinkModel> { new GridLinkModel { Action = "WatchList/EditWatchList/" + x.WatchListID, Behaviour = GridActionBehaviour.Popup, Text = "Edit" }, new GridLinkModel { Action = "WatchList/DeleteWatchList/" + x.WatchListID, Behaviour = GridActionBehaviour.PostSilent, Text = "Delete" } }).ToList() } }; // Create grid model builder entity. var gridModelBuilderEntity = new GridModelBuilderEntity { Columns = columns, GridContext = gridContext }; // Build the grid context to be returned. return(gridModelBuilder.BuildGridModel(gridModelBuilderEntity, watchListModels, Url.Action(_DefaultGridAction))); }
private GridModel GetGridModel(IList <WatchListModel> watchListModels, int recordCount) { var columns = new List <GridColumnModel> { new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.CheckBox, IsDisabled = false } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Image, BindingColumnName = "AlertRequired", Label = "Alert Required?" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Text, BindingColumnName = "AlertRequired", Label = "Alert Required?" } }, new GridColumnModel { HeaderCell = new GridHeaderCellModel { ColumnType = GridColumnType.Link, Label = "Links" }, Links = watchListModels.Select(x => new List <GridLinkModel> { new GridLinkModel { Action = "test/test", AlertMessage = "Are you sure?", Behaviour = GridActionBehaviour.PostSilent, ImagePath = ConfigHelper. GetBooleanImage( x.AlertRequired) }, new GridLinkModel { Action = "http://google.com", Behaviour = GridActionBehaviour.PostSilent, Text = x.Name } }).ToList() } }; // Set the GridModelBuilderEntity var gridModelBuilderEntity = new GridModelBuilderEntity { Columns = columns, GridContext = new GridContext { SortInfo = new GridSortInfo { SortOn = "AlertRequired", SortOrder = SortDirection.Descending }, SearchInfo = new GridSearchInfo { TextSearchKey = "Test search" } } }; var gridModelBuilder = new GridModelBuilder(); return(gridModelBuilder.BuildGridModel(gridModelBuilderEntity, watchListModels, Url.Action(gridModelBuilderEntity.GridContext.DefaultAction))); }