Ejemplo n.º 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(LayuiConsts.Option);

            if (option.IsNotNullOrWhiteSpace())
            {
                foreach (var x in option.Split(','))
                {
                    var items = x.Split(':');
                    col.Options.AddOrUpdate(items[0], items[1].Replace("'", ""));
                }
            }

            col.Field        = col.Options.GetOrDefault(LayuiConsts.Field)?.ToString();
            col.Style        = col.Options.GetOrDefault(LayuiConsts.Grid_Col_Style)?.ToString();
            col.Formatter    = col.Options.GetOrDefault(LayuiConsts.Grid_Col_Formatter)?.ToString();
            col.Width        = col.Options.GetOrDefault(LayuiConsts.Item_Replace)?.ToString();
            col.Sort         = col.Options.GetOrDefault(LayuiConsts.Item_Sort).ToNullable <int>();
            col.Edit         = col.Options.GetOrDefault(LayuiConsts.Grid_Col_Editor).ToNullable <Edit>();
            col.ReplaceField = th.GetAttribute(LayuiConsts.Item_Replace);
            col.Align        = col.Options.GetOrDefault(LayuiConsts.Grid_Col_Align).To <Align>();


            return(col);
        }
Ejemplo n.º 2
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (ModelTagHelper.HasModel)
            {
                var table = new StringBuilder();

                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;
                    }

                    var col = new DataGrodColumnTagHelper();


                    var fixedAttr = property.GetCustomAttribute <FixedColAttribute>();
                    if (fixedAttr?.Fixed != null)
                    {
                        col.Fixed = fixedAttr.Fixed.To <Fixed>();
                    }


                    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();
                            }
                            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.Edit = "numberbox";
                        }
                        else if (valueType == typeof(DateTime))
                        {
                            //col.Edit = "datebox";
                        }
                        else
                        {
                            col.Edit = Edit.Text;
                        }
                    }

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

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

                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;
                                itemTag.Width     = replaceTag.Width;
                                itemTag.Style     = replaceTag.Style;
                                itemTag.Title     = replaceTag.Title;
                                itemTag.Formatter = replaceTag.Formatter;
                                if (!itemTag.Sort.HasValue)
                                {
                                    itemTag.Sort = replaceTag.Sort;
                                }
                                if (itemTag.Edit.HasValue)
                                {
                                    itemTag.Edit = replaceTag.Edit;
                                }
                                _cols.Remove(replaceTag);
                            }
                        }

                        _cols.Add(itemTag);
                    }
                }


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

                Options.Add(LayuiConsts.FitColumns, FitColumns);

                async Task AddHead(List <DataGrodColumnTagHelper> cols)
                {
                    table.Append("<thead>");
                    if (HasCheckBox)
                    {
                        table.Append($"<th {LayuiConsts.Option}=\"{{ type: 'checkbox' , fixed: 'left'}}\"></th>");
                    }
                    if (ShowRowNumber)
                    {
                        table.Append($"<th {LayuiConsts.Option}=\"{{ type: 'numbers' , fixed: 'left'}} \"></th>");
                    }


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

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

                await AddHead(_cols.ToList());

                output.Content.SetHtmlContent(table.ToString());
            }
            await base.ProcessAsync(context, output);
        }