public void RenderPager(DataTablesGridModel <T> model, IEnumerable <T> dataSource, int?filteredRowCount, int totalRowCount, TextWriter output) { output.Write("<div class='dataTables_info'>"); if ((filteredRowCount ?? totalRowCount) < 1) { output.Write("No entries"); } else { output.Write("Showing {0} to {1} of {2} entries", Math.Min(model.PageIndex * model.RowsPerPage + 1, (filteredRowCount ?? totalRowCount)), Math.Min((model.PageIndex + 1) * model.RowsPerPage, (filteredRowCount ?? totalRowCount)), filteredRowCount ?? totalRowCount); } if (filteredRowCount.HasValue) { output.Write(" (filtered from {0} total entries)", totalRowCount); } output.Write("</div>"); output.Write("<div class='dataTables_paginate paging_two_button'>"); if (model.PageIndex < 1) { output.Write("<div class='paginate_disabled_previous' title='Previous'></div>"); } else { output.Write("<a href='{0}' class='paginate_enabled_previous' title='Previous'></a>", HttpUtility.HtmlEncode(model.GetPageUrl(model.PageIndex - 1))); } if ((model.PageIndex + 1) * model.RowsPerPage >= (filteredRowCount ?? totalRowCount)) { output.Write("<div class='paginate_disabled_next' title='Next' title='Next'></div>"); } else { output.Write("<a href='{0}' class='paginate_enabled_next' title='Next'></a>", HttpUtility.HtmlEncode(model.GetPageUrl(model.PageIndex + 1))); } output.Write("</div>"); }
public bool IsSorted(DataTablesGridModel <T> model) { return(ColumnName.Equals(model.SortColumnName, StringComparison.OrdinalIgnoreCase)); }
public void Render(DataTablesGridModel <T> model, IEnumerable <T> dataSource, int?filteredRowCount, int totalRowCount, TextWriter output) { if (model.CanAnyColumnFilter) { output.Write("<form method='get' action='{0}' class='form'>", HttpUtility.HtmlEncode(model.GetFilterUrl())); if (model.IsSortParameterSpecified) { output.Write("<input type='hidden' name='{0}' value='{1}'/>", HttpUtility.HtmlEncode(model.SortParameterName), HttpUtility.HtmlEncode(model.SortColumnName)); } if (model.IsSortAscendingParameterSpecified) { output.Write("<input type='hidden' name='{0}' value='{1}'/>", HttpUtility.HtmlEncode(model.SortAscendingParameterName), model.SortAscending); } if (!string.IsNullOrWhiteSpace(model.ParameterNamePrefix)) { output.Write("<input type='hidden' name='parameterNamePrefix' value='{0}'/>", HttpUtility.HtmlEncode(model.ParameterNamePrefix)); } } output.Write("<table cellspacing='0' cellpadding='0' border='0' class='display'>"); output.Write("<thead><tr>"); foreach (var column in model.Columns) { if (column.CanSort) { string @class = column.IsSorted(model) ? model.SortAscending ? "sorting_asc" : "sorting_desc" : string.Empty; output.Write("<th class='sortable {0}'>", @class); output.Write("<a href='{0}'><span>{1}</span></a>", HttpUtility.HtmlEncode(model.GetSortUrl(column.ColumnName, column.DefaultSortAscending)), HttpUtility.HtmlEncode(column.Header)); } else { output.Write("<th>"); output.Write(HttpUtility.HtmlEncode(column.Header)); } output.Write("</th>"); } if (model.CanAnyColumnFilter) { output.Write("</tr><tr class='filters'>"); foreach (var column in model.Columns) { output.Write("<th class='field'>"); if (column.CanFilter) { output.Write("<input type='text' name='{0}' value='{1}'", model.GetFilterParameterName(column.ColumnName), model.FiltersByColumnName.ContainsKey(column.ColumnName) ? HttpUtility.HtmlEncode(model.FiltersByColumnName[column.ColumnName]) : string.Empty ); if (column.FilterTitle != null) { output.Write(" title='{0}'", HttpUtility.HtmlEncode(column.FilterTitle)); output.Write(" placeholder='{0}'", HttpUtility.HtmlEncode(column.FilterTitle)); } output.Write("/>"); } output.Write("</th>"); } } output.Write("</tr></thead>"); output.Write("<tbody>"); bool isEven = false; foreach (T row in dataSource) { string @class = isEven ? "even" : "odd"; output.Write("<tr class='{0}'>", @class); foreach (var column in model.Columns) { output.Write("<td>"); output.Write(column.Evaluate(row)); output.Write("</td>"); } output.Write("</tr>"); isEven = !isEven; } output.Write("</tbody>"); output.Write("</table>"); if (model.CanAnyColumnFilter) { // workaround to allow form submission using enter key output.Write("<input type='submit' style='position: absolute; left: -9999px;' />"); output.Write("</form>"); } }
public static IHtmlString DataTablesGrid <T>(this HtmlHelper helper, Action <DataTablesColumnBuilder <T> > columns = null, int?pageIndex = null, string parameterNamePrefix = null, int?rowsPerPage = null, bool?sortAscending = null, string sortColumnName = null, IEnumerable <T> dataSource = null, int?totalRowCount = null, int?filteredRowCount = null, bool canPage = true) where T : class { var model = new DataTablesGridModel <T>(helper.ViewContext); if (columns == null) { throw new ArgumentException("Expected non null value for columns."); } var columnBuilder = new DataTablesColumnBuilder <T>(); columns(columnBuilder); model.Columns = columnBuilder.Columns; if (pageIndex.HasValue) { model.PageIndex = pageIndex.Value; } if (parameterNamePrefix != null) { model.ParameterNamePrefix = parameterNamePrefix; } if (rowsPerPage.HasValue) { model.RowsPerPage = rowsPerPage.Value; } if (!canPage) { model.RowsPerPage = int.MaxValue; } if (sortAscending.HasValue) { model.SortAscending = sortAscending.Value; } if (sortColumnName != null) { model.SortColumnName = sortColumnName; } if (dataSource == null) { throw new ArgumentException("Expected non null value for dataSource."); } int calculatedTotalRowCount = totalRowCount.HasValue ? totalRowCount.Value : dataSource.Count(); using (TextWriter output = new StringWriter()) { output.Write("<div class='dataTablesGrid'>"); var render = new DataTablesGridRenderer <T>(); render.Render(model, dataSource, filteredRowCount, calculatedTotalRowCount, output); if (canPage) { render.RenderPager(model, dataSource, filteredRowCount, calculatedTotalRowCount, output); } output.Write("</div>"); return(new HtmlString(output.ToString())); } }