Example #1
0
        private IHtmlContent RenderSwitch(string name, ModelExplorer modelExplorer, ModelMetadata metadata, bool inline, object htmlAttributes)
        {
            var tag = new TagBuilder("div");

            tag.AddCssClass(inline ? "layui-inline" : "layui-form-item");

            var displayName = metadata.GetDisplayName();
            var placeholder = metadata.Placeholder;

            var input = new TagBuilder("input");

            input.GenerateId(name, "");
            input.TagRenderMode = TagRenderMode.SelfClosing;
            input.MergeAttribute("type", InputType.CheckBox.ToString());
            input.MergeAttribute("name", name, replaceExisting: true);
            input.MergeAttribute("value", "true");
            input.MergeAttribute("lay-skin", "switch");

            if (!string.IsNullOrEmpty(placeholder))
            {
                input.MergeAttribute("lay-text", placeholder);
            }

            MergeHtmlAttributes(input, htmlAttributes);

            if (modelExplorer.Model != null)
            {
                if (bool.TryParse(modelExplorer.Model.ToString(), out bool modelChecked))
                {
                    if (modelChecked)
                    {
                        input.MergeAttribute("checked", "checked");
                    }
                }
            }

            tag.InnerHtml.AppendHtml($"<label class=\"layui-form-label\">{displayName}</label>");
            tag.InnerHtml.AppendHtml("<div class=\"layui-input-inline\">");
            tag.InnerHtml.AppendHtml(input);
            tag.InnerHtml.AppendHtml("</div>");
            if (!string.IsNullOrEmpty(metadata.Description))
            {
                tag.InnerHtml.AppendHtml($"<div class=\"layui-form-mid layui-word-aux\">{metadata.Description}</div>");
            }
            return(tag);
        }
Example #2
0
        private IHtmlContent RenderDateTime(string name, ModelExplorer modelExplorer, bool inline, object htmlAttributes)
        {
            ModelMetadata metadata = modelExplorer.Metadata;
            var           tag      = new TagBuilder("div");

            tag.AddCssClass(inline ? "layui-inline" : "layui-form-item");

            var displayName = metadata.GetDisplayName();
            var placeholder = metadata.Placeholder;

            var input = new TagBuilder("input");

            input.AddCssClass("layui-input");
            input.AddCssClass("laydate");
            input.GenerateId(name, "");
            input.TagRenderMode = TagRenderMode.SelfClosing;
            input.MergeAttribute("type", InputType.Text.ToString());
            input.MergeAttribute("name", name, replaceExisting: true);

            Type type         = GetRealType(modelExplorer.ModelType);
            var  dataTypeName = metadata.DataTypeName;

            if (string.IsNullOrEmpty(dataTypeName))
            {
                dataTypeName = "datetime";
            }
            dataTypeName = dataTypeName.ToLower();
            var defaultFormatStr = "yyyy-MM-dd HH:mm:ss";

            switch (dataTypeName)
            {
            case "datetime":
                defaultFormatStr = "yyyy-MM-dd HH:mm:ss";
                break;

            case "date":
                defaultFormatStr = "yyyy-MM-dd";
                break;

            case "year":
                defaultFormatStr = "yyyy";
                break;

            case "month":
                defaultFormatStr = "yyyy-MM";
                break;

            case "time":
                defaultFormatStr = "HH:mm:ss";
                break;
            }

            var formatString = metadata.DisplayFormatString ?? defaultFormatStr;

            if (!string.IsNullOrEmpty(placeholder))
            {
                input.MergeAttribute("lay-text", placeholder);
            }
            input.MergeAttribute("data-format", formatString);
            if (!string.IsNullOrEmpty(metadata.DataTypeName))
            {
                input.MergeAttribute("data-type", dataTypeName);
            }

            if (modelExplorer.Model != null)
            {
                if (type == typeof(DateTime))
                {
                    input.MergeAttribute("value", ((DateTime)modelExplorer.Model).ToString(formatString));
                }
                else
                {
                    input.MergeAttribute("value", modelExplorer.Model.ToString());
                }
            }

            MergeHtmlAttributes(input, htmlAttributes);

            tag.InnerHtml.AppendHtml($"<label class=\"layui-form-label\">{displayName}</label>");
            tag.InnerHtml.AppendHtml("<div class=\"layui-input-inline\">");
            tag.InnerHtml.AppendHtml(input);
            tag.InnerHtml.AppendHtml("</div>");

            RenderDescription(tag, metadata);

            return(tag);
        }
