public async Task <JsonResult> OnPostPaging([FromForm] DataTableAjaxPostModel Model) { int filteredResultsCount = 0; int totalResultsCount = 0; DataTableAjaxPostModel.GetOrderByParameters(Model.order, Model.columns, "LocationName", out bool SortDir, out string SortBy); if (SortBy.ToLower().Contains("gps")) { SortBy = ExtensionMethods.UppercaseSpecifiedNumbers(SortBy, 3); } //First create the View of the new model you wish to display to the user var LocationQuery = _context.Locations .Include(p => p.Province) .Include(c => c.Province.Country) .Select(loc => new { loc.LocationID, loc.Province.Country.CountryName, loc.Province.ProvinceName, loc.LocationName, loc.GPSCoordinates } ); totalResultsCount = LocationQuery.Count(); filteredResultsCount = totalResultsCount; if (!string.IsNullOrEmpty(Model.search.value)) { LocationQuery = LocationQuery .Where( l => l.LocationName.ToLower().Contains(Model.search.value.ToLower()) || l.GPSCoordinates.ToString().ToLower().Contains(Model.search.value.ToLower()) || l.CountryName.ToLower().Contains(Model.search.value.ToLower()) || l.ProvinceName.ToLower().Contains(Model.search.value.ToLower())); filteredResultsCount = LocationQuery.Count(); } var Result = await LocationQuery .Skip(Model.start) .Take(Model.length) .OrderBy(SortBy, SortDir) .ToListAsync(); var value = new { // this is what datatables wants sending back draw = Model.draw, recordsTotal = totalResultsCount, recordsFiltered = filteredResultsCount, data = Result }; return(new JsonResult(value)); }
public override void Process(TagHelperContext context, TagHelperOutput output) { IEnumerable model = DataModel.Model as IEnumerable; if (model == null) { return; } else { StringBuilder tableTag = new StringBuilder(); //Getting properties for List Type Type listType = Type.GetType(((System.Reflection.TypeInfo)(model.GetType()).GenericTypeArguments[0]).FullName); var properties = listType.GetProperties(); tableTag.Append("<thead><tr role='row'>"); //Simple table, header not sortable if (!Sortable) { foreach (var property in properties) { tableTag.Append("<th >"); //If the property has display name, get the display name var isDisplayNameAttributeDefined = Attribute.IsDefined(property, typeof(DisplayNameAttribute)); if (isDisplayNameAttributeDefined) { DisplayNameAttribute dna = (DisplayNameAttribute)Attribute.GetCustomAttribute(property, typeof(DisplayNameAttribute)); tableTag.Append(dna.DisplayName); } else { tableTag.Append(property.Name.ToString()); } tableTag.Append("</th>"); } } else // for sortable table { string linkPretextTag = "<a href = '/" + Controller + "/" + Action; QueryString = QueryString ?? ""; foreach (var property in properties) { string columnName = property.Name.ToString(); var isDisplayNameAttributeDefined = Attribute.IsDefined(property, typeof(DisplayNameAttribute)); if (isDisplayNameAttributeDefined) { DisplayNameAttribute dna = (DisplayNameAttribute)Attribute.GetCustomAttribute(property, typeof(DisplayNameAttribute)); columnName = dna.DisplayName; } tableTag.Append("<th aria-controls='"); tableTag.Append(ID); if (property.Name.ToString().ToLower() == SortBy.ToLower()) { //if sorted descending if (SortDirection.ToLower() == "desc") { tableTag.Append("' class='sorting_desc' aria-sort='descending' aria-label='"); tableTag.Append(columnName); tableTag.Append(": activate to sort column ascending' >"); tableTag.Append(linkPretextTag); tableTag.Append("?sortorder="); tableTag.Append(property.Name.ToString()); tableTag.Append(QueryString); tableTag.Append("'>"); // sb.Append("&sortDirection=asc'>"); } else { tableTag.Append("' class='sorting_asc' aria-sort='ascending' aria-label='"); tableTag.Append(columnName); tableTag.Append(": activate to sort column descending' >"); tableTag.Append(linkPretextTag); tableTag.Append("?sortorder="); tableTag.Append(property.Name.ToString()); tableTag.Append(QueryString); tableTag.Append("&sortDirection=desc'>"); } } else { tableTag.Append("' class='sorting' aria-label='"); tableTag.Append(columnName); tableTag.Append(": activate to sort column ascending' >"); tableTag.Append(linkPretextTag); tableTag.Append("?sortorder="); tableTag.Append(property.Name.ToString()); tableTag.Append(QueryString); tableTag.Append("'>"); // sb.Append("&sortDirection=asc'>"); } tableTag.Append(columnName); tableTag.Append("</a></th>"); } } tableTag.Append("</tr></thead>"); //Generate data foreach (var m in model) { PropertyInfo[] dataProperties = m.GetType().GetProperties(); tableTag.Append("<tr>"); for (int i = 0; i < dataProperties.Length; i++) { tableTag.Append("<td>" + m.GetType().GetProperty(dataProperties[i].Name).GetValue(m, null) + "</td>"); } tableTag.Append("</tr>"); } // tableTag.Append("<colgroup> <col span = '2' style = 'background-color:red' > <col style = 'background-color:yellow' ></colgroup>"); output.Content.SetHtmlContent(tableTag.ToString()); } }