コード例 #1
0
        public static IHtmlContent CardView <TModel>(this IHtmlHelper html, IEnumerable <TModel> dataList, Func <TModel, IHtmlContent> operatingBuilder = default)
        {
            var type       = typeof(TModel);
            var columnMeta = DataListHelper.GetDataColumnMeta(type);
            var groupMeta  = DataListHelper.GetDataMeta(type);
            var group      = new TagBuilder("div");

            group.AddCssClass("card-deck");
            if (groupMeta.HtmlAttribute != null)
            {
                group.MergeAttributes(groupMeta.HtmlAttribute, true);
            }

            if (dataList != null)
            {
                foreach (var item in dataList)
                {
                    var card = new TagBuilder("div");
                    card.AddCssClass("card");
                    TagBuilder          header    = null;
                    TagBuilder          body      = null;
                    TagBuilder          listGroup = null;
                    TagBuilder          footer    = null;
                    IHtmlContentBuilder root      = null;
                    foreach (var column in columnMeta.FindAll(p => p.Attribute != null))
                    {
                        switch (column.CardContentContainer)
                        {
                        case CardContentContainer.Root:
                            root ??= new HtmlContentBuilder();
                            root.AppendHtml(column.GetValue(type, item));
                            break;

                        case CardContentContainer.Header:
                            header ??= new TagBuilder("div");
                            var headerItem = new TagBuilder("div");
                            headerItem.AddCssClass("card-text");
                            if (column.ContentHtmlAttribute != null)
                            {
                                headerItem.MergeAttributes(column.ContentHtmlAttribute, true);
                            }
                            headerItem.InnerHtml.AppendHtml(column.GetValue(type, item));
                            header.InnerHtml.AppendHtml(headerItem);
                            break;

                        case CardContentContainer.Body:
                            body ??= new TagBuilder("div");
                            var bodyItem = new TagBuilder("div");
                            bodyItem.AddCssClass("card-text");
                            if (column.ContentHtmlAttribute != null)
                            {
                                bodyItem.MergeAttributes(column.ContentHtmlAttribute, true);
                            }
                            bodyItem.InnerHtml.AppendHtml(column.GetValue(type, item));
                            body.InnerHtml.AppendHtml(bodyItem);
                            break;

                        case CardContentContainer.ListGroup:
                            listGroup ??= new TagBuilder("ul");
                            var listItem = new TagBuilder("li");
                            listItem.AddCssClass("list-group-item");
                            if (column.ContentHtmlAttribute != null)
                            {
                                listItem.MergeAttributes(column.ContentHtmlAttribute, true);
                            }
                            listItem.InnerHtml.AppendHtml(column.GetValue(type, item));
                            listGroup.InnerHtml.AppendHtml(listItem);
                            break;

                        case CardContentContainer.Footer:
                            footer ??= new TagBuilder("div");
                            var footerItem = new TagBuilder("div");
                            footerItem.AddCssClass("text-muted");
                            if (column.ContentHtmlAttribute != null)
                            {
                                footerItem.MergeAttributes(column.ContentHtmlAttribute, true);
                            }
                            footerItem.InnerHtml.AppendHtml(column.GetValue(type, item));
                            footer.InnerHtml.AppendHtml(footerItem);
                            break;
                        }
                    }
                    if (header != null)
                    {
                        header.AddCssClass("card-header");
                        card.InnerHtml.AppendHtml(header);
                    }
                    if (root != null)
                    {
                        card.InnerHtml.AppendHtml(root);
                    }

                    if (body != null)
                    {
                        body.AddCssClass("card-body");
                        card.InnerHtml.AppendHtml(body);
                    }

                    if (listGroup != null)
                    {
                        listGroup.AddCssClass("list-group list-group-flush");
                        card.InnerHtml.AppendHtml(listGroup);
                    }

                    if (operatingBuilder != null)
                    {
                        card.InnerHtml.AppendHtml(operatingBuilder(item));
                    }

                    if (footer != null)
                    {
                        footer.AddCssClass("card-footer");
                        card.InnerHtml.AppendHtml(footer);
                    }

                    group.InnerHtml.AppendHtml(card);
                }
            }
            return(group);
        }
コード例 #2
0
        /// <summary>
        /// 生成table
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="html"></param>
        /// <param name="dataList">数据集合</param>
        /// <param name="operatingBuilder">在table最后一列添加html</param>
        /// <returns></returns>
        public static IHtmlContent TableView <TModel>(this IHtmlHelper html, IEnumerable <TModel> dataList, Func <TModel, IHtmlContent> operatingBuilder = default)
        {
            var type           = typeof(TModel);
            var columnMeta     = DataListHelper.GetDataColumnMeta(type);
            var tableMeta      = DataListHelper.GetDataMeta(type);
            var tableContainer = new TagBuilder("div");

            tableContainer.AddCssClass("table-responsive-md");
            var table = new TagBuilder("table");

            table.MergeAttribute("class", "table table-hover");
            if (tableMeta.HtmlAttribute != null)
            {
                table.MergeAttributes(tableMeta.HtmlAttribute, true);
            }
            var header = new TagBuilder("tr");

            foreach (var column in columnMeta.FindAll(p => p.Attribute != null))
            {
                var th = new TagBuilder("th");
                th.MergeAttribute("scope", "col");
                if (column.HeaderHtmlAttribute != null)
                {
                    th.MergeAttributes(column.HeaderHtmlAttribute, true);
                }
                th.InnerHtml.AppendHtml(column.DisplayName);
                header.InnerHtml.AppendHtml(th);
            }
            if (operatingBuilder != null)
            {
                var action = new TagBuilder("th");
                header.InnerHtml.AppendHtml(action);
            }
            table.InnerHtml.AppendHtml(header);
            if (dataList != null)
            {
                foreach (var item in dataList)
                {
                    var tr = new TagBuilder("tr");
                    foreach (var column in columnMeta.FindAll(p => p.Attribute != null))
                    {
                        var td = new TagBuilder("td");
                        if (column.ContentHtmlAttribute != null)
                        {
                            td.MergeAttributes(column.ContentHtmlAttribute, true);
                        }
                        td.InnerHtml.AppendHtml(column.GetValue(type, item));
                        tr.InnerHtml.AppendHtml(td);
                    }
                    if (operatingBuilder != null)
                    {
                        var td = new TagBuilder("td");
                        td.InnerHtml.AppendHtml(operatingBuilder(item));
                        tr.InnerHtml.AppendHtml(td);
                    }
                    table.InnerHtml.AppendHtml(tr);
                }
            }
            tableContainer.InnerHtml.AppendHtml(table);
            return(tableContainer);
        }