Example #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="tagBuilder"></param>
 /// <param name="id"></param>
 /// <returns></returns>
 public static TagBuilder Id(this TagBuilder tagBuilder, string id)
 {
     tagBuilder.GenerateId(id, "-");
     return(tagBuilder);
 }
Example #4
0
 /// <summary>
 /// 添加标识
 /// </summary>
 /// <param name="id">标识</param>
 public TagBuilder Id(string id)
 {
     _tagBuilder.GenerateId(id, string.Empty);
     return(this);
 }
        /// <summary>
        /// Generates the html for a grid of checkboxes
        /// </summary>
        /// <typeparam name="TModel">The type of the model</typeparam>
        /// <typeparam name="T">The type enumerations. the of simple value types is recommended</typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression">The lambda expression resunting in an enumeration of Ts</param>
        /// <param name="items">The list of available options</param>
        /// <param name="htmlAttributes">A object whose properties will become attibutes of the resulting html container element</param>
        /// <param name="cols">The number of columns to generate. Accepts values between 1 and 12.</param>
        /// <returns></returns>
        public static HtmlString MultiSelect <TModel, T> (
            this IHtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, IEnumerable <T> > > expression,
            IEnumerable <SelectListItem> items,
            object htmlAttributes,
            int cols = 1)
        {
            cols = (cols < 1 || cols > 12) ? 1 : cols;

            ModelExplorer modelExplorer = ExpressionMetadataProvider.FromLambdaExpression(expression, htmlHelper.ViewData, htmlHelper.MetadataProvider);
            StringBuilder strBuilder    = new StringBuilder(modelExplorer.Metadata.Name);

            while (modelExplorer.Container.Metadata.Name != null)
            {
                modelExplorer = modelExplorer.Container;
                strBuilder.Insert(0, ".");
                strBuilder.Insert(0, modelExplorer.Metadata.Name);
            }

            string cboxName = strBuilder.ToString();

            TagBuilder mainContainer = new TagBuilder("div");

            if (htmlAttributes != null)
            {
                foreach (PropertyInfo prop in htmlAttributes.GetType().GetProperties())
                {
                    mainContainer.Attributes.Add(prop.Name, prop.GetValue(htmlAttributes).ToString());
                }
            }

            int        totalRows = items.Count() % cols == 0 ? items.Count() / cols : items.Count() / cols + 1;
            TagBuilder grid      = new TagBuilder("div");

            grid.Attributes.Add("style", $"display: grid; grid-template-columns: {cols}; grid-template-rows: {totalRows}");

            int index = 0;

            foreach (SelectListItem item in items)
            {
                int currentCol = index % cols + 1;
                int currentRow = index / cols + 1;

                TagBuilder cell = new TagBuilder("div");
                cell.Attributes.Add("style", $"grid-column-start: {currentCol}; grid-row-start: {currentRow}");

                TagBuilder checkbox = new TagBuilder("input");
                Dictionary <string, string> attributes = new Dictionary <string, string>()
                {
                    { "type", "checkbox" },
                    { "name", cboxName },
                    { "value", item.Value }
                };

                //if(results.Any(r => r.ToString().Equals(item.Value))) {
                //    attributes.Add("checked", "checked");
                //}
                if (item.Selected)
                {
                    attributes.Add("checked", "checked");
                }

                checkbox.GenerateId($"{cboxName}_{item.Value}", "_");
                foreach (string key in attributes.Keys)
                {
                    checkbox.Attributes.Add(key, attributes[key]);
                }

                TagBuilder label = new TagBuilder("label");
                label.Attributes.Add("for", checkbox.Attributes["id"]);
                label.InnerHtml.Append(item.Text);


                cell.InnerHtml.AppendLine(checkbox.RenderSelfClosingTag());
                cell.InnerHtml.AppendHtml(label.RenderStartTag());
                cell.InnerHtml.AppendHtml(label.RenderBody());
                cell.InnerHtml.AppendLine(label.RenderEndTag());

                grid.InnerHtml.AppendHtml(cell.RenderStartTag());
                grid.InnerHtml.AppendHtml(cell.RenderBody());
                grid.InnerHtml.AppendLine(cell.RenderEndTag());

                index++;
            }

            mainContainer.InnerHtml.AppendHtml(grid.RenderStartTag());
            mainContainer.InnerHtml.AppendHtml(grid.RenderBody());
            mainContainer.InnerHtml.AppendLine(grid.RenderEndTag());

            string result;

            using (TextWriter textWriter = new StringWriter()) {
                mainContainer.WriteTo(textWriter, HtmlEncoder.Default);
                result = textWriter.ToString();
            }

            return(new HtmlString(result));
        }