Пример #1
0
        /// <summary>
        /// Prints a number of items in a grid as HTML table
        /// </summary>
        /// <typeparam name="T">Type of items to be processed.</typeparam>
        /// <param name="htmlHelper">HTML helper. Cannot be null.</param>
        /// <param name="items">List of items to be printed. Cannot be null or empty.</param>
        /// <param name="columns">Number of  columns in the grid. Must be higher than 0.</param>
        /// <param name="contentFunc">
        /// Provides the content for individual table cells. Return value is raw HTML. Cannot be null.</param>
        /// <returns></returns>
        public static MvcHtmlString Grid <T>(this HtmlHelper htmlHelper, IEnumerable <T> items,
                                             int columns, Func <T, IHtmlString> contentFunc)
        {
            ParamIs.NotNull(() => htmlHelper);
            ParamIs.NotNull(() => items);
            ParamIs.Positive(columns, "columns");
            ParamIs.NotNull(() => contentFunc);

            var        tableTag = new TagBuilder("table");
            TagBuilder trTag    = null;
            int        i        = 0;

            foreach (var item in items)
            {
                if (i % columns == 0)
                {
                    if (trTag != null)
                    {
                        tableTag.InnerHtml += trTag.ToString();
                    }

                    trTag = new TagBuilder("tr");
                }

                var tdTag = new TagBuilder("td");
                tdTag.InnerHtml  = contentFunc(item).ToHtmlString();
                trTag.InnerHtml += tdTag.ToString();
                i++;
            }

            if (trTag != null)
            {
                tableTag.InnerHtml += trTag.ToString();
            }

            return(new MvcHtmlString(tableTag.ToString()));
        }
Пример #2
0
        public static IHtmlContent Grid <T>(this IHtmlHelper htmlHelper, IEnumerable <T> items,
                                            int columns, Func <T, IHtmlContent> contentFunc)
        {
            ParamIs.NotNull(() => htmlHelper);
            ParamIs.NotNull(() => items);
            ParamIs.Positive(columns, "columns");
            ParamIs.NotNull(() => contentFunc);

            var        tableTag = new TagBuilder("table");
            TagBuilder trTag    = null;
            int        i        = 0;

            foreach (var item in items)
            {
                if (i % columns == 0)
                {
                    if (trTag != null)
                    {
                        tableTag.InnerHtml.AppendHtml(trTag);
                    }

                    trTag = new TagBuilder("tr");
                }

                var tdTag = new TagBuilder("td");
                tdTag.InnerHtml.AppendHtml(contentFunc(item));
                trTag.InnerHtml.AppendHtml(tdTag);
                i++;
            }

            if (trTag != null)
            {
                tableTag.InnerHtml.AppendHtml(trTag);
            }

            return(tableTag);
        }
Пример #3
0
        /// <summary>
        /// Prints a number of items in a grid as HTML table
        /// </summary>
        /// <typeparam name="T">Type of items to be processed.</typeparam>
        /// <param name="htmlHelper">HTML helper. Cannot be null.</param>
        /// <param name="items">List of items to be printed. Cannot be null or empty.</param>
        /// <param name="columns">Number of  columns in the grid. Must be higher than 0.</param>
        /// <param name="contentFunc">
        /// Provides the content for individual table cells. Return value is raw HTML. Cannot be null.</param>
        /// <returns></returns>
        public static MvcHtmlString Grid <T>(this HtmlHelper htmlHelper, IEnumerable <T> items,
                                             int columns, Func <T, IHtmlString> contentFunc)
        {
            ParamIs.NotNull(() => htmlHelper);
            ParamIs.NotNull(() => items);
            ParamIs.Positive(columns, "columns");
            ParamIs.NotNull(() => contentFunc);

            var        tableTag = new TagBuilder("table");
            TagBuilder trTag    = null;
            int        i        = 0;

            foreach (var item in items)
            {
                if (i % columns == 0)
                {
                    if (trTag != null)
                    {
                        tableTag.InnerHtml += trTag.ToString();
                    }

                    trTag = new TagBuilder("tr");
                }

                var tdTag = new TagBuilder("td");
                tdTag.InnerHtml  = contentFunc(item).ToHtmlString();
                trTag.InnerHtml += tdTag.ToString();
                i++;
            }

            if (trTag != null)
            {
                tableTag.InnerHtml += trTag.ToString();
            }

            /*
             *      @{ int i = 0; }
             *
             * @foreach (var user in users) {
             * if (i % columns == 0) {
             *      @Html.Raw("<tr>")
             * }
             * <td>
             *      @ProfileIcon(user, 20)
             * </td>
             * <td>
             *      @UserLink(user)
             * </td>
             * { i++; }
             * if (i % columns == 0) {
             *      @Html.Raw("</tr>")
             * }
             * }
             * @if (i % columns != 0) {
             * @Html.Raw("</tr>")
             * }
             *
             */

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