public static GridBoundColumnBuilder <TModel> ForeignKey <TModel, TValue>(this GridColumnFactory <TModel> factory, Expression <Func <TModel, TValue> > expression, IEnumerable data, string dataFieldValue, string dataFieldText, FilterRow filterRow) where TModel : class { GridBoundColumnBuilder <TModel> builder = factory.ForeignKey(expression, data, dataFieldValue, dataFieldText); filterRow.AddCell(new DropDownFilterCell(builder.Column.Member, data, dataFieldValue, dataFieldText)); return(builder); }
public void Bound_column_by_name() { var customer = new Customer { Id = 1 }; GridColumnFactory <Customer> factory = Factory <Customer>(); var builder = factory.Bound("Id"); Assert.IsAssignableFrom <GridBoundColumn <Customer, int> >(builder.Column); Assert.Equal(1, ((GridBoundColumn <Customer, int>)builder.Column).Value(customer)); }
private static bool CheckAndAddComboBoxColumn <TContext, TEntity>(GridColumnFactory <TEntity> cols, KeyValuePair <PropertyInfo, Attribute[]> p) where TContext : CachableDbContext <TContext>, new() where TEntity : class { var comboAttr = p.Value.FirstOrDefault(a => a.GetType() == typeof(ComboBoxColumnAttribute)) as ComboBoxColumnAttribute; if (comboAttr == null) { return(false); } var fkEntityType = comboAttr.ReferenceType; CreateForeignKeyColumn <TContext, TEntity>(cols, fkEntityType, p); return(true); }
public static GridButtonsColumnBuilder Buttons <TModel>(this GridColumnFactory <TModel> factory, Action <GridButtonsColumnFactory <TModel> > buttonAction, FilterRow filterRow = null) where TModel : class { if (filterRow != null) { filterRow.AddCell(new ButtonFilterCell()); } GridButtonsColumn <TModel> column = new GridButtonsColumn <TModel>(factory.Container); buttonAction(new GridButtonsColumnFactory <TModel>(column)); factory.Container.Columns.Add(column); return(new GridButtonsColumnBuilder(column)); }
static void CreateEnumColumn <TContext, TEntity>(GridColumnFactory <TEntity> cols, KeyValuePair <PropertyInfo, Attribute[]> p) where TContext : CachableDbContext <TContext>, new() where TEntity : class { List <KeyValuePair <int, string> > data = new List <KeyValuePair <int, string> >(); var values = Enum.GetValues(p.Key.PropertyType); foreach (var v in values) { var name = Enum.GetName(p.Key.PropertyType, v); var title = TitleAttribute.GetTitle(p.Key.PropertyType, name); if (string.IsNullOrEmpty(title)) { title = name; } data.Add(new KeyValuePair <int, string>((int)v, title)); } var col = cols.ForeignKey(p.Key.Name, data, "Key", "Value"); SetColumnProperties(p.Key, p.Value, col); }
private static void CreateForeignKeyColumn <TContext, TEntity>(GridColumnFactory <TEntity> cols, Type fkEntityType, KeyValuePair <PropertyInfo, Attribute[]> p) where TContext : CachableDbContext <TContext>, new() where TEntity : class { var fkEntityId = EntityReflectionHelper.GetIdProperty(fkEntityType); var fkEntityNameField = GetEntityNameField(fkEntityType); var data = new TContext().GetAll(fkEntityType).ToList(); //dynamic empty = new ExpandoObject(); //((IDictionary<string, object>)empty).Add(fkEntityId.Name, 0); //((IDictionary<string, object>)empty).Add(fkEntityNameField.Name, "انتخاب کنید ..."); if (data.Any()) { var t = data[0].GetType(); var first = t.GetConstructor(Type.EmptyTypes).Invoke(null); data.Insert(0, first); } var fkCol = cols.ForeignKey(p.Key.Name, data, fkEntityId.Name, fkEntityNameField.Name); SetColumnProperties(p.Key, p.Value, fkCol); }
public void Bound_column_by_name_and_type_data_row() { DataTable dataTable = new DataTable { Columns = { new DataColumn("ID", typeof(int)), new DataColumn("Name", typeof(string)) } }; GridColumnFactory <DataRowView> factory = Factory <DataRowView>(); var builder = factory.Bound(typeof(int), "ID"); Assert.Equal("ID", builder.Column.Title); dataTable.Rows.Add(1, "Test"); Assert.Equal(1, ((GridBoundColumn <DataRowView, int?>)builder.Column).Value(dataTable.DefaultView[0])); }
/// <summary> /// Creates a clickable column that will trigger a custom event to be handled by the view component. /// </summary> /// <param name="componentId">The id of the view component wrapper control.</param> /// <param name="eventName">The name of the custom event to be triggered (note: the full event name will be "componentId.eventName"</param> /// <param name="configureData">The strings or lamdas specifing the data model values to pass in the event.</param> /// <param name="template">The HTML to display for this column.</param> public static GridTemplateColumnBuilder <T> CustomEvent <T>(this GridColumnFactory <T> columnFactory, string componentId, string eventName, Action <DataBuilder <T> > configureData, string template) where T : class { var dataBuilder = new DataBuilder <T>(); configureData(dataBuilder); var sb = new StringBuilder(); sb.Append($"<div data-kendo-event-cid=\"{componentId}\" data-kendo-event-name=\"{eventName}\""); foreach (var item in dataBuilder.Items) { string name = Regex.Replace(item.Key, @"([A-Z])", "-$1").ToLower(); // convert camel case to hyphened string value = item.Value.Replace("\"", """).Replace("&", "&"); sb.Append($" data-kendo-param{name}=\"{value}\""); } sb.Append(" style = \"cursor: pointer\""); sb.Append(">"); sb.Append(template); sb.Append("</div>"); var templateStr = sb.ToString(); return(columnFactory.Template(templateStr)); }
private static void ConfigureColumns(GridColumnFactory <DataRow> columnFactory, IEnumerable <Field> displayFields, bool allowMultipleEntitySelection, bool hasTitleLink) { var currentTheme = HttpContext.Current.Session[HttpContextSession.CurrentCssTheme].ToString(); if (allowMultipleEntitySelection) { columnFactory .Bound(string.Empty) .Title(@"<input type=""checkbox"" name=""SelectHeader"" value="""" />") .Width(30) .HeaderHtmlAttributes(new { @class = "t-select-header" }) .ClientTemplate($@"<input type=""checkbox"" name=""SelectedArticlesIDs"" value=""<#= {FieldName.ContentItemId} #>"" />") .HtmlAttributes(new { @class = "t-select-cell" }) .Sortable(false); } columnFactory.Bound(typeof(decimal), FieldName.ContentItemId) .Title(FieldName.Id) .HtmlAttributes(new { @class = "id" }) .Width(30); columnFactory.Bound(typeof(decimal), FieldName.LockedBy) .Title(string.Format("<img src=\"{0}\" style=\"width: 16px; height: 16px;\" class=\"t-image\" title=\"{1}\" alt=\"{1}\" />", Url.ToAbsolute(PathUtility.Combine(SitePathHelper.GetThemeRootImageFolderUrl(currentTheme), "/grid/header_icons/lock.gif")), ArticleStrings.IsLockedHeaderTooltip )).Width(18) .HeaderHtmlAttributes(new { @class = "t-image-header" }) .ClientTemplate(string.Format( @"<img src=""{0}/{1}"" title=""{2}"" alt=""{2}"" class=""smallIcon"" />", SitePathHelper.GetThemeSmallIconsImageFolderUrl(currentTheme), "<#= " + FieldName.LockedByIcon + " #>", "<#= " + FieldName.LockedByTooltip + " #>" )).HtmlAttributes(new { style = "text-align: center;" }); columnFactory.Bound(typeof(bool), FieldName.Scheduled) .Title(string.Format("<img src=\"{0}\" style=\"width: 16px; height: 16px;\" class=\"t-image\" title=\"{1}\" alt=\"{1}\" />", Url.ToAbsolute(PathUtility.Combine(SitePathHelper.GetThemeRootImageFolderUrl(currentTheme), "/grid/header_icons/scheduled.gif")), ArticleStrings.IsScheduledTooltip )).Width(18) .HeaderHtmlAttributes(new { @class = "t-image-header" }) .ClientTemplate(string.Format( @"<img src=""{0}/{1}"" title=""{2}"" alt=""{2}"" class=""smallIcon"" />", SitePathHelper.GetThemeSmallIconsImageFolderUrl(currentTheme), "<#= " + FieldName.ScheduledIcon + " #>", "<#= " + FieldName.ScheduledTooltip + " #>" )).HtmlAttributes(new { style = "text-align: center;" }); columnFactory.Bound(typeof(bool), FieldName.Splitted) .Title(string.Format("<img src=\"{0}\" style=\"width: 16px; height: 16px;\" class=\"t-image\" title=\"{1}\" alt=\"{1}\" />", Url.ToAbsolute(PathUtility.Combine(SitePathHelper.GetThemeRootImageFolderUrl(currentTheme), "/grid/header_icons/splited.gif")), ArticleStrings.IsSplitedTooltip )).Width(18) .HeaderHtmlAttributes(new { @class = "t-image-header" }) .ClientTemplate(string.Format( @"<img src=""{0}/{1}"" title=""{2}"" alt=""{2}"" class=""smallIcon"" />", SitePathHelper.GetThemeSmallIconsImageFolderUrl(currentTheme), "<#= " + FieldName.SplittedIcon + " #>", "<#= " + FieldName.SplittedTooltip + " #>" )).HtmlAttributes(new { style = "text-align: center;" }); columnFactory.Bound(typeof(bool), FieldName.Visible) .Title(string.Format("<img src=\"{0}\" style=\"width: 16px; height: 16px;\" class=\"t-image\" title=\"{1}\" alt=\"{1}\" />", Url.ToAbsolute(PathUtility.Combine(SitePathHelper.GetThemeRootImageFolderUrl(currentTheme), "/grid/header_icons/invisible.gif")), ArticleStrings.IsInvisibleTooltip )).Width(18) .HeaderHtmlAttributes(new { @class = "t-image-header" }) .ClientTemplate(string.Format( @"<img src=""{0}/{1}"" title=""{2}"" alt=""{2}"" class=""smallIcon"" />", SitePathHelper.GetThemeSmallIconsImageFolderUrl(currentTheme), "<#= " + FieldName.VisibleIcon + " #>", "<#= " + FieldName.VisibleTooltip + " #>" )).HtmlAttributes(new { style = "text-align: center;" }); var i = 0; var relationCounters = new Dictionary <int, int>(); foreach (var displayField in displayFields) { ConfigureDynamicColumn(columnFactory, displayField, i == 0, hasTitleLink, relationCounters); i++; } columnFactory.Bound(typeof(string), FieldName.StatusTypeName).Title(ArticleStrings.Status).ClientTemplate("<#= " + FieldName.StatusTypeName + " #>"); columnFactory.Bound(typeof(DateTime), FieldName.Created).Title(ArticleStrings.Created).ClientTemplate("<#= " + FieldName.Created + " #>"); columnFactory.Bound(typeof(DateTime), FieldName.Modified).Title(ArticleStrings.Modified).ClientTemplate("<#= " + FieldName.Modified + " #>"); columnFactory.Bound(typeof(int), FieldName.ModifierLogin).Title(ArticleStrings.LastModifiedBy).ClientTemplate("<#= " + FieldName.ModifierLogin + " #>"); }
//Created By SK. public static GridBuilder <T> BuildGrid <T>( this GridBuilder <T> gridbuilder, string gridName, string ajaxController, string action, object routedValue, string indexID, string[] hiddenColumns = null, bool allowInsert = true, bool allowEdit = true, bool allowDelete = true, EnumFeatures feature = EnumFeatures.Exempt) where T : class { bool isEditable = true; bool isVisible = true; allowEdit &= (feature == EnumFeatures.Exempt) ? true : AccessControl.IsActionAccessible(HttpContext.Current.User, feature, EnumActions.Edit); allowInsert &= (feature == EnumFeatures.Exempt) ? true : AccessControl.IsActionAccessible(HttpContext.Current.User, feature, EnumActions.Add); allowDelete &= (feature == EnumFeatures.Exempt) ? true : AccessControl.IsActionAccessible(HttpContext.Current.User, feature, EnumActions.Delete); GridBuilder <T> retVal = gridbuilder.Name(gridName).DataKeys(k => k.Add("ID")) .Scrollable(sc => sc.Height("*")) .Resizable(rs => rs.Columns(true)) .EnableCustomBinding(true) .Pageable(paging => paging.PageSize(ConfigurationHelper.GetsmARTDetailGridPageSize()) .Style(Telerik.Web.Mvc.UI.GridPagerStyles.NextPreviousAndNumeric) .Total(100)) .DataBinding(bindings => bindings.Ajax() .Select(action, ajaxController, routedValue) .Insert("_Insert", ajaxController, new { isNew = (indexID.Equals("0") ? true : false) }) .Update("_Update", ajaxController, new { isNew = (indexID.Equals("0") ? true : false) }) .Delete("_Delete", ajaxController, new { MasterID = indexID, isNew = (indexID.Equals("0") ? true : false) }) ); if (isEditable && isVisible) { if (allowInsert) { retVal.Editable(editing => editing.Enabled(isEditable).Mode(Telerik.Web.Mvc.UI.GridEditMode.PopUp).Window(w => w.Modal(true))).ToolBar(commands => commands.Insert()); } retVal.Editable(editing => editing.Enabled(isEditable).Mode(Telerik.Web.Mvc.UI.GridEditMode.PopUp).Window(w => w.Modal(true))); } else { retVal.Editable(editing => editing.Enabled(false)); } Type typeT = typeof(T); PropertyInfo[] properties = typeT.GetProperties(); //// Add keys //GridDataKeyFactory<T> dataKeys = new GridDataKeyFactory<T>(retVal); //foreach (PropertyInfo property in properties) // if (IsAttributePresent<KeyAttribute>(property)) // dataKeys.Add(property.Name); // Add Columns GridColumnFactory <T> columns = new GridColumnFactory <T>(retVal); foreach (PropertyInfo property in properties) { bool IsColumnVisible = !IsAttributePresent <HiddenInputAttribute>(property); if (hiddenColumns != null && hiddenColumns.Contains(property.Name)) { continue; } GridBoundColumnBuilder <T> gridBoundBuilder = columns.Bound(property.Name).Visible(IsColumnVisible); //Set numeric column to right align. if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(double)) { gridBoundBuilder.HtmlAttributes(new { style = "text-align: right;" }).Format("{0:0.00}"); } if (IsAttributePresent <ClientTemplateHtmlAttribute>(property)) { gridBoundBuilder.ClientTemplate(property.TemplateHtmlForProperty()); } } if (isEditable && isVisible) { columns.Command(commands => { if (allowEdit) { commands.Edit().ButtonType(Telerik.Web.Mvc.UI.GridButtonType.Image); } if (allowDelete) { commands.Delete().ButtonType(Telerik.Web.Mvc.UI.GridButtonType.Image); } } ); } return(retVal); }
public static GridBuilder <T> BuildGrid <T>( this GridBuilder <T> gridbuilder, string gridName, string ajaxController, bool useClientTemplateHtml ) where T : class { bool isEditable = true; bool isVisible = true; GridBuilder <T> retVal = gridbuilder.Name(gridName).DataKeys(k => k.Add("ID")) .Scrollable(sc => sc.Height("*")) .Resizable(rs => rs.Columns(true)) .EnableCustomBinding(true) .Pageable(paging => paging.PageSize(ConfigurationHelper.GetsmARTDetailGridPageSize()) .Style(Telerik.Web.Mvc.UI.GridPagerStyles.NextPreviousAndNumeric) .Total(100)) .DataBinding(bindings => bindings.Ajax() .Select("_Index", ajaxController) .Insert("_Insert", ajaxController) .Update("_Update", ajaxController) .Delete("_Delete", ajaxController)); if (isEditable && isVisible) { retVal.Editable(editing => editing.Enabled(isEditable).Mode(Telerik.Web.Mvc.UI.GridEditMode.PopUp).Window(w => w.Modal(true))).ToolBar(commands => commands.Insert()); } else { retVal.Editable(editing => editing.Enabled(false)); } Type typeT = typeof(T); PropertyInfo[] properties = typeT.GetProperties(); //// Add keys //GridDataKeyFactory<T> dataKeys = new GridDataKeyFactory<T>(retVal); //foreach (PropertyInfo property in properties) // if (IsAttributePresent<KeyAttribute>(property)) // dataKeys.Add(property.Name); // Add Columns if (!useClientTemplateHtml) { GridColumnFactory <T> columns = new GridColumnFactory <T>(retVal); foreach (PropertyInfo property in properties) { bool IsColumnVisible = !IsAttributePresent <HiddenInputAttribute>(property); GridBoundColumnBuilder <T> gridBoundBuilder = columns.Bound(property.Name).Visible(IsColumnVisible); //Set numeric column to right align. if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(double)) { gridBoundBuilder.HtmlAttributes(new { style = "text-align: right;" }).Format("{0:0.00}"); } if (IsAttributePresent <ClientTemplateHtmlAttribute>(property)) { gridBoundBuilder.ClientTemplate(property.TemplateHtmlForProperty()); } } if (isEditable && isVisible) { columns.Command(commands => { commands.Edit().ButtonType(Telerik.Web.Mvc.UI.GridButtonType.Image); commands.Delete().ButtonType(Telerik.Web.Mvc.UI.GridButtonType.Image); } ); } } else { Type type = typeof(T); retVal.ClientRowTemplate(type.TemplateHtml()).Columns(columns => columns.AutoGenerate(false)); } return(retVal); }
public static GridActionColumnBuilder DeleteCommand <T>(this GridColumnFactory <T> factory) where T : class { return(factory.Command(commands => commands.EmsgDelete()).AsEmsgCommand()); }