Пример #1
0
        public static DataGrodColumnTagHelper Create(string html)
        {
            var col = new DataGrodColumnTagHelper();

            var th = new HtmlParser().ParseDocument($"<html> <head> </head> <body> <table> <thead>{html} </thead> </table> </body> </html>").QuerySelector("th");

            var option = th.GetAttribute(EasyuiConsts.Option);

            if (option.IsNotNullOrWhiteSpace())
            {
                foreach (var property in JObject.Parse(option).Properties())
                {
                    col.Options.AddOrUpdate(property.Name, property.Value);
                }
            }

            //col.Field = col.Options.GetOrDefault(EasyuiConsts.Field)?.ToString();
            //col.Styler = col.Options.GetOrDefault(EasyuiConsts.Grid_Col_Styler)?.ToString();
            //col.Editor = col.Options.GetOrDefault(EasyuiConsts.Grid_Col_Editor)?.ToString();
            //col.Formatter = col.Options.GetOrDefault(EasyuiConsts.Grid_Col_Formatter)?.ToString();
            //col.Width = col.Options.GetOrDefault(EasyuiConsts.Item_Replace).To(0);
            //col.Sort = col.Options.GetOrDefault(EasyuiConsts.Item_Sort).ToNullable<int>();

            col.ReplaceField = th.GetAttribute(EasyuiConsts.Item_Replace);
            col.IsFrozen     = th.GetAttribute(EasyuiConsts.Grid_Col_IsFrozen).To(false);
            col.IsEdit       = th.GetAttribute(EasyuiConsts.Grid_Col_IsEdit).To(false);

            var align = col.Options.GetOrDefault(EasyuiConsts.Grid_Col_Align)?.ToString();

            if (align.IsNotNullOrWhiteSpace())
            {
                switch (align)
                {
                case "center":
                    col.Align = Align.Center;
                    break;

                case "right":
                    col.Align = Align.Right;
                    break;

                default:
                    col.Align = Align.Left;
                    break;
                }
            }

            return(col);
        }
