private void ShowDatatable(DataColumn dataColumn, DataColumnType type, DataColumnModel data, bool readOnly = true)
 {
     dataColumn.gameObject.SetActive(true);
     dataColumn.SetClass(type);
     dataColumn.Show(data, true);
     dataColumn.ReadOnly  = readOnly;
     dataColumn.Changable = false;
     dataColumn.Deletable = false;
 }
Пример #2
0
        public static GridBuilder <TEntity> EntityEditor <TContext, TEntity>(this HtmlHelper html, string name
                                                                             , object defaultFieldValues = null, params DataEditorMemberMap[] memberFormMaps)
            where TEntity : class
            where TContext : CachableDbContext <TContext>, new()
        {
            var entityName          = typeof(TEntity).Name;
            GridBuilder <TEntity> g = html.Kendo().Grid <TEntity>().Name(name);
            var props = typeof(TEntity).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            Dictionary <PropertyInfo, Attribute[]> attrs = props.ToDictionary(p => p, p => p.GetCustomAttributes().ToArray());
            var           keyProp           = attrs.FirstOrDefault(pa => pa.Value.Any(a => a.GetType() == typeof(KeyAttribute)));
            List <string> dontRenderColumns = new List <string>();
            List <Tuple <PropertyInfo, PropertyInfo> > foreignKeys = GetForeignKeys(attrs);

            Dictionary <string, object> fieldValues = new Dictionary <string, object>();

            if (defaultFieldValues != null)
            {
                foreach (var p in defaultFieldValues.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    fieldValues[p.Name] = p.GetValue(defaultFieldValues);
                }
            }
            List <PropertyInfo>    usedFields   = new List <PropertyInfo>();
            List <DataColumnModel> columnsModel = new List <KendoHelpers.DataEditorHelpers.DataColumnModel>();

            g.Columns(cols =>
            {
                foreach (var p in attrs)
                {
                    var cm   = new DataColumnModel();
                    cm.field = p.Key.Name;
                    if (p.Key == keyProp.Key)
                    {
                        continue;
                    }
                    if (fieldValues.ContainsKey(p.Key.Name))
                    {
                        continue;
                    }
                    //if (p.Value.Any(a => a is IgonreColumnAttribute))
                    //    continue;
                    if (memberFormMaps.Any(m => m.MemberName == p.Key.Name))
                    {
                        continue;
                    }
                    if (foreignKeys.Any(f => f.Item2 == p.Key))
                    {
                        continue;
                    }
                    var columnAttribute = p.Value.OfType <DataColumnAttribute>().FirstOrDefault();
                    if (columnAttribute != null && !columnAttribute.Display)
                    {
                        continue;
                    }

                    columnsModel.Add(cm);
                    usedFields.Add(p.Key);
                    var fk = foreignKeys.SingleOrDefault(f => f.Item1 == p.Key);

                    if (fk != null)
                    {
                        cm.type = "combo";
                        CreateForeignKeyColumn <TContext, TEntity>(cols, p, fk);
                        continue;
                    }
                    if (CheckAndAddComboBoxColumn <TContext, TEntity>(cols, p))
                    {
                        cm.type = "combo";
                        continue;
                    }
                    if (p.Key.PropertyType.IsEnum)
                    {
                        cm.type = "combo";
                        CreateEnumColumn <TContext, TEntity>(cols, p);
                        continue;
                    }
                    //cols.Template()
                    var col = cols.Bound(p.Key.Name);
                    if (p.Value.OfType <ImageColumnAttribute>().Any())
                    {
                        cm.type = "image";
                        col.ClientTemplate("<div class='imgCol'></div>");
                        continue;
                    }
                    cm.type = DataColumnModel.GetType(p.Key.PropertyType);
                }
                cols.Command(c => c.Localize());
            }).Editable(c => c.Mode(GridEditMode.InLine))
            .DataSource(ds =>
            {
                var ds2 = ds.Ajax().Batch(false).Create(c => c.Action("Create", entityName).AddCRUDOperationData(fieldValues, memberFormMaps))
                          .Destroy("Delete", entityName)
                          .Read(r => r.Action("Get", entityName).AddCRUDOperationData(fieldValues, memberFormMaps))
                          .Update(r => r.Action("Update", entityName).AddCRUDOperationData(fieldValues, memberFormMaps))
                          //ds.Events(ev => ev.Error("onError"));
                          .Model(m =>
                {
                    m.Id(keyProp.Key.Name);
                    foreach (var f in usedFields)
                    {
                        m.Field(f.Name, f.PropertyType);
                    }
                    foreach (var p in props)
                    {
                        if (!usedFields.Any(f => f.Name == p.Name))
                        {
                            m.Field(p.Name, p.PropertyType).Editable(false);
                        }
                    }
                })
                          .Events(ev => ev.Error("Unicorn.kendoErrorHandler"));
            })
            .ToolBar(t => t.AddCreateLocalize())
            .Sortable().DoConfig()
            .Events(ev => ev.Edit("DataEditor_GridEdit"));
            g.TableHtmlAttributes(new Dictionary <string, object> {
                { "data-model", Newtonsoft.Json.JsonConvert.SerializeObject(columnsModel) }
            });
            return(g);
        }