Exemplo n.º 1
0
        //TODO: Test
        public static MvcHtmlString Table <T>(this HtmlHelper html, IEnumerable <T> items, object htmlAttributes = null)
        {
            var builder = new FluentTagBuilder("table")
                          .MergeAttributes(htmlAttributes)
                          .StartTag("thead")
                          .StartTag("tr");

            var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                builder = builder.StartTag("th").SetInnerText(property.Name).EndTag();
            }

            builder
            .EndTag()     // </tr>
            .EndTag()     // </thead>
            .StartTag("tbody");

            foreach (var item in items)
            {
                builder = builder.StartTag("tr");
                foreach (var property in properties)
                {
                    string value = property.GetValue(item).ToString();
                    builder = builder.StartTag("td").SetInnerText(value).EndTag();
                }
                builder = builder.EndTag(); // </tr>
            }
            builder = builder.EndTag();     // </tbody>

            return(MvcHtmlString.Create(builder.ToString()));
        }
Exemplo n.º 2
0
        public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(
            this HtmlHelper <TModel> html,
            Expression <Func <TModel, IEnumerable <TProperty> > > expression,
            IEnumerable <SelectListItem> selectList,
            byte numberOfColumns          = 1,
            object labelHtmlAttributes    = null,
            object checkboxHtmlAttributes = null) where TModel : class
        {
            string htmlFieldName     = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
            string fullHtmlFieldId   = html.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName);

            var func           = expression.Compile();
            var selectedValues = func(html.ViewData.Model);

            var values = new List <string>();

            if (selectedValues != null)
            {
                values.AddRange(selectedValues.Select(x => Convert.ToString(x)));
            }

            if (selectList == null)
            {
                throw new ArgumentNullException("selectList");
            }

            int index = 0;

            var sb = new StringBuilder();

            bool groupByCategory = (selectList.First() is ExtendedSelectListItem);

            if (groupByCategory)
            {
                var items  = selectList.Cast <ExtendedSelectListItem>().ToList();
                var groups = items.GroupBy(x => x.Category);

                foreach (var @group in groups)
                {
                    sb.AppendFormat("<strong>{0}</strong>", group.Key);

                    foreach (var item in group)
                    {
                        var isChecked = values.Contains(item.Value);

                        var tagBuilder = new FluentTagBuilder("label")
                                         .MergeAttribute("for", fullHtmlFieldName)
                                         .MergeAttributes(labelHtmlAttributes)
                                         .StartTag("input", TagRenderMode.SelfClosing)
                                         .MergeAttribute("type", "checkbox")
                                         .MergeAttribute("name", fullHtmlFieldName)
                                         .MergeAttribute("id", fullHtmlFieldId + "_" + index)
                                         .MergeAttribute("value", item.Value);

                        if (isChecked)
                        {
                            tagBuilder = tagBuilder.MergeAttribute("checked", "checked");
                        }

                        if (checkboxHtmlAttributes != null)
                        {
                            tagBuilder = tagBuilder.MergeAttributes(checkboxHtmlAttributes);
                        }

                        tagBuilder = tagBuilder.EndTag(); //end checkbox
                        tagBuilder = tagBuilder
                                     .StartTag("span")
                                     .SetInnerText(item.Text)
                                     .EndTag();

                        sb.Append(tagBuilder.ToString());
                        index++;
                    }
                }
            }
            else
            {
                var rows        = (int)Math.Ceiling((selectList.Count() * 1d) / numberOfColumns);
                var columnWidth = (int)Math.Ceiling(12d / numberOfColumns);

                for (var i = 0; i < numberOfColumns; i++)
                {
                    var items = selectList.Skip(i * rows).Take(rows);
                    sb.AppendFormat("<div class=\"col-md-{0}\">", columnWidth);

                    foreach (var item in items)
                    {
                        var isChecked = values.Contains(item.Value);

                        var tagBuilder = new FluentTagBuilder("label")
                                         .MergeAttribute("for", fullHtmlFieldName)
                                         .MergeAttributes(labelHtmlAttributes)
                                         .StartTag("input", TagRenderMode.SelfClosing)
                                         .MergeAttribute("type", "checkbox")
                                         .MergeAttribute("name", fullHtmlFieldName)
                                         .MergeAttribute("id", fullHtmlFieldId + "_" + index)
                                         .MergeAttribute("value", item.Value);

                        if (isChecked)
                        {
                            tagBuilder = tagBuilder.MergeAttribute("checked", "checked");
                        }

                        if (checkboxHtmlAttributes != null)
                        {
                            tagBuilder = tagBuilder.MergeAttributes(checkboxHtmlAttributes);
                        }

                        tagBuilder = tagBuilder.EndTag(); //end checkbox
                        tagBuilder = tagBuilder
                                     .StartTag("span")
                                     .SetInnerText(item.Text)
                                     .EndTag();

                        sb.Append(tagBuilder.ToString());
                        index++;
                    }

                    sb.Append("</div>");
                }
            }

            return(new MvcHtmlString(sb.ToString()));
        }