Beispiel #1
0
        //TODO: Implement Display(Order) built in attribute
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            // setup tag
            output.TagName = "table";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.Add("class", styles.Table);

            // check that we have a list
            if (!For.Metadata.IsCollectionType)
            {
                return;
            }

            // setup properties to hide
            var hideProperties = HideProperties?.Split(',').Select(p => p.Trim()).ToList();

            // get the key property
            string keyProperty = GetKeyProperty();

            // create the headers
            CreateHeaders(output, hideProperties);

            // can we conextualise the htmlhelper - if not do not render html values
            if (htmlHelper is IViewContextAware ht)
            {
                ht.Contextualize(ViewContext);
            }
            else
            {
                RenderValueHtml = false;
            }

            // now loop through items
            var collection = (ICollection)For.Model;

            foreach (var item in collection)
            {
                // create the row
                var row = new TagBuilder("tr")
                {
                    TagRenderMode = TagRenderMode.Normal
                };

                // get the explorer
                var explorer = For.ModelExplorer.GetExplorerForModel(item);

                // get the key value
                string keyValue = Utility.GetKeyValue(keyProperty, explorer);

                // loop through the element properties
                foreach (var p in explorer.Properties)
                {
                    if (hideProperties != null && hideProperties.Contains(p.Metadata.PropertyName))
                    {
                        continue;
                    }

                    if (!Utility.DisplayForView(p.Metadata))
                    {
                        continue;
                    }

                    // create a model expression from the explorer
                    //var f = new ModelExpression($"{p.Container.Metadata.Name }.{ p.Metadata.Name}", explorer);

                    var value = Utility.GetFormattedHtml(p, ViewContext, htmlHelper, RenderValueHtml);

                    // check for complex object and set value
                    value = Utility.GetComplexValue(p, value, ViewContext, htmlHelper, RenderValueHtml);

                    // render the cell
                    var cell = new TagBuilder("td");
                    //TODO: Uncomment in order to hide this on mobile when this works (FormIndex)
                    //cell.Attributes.Add("class", styles.TableCellHideMobile);
                    cell.InnerHtml.AppendHtml(value);
                    row.InnerHtml.AppendHtml(cell);
                }

                // render the buttons cell
                if (keyValue != null)
                {
                    var buttons = new TagBuilder("td");
                    var routes  = new Dictionary <string, string>()
                    {
                        { keyProperty.ToLower(), keyValue }
                    };
                    if (!string.IsNullOrEmpty(EditPage))
                    {
                        buttons.InnerHtml.AppendHtml(GenerateEditButton(explorer, keyProperty, keyValue));
                    }
                    if (!string.IsNullOrEmpty(ViewPage))
                    {
                        buttons.InnerHtml.AppendHtml(GenerateViewButton(explorer, keyProperty, keyValue));
                    }
                    if (!string.IsNullOrEmpty(DeletePage))
                    {
                        buttons.InnerHtml.AppendHtml(GenerateDeleteButton(explorer, keyProperty, keyValue));
                    }
                    row.InnerHtml.AppendHtml(buttons);
                }
                output.Content.AppendHtml(row);
            }
        }
Beispiel #2
0
 protected virtual string GetKeyProperty()
 {
     return(Utility.GetKeyProperty(For.Metadata.ElementMetadata.Properties));
 }