예제 #1
0
        /// <summary>
        /// Process the data table building.
        /// </summary>
        /// <param name="context">Contains information associated with the current HTML tag.</param>
        /// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
        /// <returns>A <see cref="Task"/> that on completion updates the output.</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            await base.ProcessAsync(context, output);

            output.Attributes.SetAttribute("id", context.UniqueId);

            if (Data == null && DataType == null)
            {
                throw new InvalidOperationException("No data specified.");
            }
            if (Data != null)
            {
                DataType = Data.GetType().GetInterface("IEnumerable`1")?.GetGenericArguments()[0]
                           ?? throw new NotSupportedException();
            }

            var viewModel = await FactoryCache.GetOrCreateAsync(DataType,
                                                                entry => DataRowFunctions.Factory((Type)entry.Key));

            output.TagName = "table";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.AddClass("data-table table table-sm table-striped");
            output.PreElement.AppendHtml("<div class=\"table-wrapper\">");
            output.PostElement.AppendHtml("</div>");
            PrintScript(context.UniqueId, viewModel, output.PostElement);
            if (AutoWidth)
            {
                output.Attributes.Add("style", "width:auto");
            }

            var thead = new TagBuilder("thead").WithBody(viewModel.THead);

            if (TableHeaderClass != null)
            {
                thead.AddCssClass(TableHeaderClass);
            }
            output.Content.AppendHtml("\r\n").AppendHtml(thead);

            if (Data != null)
            {
                var tbody = new TagBuilder("tbody");

                foreach (var item in Data)
                {
                    if (item == null)
                    {
                        tbody.InnerHtml.SetHtmlContent("<tr><td>NULL error</td></tr>");
                        break;
                    }
                    else
                    {
                        var tr = viewModel.TRow(item);
                        tr.MergeAttribute("role", "row");
                        tbody.InnerHtml.AppendHtml("\r\n").AppendHtml(tr);
                    }
                }

                output.Content.AppendHtml(tbody);
            }
        }
예제 #2
0
        /// <summary>
        /// Appends a <c>&lt;table&gt;</c> element.
        /// </summary>
        /// <typeparam name="TElement">The element types.</typeparam>
        /// <param name="elements">The inner elements.</param>
        /// <param name="tableClass">The attached class for table.</param>
        /// <param name="theadClass">The attached class for thead.</param>
        /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
        protected IHtmlContentBuilder AppendDataTable <TElement>(
            IReadOnlyList <TElement> elements,
            string?tableClass = null,
            string?theadClass = null)
            where TElement : class
        {
            // Justification: the factory is implemented with Task.FromResult.
            var viewModel = DataTableFactoryCache.GetOrCreate(typeof(TElement),
                                                              entry => DataRowFunctions.Factory((Type)entry.Key)).Result;

            var uniqueId = Guid.NewGuid().ToString()[0..6];