Пример #2
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var table = new StringBuilder();

            DefaultWidth = DefaultWidth ?? 200;
            if (ModelTagHelper.HasModel)
            {
                foreach (var property in ModelTagHelper.PropertyInfos)
                {
                    var isId = property.Name == WebConsts.Id;
                    if (property.IsDefined(typeof(ColHideAttribute)))
                    {
                        continue;
                    }
                    if (property.IsDefined(typeof(ComboboxAttribute)))
                    {
                        continue;
                    }
                    if (isId && IdName.IsNullOrWhiteSpace())
                    {
                        continue;
                    }


                    if (property.PropertyType.IsGenericType && property.PropertyType.IsAssignableFromGenericTypeInterface(typeof(IEnumerable <>)))
                    {
                        var genericTypes = property.PropertyType.GetGenericArguments();
                        if (genericTypes.Length > 1 || !genericTypes[0].IsValueType())
                        {
                            continue;
                        }
                    }

                    var col = new DataGrodColumnTagHelper();


                    col.IsFrozen = property.IsDefined(typeof(FixedColAttribute));

                    var propertyType = property.PropertyType;
                    var valueType    = propertyType.GetValueType();
                    var isEnum       = valueType.IsEnum;
                    var name         = property.GetDisplayName();
                    var isBool       = valueType == typeof(bool);
                    if (name == property.Name)
                    {
                        switch (name)
                        {
                        case nameof(IHasRemark.Remark):
                            name = WebConsts.DisplayName_Remark;
                            break;

                        case nameof(IHasName.Name):
                            name = WebConsts.DisplayName_Name;
                            break;

                        case nameof(WebConsts.IsActive):
                            name = WebConsts.IsAble_Name;
                            break;

                        case nameof(IHasSort.Sort):
                            name = WebConsts.Sort_Name;
                            break;

                        case WebConsts.Id:
                            name = IdName;
                            break;

                        default:
                            if (isEnum)
                            {
                                name = valueType.GetDisplayName();
                            }
                            if (name == property.PropertyType.Name)
                            {
                                name = valueType.GetDescription();
                            }
                            break;
                        }
                    }

                    if (isEnum || isBool)
                    {
                        string data;
                        if (isBool)
                        {
                            data = EnumUtil.BoolDictionary.Select(x => new { value = x.Key, text = x.Value })
                                   .ToJsonString(true);
                        }
                        else
                        {
                            data = EnumUtil.GetEnumValueList(valueType)
                                   .Select(x => new { value = x.Key, text = x.Value.Replace("\"", "&quot;") })
                                   .ToJsonString(true);
                        }


                        if (col.Formatter.IsNullOrWhiteSpace())
                        {
                            col.Formatter = $"function(value, rowData, rowIndex) {{ return getArrayText({data}, value, 'text', 'value'); }}";
                        }

                        if (col.Editor.IsNullOrWhiteSpace())
                        {
                            col.Editor = $"{{ type: 'combobox', options: {{ valueField: 'value', textField: 'text', data: {data}, required: {propertyType.IsValueType().ToString().ToLower()} }} }}";
                        }
                    }
                    else
                    {
                        if (propertyType.IsNumberType())
                        {
                            col.Editor = "numberbox";
                        }
                        else if (valueType == typeof(DateTime))
                        {
                            col.Editor = "datebox";
                        }
                        else
                        {
                            col.Editor = "text";
                        }

                        col.Editor = $"'{col.Editor}'";
                    }

                    col.Field    = property.Name;
                    col.Width    = DefaultWidth.Value;
                    col.Sortable = IsSort;
                    col.Title    = name;

                    _cols.Add(col);
                    col.Sort = isId ? 0 : ColCount;

                    var itemAttr = property.GetCustomAttribute <GridItemAttribute>();
                    if (itemAttr != null)
                    {
                        if (itemAttr.Sort.HasValue)
                        {
                            col.Sort = itemAttr.Sort;
                        }

                        if (itemAttr.Editor.IsNotNullOrWhiteSpace())
                        {
                            col.Editor = itemAttr.Editor;
                        }

                        if (itemAttr.Formatter.IsNotNullOrWhiteSpace())
                        {
                            col.Formatter = itemAttr.Formatter;
                        }

                        if (itemAttr.Width.IsNotNullOrWhiteSpace())
                        {
                            col.Width = itemAttr.Width;
                        }
                    }
                }
            }
            var childHtml = (await output.GetChildContentAsync()).GetContent();

            if (childHtml.IsNotNullOrWhiteSpace())
            {
                var split = "</th>";

                var childs = HtmlTrim(childHtml).Split(split, StringSplitOptions.RemoveEmptyEntries);

                foreach (var child in childs)
                {
                    var itemTag = DataGrodColumnTagHelper.Create(child + split);
                    if (itemTag.ReplaceField.IsNotNullOrWhiteSpace())
                    {
                        var replaceTag = _cols.FirstOrDefault(x => x.Field.Equals(itemTag.ReplaceField, StringComparison.CurrentCultureIgnoreCase));
                        if (replaceTag != null)
                        {
                            itemTag.Field = replaceTag.Field;
                            if (itemTag.Title.IsNullOrWhiteSpace())
                            {
                                itemTag.Title = replaceTag.Title;
                            }
                            if (!itemTag.Sort.HasValue)
                            {
                                itemTag.Sort = replaceTag.Sort;
                            }
                            if (itemTag.Editor.IsNullOrWhiteSpace())
                            {
                                itemTag.Editor = replaceTag.Editor;
                            }
                            if (itemTag.Formatter.IsNullOrWhiteSpace())
                            {
                                itemTag.Formatter = replaceTag.Formatter;
                            }
                            if (itemTag.Styler.IsNullOrWhiteSpace())
                            {
                                itemTag.Styler = replaceTag.Styler;
                            }
                            if (itemTag.Width == (object)0)
                            {
                                itemTag.Width = replaceTag.Width;
                            }

                            _cols.Remove(replaceTag);
                        }
                    }

                    _cols.Add(itemTag);
                }
            }

            if (!FitColumns.HasValue)
            {
                if (ColCount < FitColCount)
                {
                    FitColumns = true;
                }
                else
                {
                    FitColumns = FitColCount > ColCount;
                }
            }

            Options.Add(EasyuiConsts.FitColumns, FitColumns);

            var checkBox = "<th data-options=\"field:'_ck',checkbox: true\"></th>";

            var frozenCols = _cols.Where(x => x.IsFrozen).ToList();

            async Task AddHead(List <DataGrodColumnTagHelper> cols, bool isFrozen = false)
            {
                table.Append($"<thead {(isFrozen ? "data-options='frozen: true'" : "")}>");
                if (HasCheckBox && (isFrozen || (!isFrozen && !frozenCols.Any())))
                {
                    table.Append(checkBox);
                }

                foreach (var col in cols.OrderBy(x => x.Sort))
                {
                    if (!col.IsEdit)
                    {
                        col.Editor = null;
                    }
                    table.Append(await RenderInnerTagHelper(col, context, CreateTagHelperOutput()));
                }

                table.Append("</thead>");
            }

            if (frozenCols.Any())
            {
                await AddHead(frozenCols, true);
            }

            await AddHead(_cols.Where(x => !x.IsFrozen).ToList());

            output.Content.SetHtmlContent(table.ToString());

            await base.ProcessAsync(context, output);
        }