@using System.Globalization @using System.Text @using System.Web.Helpers.Resources @using System.Web.Mvc @using System.Web.WebPages.Scope @using Microsoft.Internal.Web.Utils @helper GridInitScript(WebGrid webGrid, HttpContextBase httpContext) { if (!webGrid.IsAjaxEnabled) { return; } if (!IsGridScriptRendered(httpContext)) { SetGridScriptRendered(httpContext, true); } } @helper Table(WebGrid webGrid, HttpContextBase httpContext, string tableStyle, string headerStyle, string footerStyle, string rowStyle, string alternatingRowStyle, string selectedRowStyle, string caption, bool displayHeader, bool fillEmptyRows, string emptyRowCellValue, IEnumerable columns, IEnumerable exclusions, Func footer, object htmlAttributes) { if (emptyRowCellValue == null) { emptyRowCellValue = " "; } @GridInitScript(webGrid, httpContext) var htmlAttributeDictionary = TypeHelper.ObjectToDictionary(htmlAttributes); if (webGrid.IsAjaxEnabled) { htmlAttributeDictionary["data-swhgajax"] = "true"; htmlAttributeDictionary["data-swhgcontainer"] = webGrid.AjaxUpdateContainerId; htmlAttributeDictionary["data-swhgcallback"] = webGrid.AjaxUpdateCallback; } @if (!caption.IsEmpty()) { @caption } @if (displayHeader) { @foreach (var column in columns) { @if (ShowSortableColumnHeader(webGrid, column)) { var text = column.Header.IsEmpty() ? column.ColumnName : column.Header; @GridLink(webGrid, webGrid.GetSortUrl(column.ColumnName), text) } else { @(column.Header ?? column.ColumnName) } } } @if (footer != null) { @Format(footer, null) } @{ int rowIndex = 0; } @foreach (var row in webGrid.Rows) { string style = GetRowStyle(webGrid, rowIndex++, rowStyle, alternatingRowStyle, selectedRowStyle); @foreach (var column in columns) { var value = (column.Format == null) ? HttpUtility.HtmlEncode(row[column.ColumnName]) : Format(column.Format, row).ToString(); @Raw(value) } } @if (fillEmptyRows) { rowIndex = webGrid.Rows.Count; while (rowIndex < webGrid.RowsPerPage) { string style = GetRowStyle(webGrid, rowIndex++, rowStyle, alternatingRowStyle, null); @foreach (var column in columns) { @Raw(emptyRowCellValue) } } } } @helper Pager( WebGrid webGrid, HttpContextBase httpContext, WebGridPagerModes mode, string firstText, string previousText, string nextText, string lastText, int numericLinksCount, bool renderAjaxContainer) { int currentPage = webGrid.PageIndex; int totalPages = webGrid.PageCount; int lastPage = totalPages - 1; @GridInitScript(webGrid, httpContext) if (renderAjaxContainer && webGrid.IsAjaxEnabled) { @: } if (ModeEnabled(mode, WebGridPagerModes.FirstLast) && currentPage > 1) { if (String.IsNullOrEmpty(firstText)) { firstText = "<<"; } @GridLink(webGrid, webGrid.GetPageUrl(0), firstText) @Raw(" ") } if (ModeEnabled(mode, WebGridPagerModes.NextPrevious) && currentPage > 0) { if (String.IsNullOrEmpty(previousText)) { previousText = "<"; } @GridLink(webGrid, webGrid.GetPageUrl(currentPage - 1), previousText) @Raw(" ") } if (ModeEnabled(mode, WebGridPagerModes.Numeric) && (totalPages > 1)) { int last = currentPage + (numericLinksCount / 2); int first = last - numericLinksCount + 1; if (last > lastPage) { first -= last - lastPage; last = lastPage; } if (first < 0) { last = Math.Min(last + (0 - first), lastPage); first = 0; } for (int i = first; i <= last; i++) { var pageText = (i + 1).ToString(CultureInfo.InvariantCulture); if (i == currentPage) { @pageText } else { @GridLink(webGrid, webGrid.GetPageUrl(i), pageText) } @Raw(" ") } } if (ModeEnabled(mode, WebGridPagerModes.NextPrevious) && (currentPage < lastPage)) { if (String.IsNullOrEmpty(nextText)) { nextText = ">"; } @GridLink(webGrid, webGrid.GetPageUrl(currentPage + 1), nextText) @Raw(" ") } if (ModeEnabled(mode, WebGridPagerModes.FirstLast) && (currentPage < lastPage - 1)) { if (String.IsNullOrEmpty(lastText)) { lastText = ">>"; } @GridLink(webGrid, webGrid.GetPageUrl(lastPage), lastText) } if (renderAjaxContainer && webGrid.IsAjaxEnabled) { @: } } @functions{ private static readonly object _gridScriptRenderedKey = new object(); private static bool IsGridScriptRendered(HttpContextBase context) { bool? value = (bool?)context.Items[_gridScriptRenderedKey]; return value.HasValue && value.Value; } private static void SetGridScriptRendered(HttpContextBase context, bool value) { context.Items[_gridScriptRenderedKey] = value; } private static bool ShowSortableColumnHeader(WebGrid grid, WebGridColumn column) { return grid.CanSort && column.CanSort && !column.ColumnName.IsEmpty(); } public static IHtmlString GridLink(WebGrid webGrid, string url, string text) { TagBuilder builder = new TagBuilder("a"); builder.SetInnerText(text); builder.MergeAttribute("href", url); if (webGrid.IsAjaxEnabled) { builder.MergeAttribute("data-swhglnk", "true"); } return builder.ToHtmlString(TagRenderMode.Normal); } private static IHtmlString Raw(string text) { return new HtmlString(text); } private static IHtmlString RawJS(string text) { return new HtmlString(HttpUtility.JavaScriptStringEncode(text)); } private static IHtmlString CssClass(string className) { return new HtmlString((!className.IsEmpty()) ? " class=\"" + HttpUtility.HtmlAttributeEncode(className) + "\"" : String.Empty); } private static string GetRowStyle(WebGrid webGrid, int rowIndex, string rowStyle, string alternatingRowStyle, string selectedRowStyle) { StringBuilder style = new StringBuilder(); if (rowIndex % 2 == 0) { if (!String.IsNullOrEmpty(rowStyle)) { style.Append(rowStyle); } } else { if (!String.IsNullOrEmpty(alternatingRowStyle)) { style.Append(alternatingRowStyle); } } if (!String.IsNullOrEmpty(selectedRowStyle) && (rowIndex == webGrid.SelectedIndex)) { if (style.Length > 0) { style.Append(" "); } style.Append(selectedRowStyle); } return style.ToString(); } private static HelperResult Format(Func format, dynamic arg) { var result = format(arg); return new HelperResult(tw => { var helper = result as HelperResult; if (helper != null) { helper.WriteTo(tw); return; } IHtmlString htmlString = result as IHtmlString; if (htmlString != null) { tw.Write(htmlString); return; } if (result != null) { tw.Write(HttpUtility.HtmlEncode(result)); } }); } private static IHtmlString PrintAttributes(IDictionary attributes) { var builder = new StringBuilder(); foreach (var item in attributes) { var value = Convert.ToString(item.Value, CultureInfo.InvariantCulture); builder.Append(' ') .Append(HttpUtility.HtmlEncode(item.Key)) .Append("=\"") .Append(HttpUtility.HtmlAttributeEncode(value)) .Append('"'); } return new HtmlString(builder.ToString()); } private static bool ModeEnabled(WebGridPagerModes mode, WebGridPagerModes modeCheck) { return (mode & modeCheck) == modeCheck; } }