public static IQueryable <T> ApplySort <T>(this IQueryable <T> query, DataTablesOptions options, IList <IPropertyMapping> mappings) { if (!options.Order.Any()) { return(query); } if (!mappings.Any()) { return(query); } var sortableColumns = options.GetSortableColumns().ToArray(); if (!sortableColumns.Any()) { return(query); } var sortOptions = options.Order.Select(order => new { sortBy = mappings.First(m => m.DestinationProperty.Equals(sortableColumns[order.Column].Data, StringComparison.OrdinalIgnoreCase)).SourceProperty, sortDirection = order.GetSortDirection().ToString() }); return(sortOptions.Aggregate(query, (current, option) => current.OrderBy($"{option.sortBy} {option.sortDirection}"))); }
public void Create_Should_Return_Data_For_The_Last_Page() { //arrange var items = new List <Item>(); for (var i = 0; i <= 5; i++) { items.AddRange(TestSeed.GetItemsForTesting()); } var sourceCount = items.Count; var paginationData = new DataTablesOptions { Length = 5, Start = 15 }; //act var result = PagedList <Item> .Create(items.AsQueryable(), paginationData); //assert result.Count.Should().Be(sourceCount - paginationData.Start); result.PageSize.Should().Be(paginationData.Length); result.PageNumber.Should().Be(4); result.TotalCount.Should().Be(sourceCount); }
public void ApplySearch_NoMatchingSearchValue_ShouldReturnQuery() { //arrange var query = TestSeed.GetQueryableEmployees(); var dtPostModel = new DataTablesOptions { Columns = new List <Column> { new Column { Data = "FirstName", Searchable = true }, new Column { Data = "LastName", Searchable = true } }, Search = new Search { Regex = string.Empty, Value = "Oh my God, zombies!" } }; //act var result = query.ApplySearch(dtPostModel, _propertyMappings).ToList(); //assert result.Should().BeNullOrEmpty(); }
public void ApplySearch_NonStringValue_MatchingSearchValue_ShouldReturnAllMatchingEntities() { //arrange var query = TestSeed.GetQueryableEmployees(); var expectedId = query.First().Id; var dtPostModel = new DataTablesOptions { Columns = new List <Column> { new Column { Data = "Id", Searchable = true } }, Search = new Search { Regex = string.Empty, Value = expectedId.ToString() } }; //act var result = query.ApplySearch(dtPostModel, _propertyMappings).ToList(); //assert result.Should().NotBeNullOrEmpty(); result.Count.Should().Be(1); result.First().Id.Should().Be(expectedId); }
public void ApplySort_NoOrderableColumnsDefined_ShouldReturnQuery() { //arrange var query = TestSeed.GetQueryableEmployees(); var dtPostModel = new DataTablesOptions { Order = new List <Order> { new Order { Column = 0, Dir = "asc" } }, Columns = new List <Column> { new Column { Orderable = false } } }; //act var result = query.ApplySort(dtPostModel, _propertyMappings).ToList(); //assert result.Should().BeEquivalentTo(query.ToList()); }
private static string InitialData <TModel>(DataTablesOptions <TModel> options) { var body = new StringBuilder(); foreach (var row in options.Data) { var tr = new StringBuilder("{"); foreach (var column in options.Columns) { if (string.IsNullOrEmpty(column.Name)) { continue; } var property = row.GetType().GetProperty(column.Name); var value = (object)property != null?property.GetValue(row, null)?.ToString() .Replace("\\", @"\u005c") // Because it JS string escape character .Replace("\"", @"\u0022") // Because it may be string delimiter .Replace("'", @"\u0027") // Because it may be string delimiter .Replace("&", @"\u0026") // Because it may interfere with HTML parsing .Replace("<", @"\u003c") // Because it may interfere with HTML parsing .Replace(">", @"\u003e") // Because it may interfere with HTML parsing; : string.Empty; tr.Append($@"""{column.Name}"":""{value}"","); } tr.Length--; tr.Append("},"); body.Append(tr); } body.Length--; return($"[{body}]"); }
public void ApplySort_SortByDescending_NestedClassProperty_ShouldReturnSortedEntities() { //arrange var query = TestSeed.GetQueryableEmployees(); var dtPostModel = new DataTablesOptions { Columns = new List <Column> { new Column { Data = "Company.Name", Orderable = true } }, Order = new List <Order> { new Order { Column = 0, Dir = "desc" } } }; //act var result = query.ApplySort(dtPostModel, _propertyMappings).ToList(); //assert result.Should().BeInDescendingOrder(e => e.Company.Name); }
public void ApplySearch_NestedProperty_ShouldReturnAllMatchingEntities() { //arrange var query = TestSeed.GetQueryableEmployees(); var expected = query.Where(e => e.Company.Name.Equals("Microsoft")).ToList(); var dtPostModel = new DataTablesOptions { Columns = new List <Column> { new Column { Data = "Company.Name", Searchable = true } }, Search = new Search { Regex = string.Empty, Value = "Microsoft" } }; //act var result = query.ApplySearch(dtPostModel, _propertyMappings).ToList(); //assert result.Should().NotBeNullOrEmpty(); result.Count.Should().Be(expected.Count); result.Select(e => e.Company.Name.Should().Be("Microsoft")); }
public ActionResult KullaniciYetkiler(DataTablesOptions model) { var kullaniciYetkilers = RolBazliKullaniciYetkiListesi(model.UstID).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); //if (!string.IsNullOrEmpty(model.Search?.Value)) aksDuzens = aksDuzens; var filter = kullaniciYetkilers.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = kullaniciYetkilers.Count, recordsTotal = kullaniciYetkilers.Count, data = filter })); }
/// <summary> /// Gets the DataTables. /// </summary> /// <param name="helper">The HTML helper instance that this method extends.</param> /// <param name="id">Id table.</param> /// <param name="options">Options for create table.</param> /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param> /// <returns></returns> public static MvcHtmlString DataTables(this HtmlHelper helper, string id, DataTablesOptions options, object htmlAttributes) { if (string.IsNullOrEmpty(id)) { throw new ArgumentException("Empty: Id"); } return(Tables.Create(id, options, htmlAttributes)); }
public ActionResult AksDuzenler(DataTablesOptions model) { var aksDuzens = _aksDuzenService.GetAll(a => a.Aktif == true && a.AracKategoriID == model.UstID).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); //if (!string.IsNullOrEmpty(model.Search?.Value)) aksDuzens = aksDuzens; var filter = aksDuzens.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = aksDuzens.Count, recordsTotal = aksDuzens.Count, data = filter })); }
internal static string SetUp(DataTablesOptions options) { var optionsBuilder = new StringBuilder(); var msg = string.Empty; InitialOptions(options, ref msg); optionsBuilder.AppendLine(msg); return(optionsBuilder.ToString()); }
public ActionResult LastikHareketler([FromBody] DataTablesOptions model) { var lastikHareketler = _lastikHareketlerService.GetAll(a => a.LastikID == model.UstID && a.Aktif == true).AsQueryable() // .OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir) .ToList(); // if (!string.IsNullOrEmpty(model.Search?.Value)) lastikHareketler = lastikHareketler.Where(a => a.AracKilometre.ToString().IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.LastikKilometre.ToString().IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.Tarih.ToString().IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); ; var filter = lastikHareketler.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = lastikHareketler.Count, recordsTotal = lastikHareketler.Count, data = filter })); }
public IActionResult GetCompanies([FromBody] DataTablesOptions options) { var mappings = _propertyMappingService.GetMappings <Company, CompanyDto>(); var companies = _companyRepository.GetCompanies() .ApplySearch(options, mappings) .ApplySort(options, mappings) .ProjectTo <CompanyDto>(_mapper.ConfigurationProvider) .ToPagedList(options); return(DataTablesResult(companies)); }
public IActionResult GetEmployees([FromBody] DataTablesOptions model) { var propertyMappings = _propertyMappingService.GetMappings <Employee, EmployeeDto>(); var employees = _employeeRepository.GetEmployees() .ApplySearch(model, propertyMappings) .ApplySort(model, propertyMappings) .ProjectTo <EmployeeDto>(_mapper.ConfigurationProvider) .ToPagedList(model); return(DataTablesResult(employees)); }
public ActionResult Ebatlar([FromBody] DataTablesOptions model) { var ebatlar = _ebatlarService.GetAll(a => a.Aktif == true).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { ebatlar = ebatlar.Where(a => a.Ad.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = ebatlar.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = ebatlar.Count, recordsTotal = ebatlar.Count, data = filter })); }
public void ApplySort_NoOrderDefined_ShouldReturnQuery() { //arrange var query = TestSeed.GetQueryableEmployees(); var dtPostModel = new DataTablesOptions(); //act var result = query.ApplySort(dtPostModel, _propertyMappings).ToList(); //assert result.Should().BeEquivalentTo(query.ToList()); }
public ActionResult AracModeller(DataTablesOptions model) { var aracModellers = _aracModellerService.GetAll(a => a.Aktif == true && a.AracMarkaID == model.UstID).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { aracModellers = aracModellers.Where(a => a.Ad.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = aracModellers.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = aracModellers.Count, recordsTotal = aracModellers.Count, data = filter })); }
public ActionResult Isletmeler(DataTablesOptions model) { var isletmeler = RolBazliIsletmeListesi().Select(a => new { a.FirmaID, a.FirmaAd, a.FirmaKisaAd, a.VergiTCNo, a.Aktif }).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { isletmeler = isletmeler.Where(a => a.FirmaAd.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.VergiTCNo.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = isletmeler.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = isletmeler.Count, recordsTotal = isletmeler.Count, data = filter })); }
public ActionResult Kullanicilar(DataTablesOptions model) { var kullanicilars = RolBazliKullaniciListesi(model.UstID).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { kullanicilars = kullanicilars.Where(a => a.FirmaAd.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.FirmaKisaAd.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = kullanicilars.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = kullanicilars.Count, recordsTotal = kullanicilars.Count, data = filter })); }
public ActionResult LastikMarkaDesenOzellikler([FromBody] DataTablesOptions model) { var lastikMarkaDesenOzellikler = _lastikMarkaDesenOzelliklerService.GetAll(a => a.Aktif == true && a.LastikMarkaDesenID == model.UstID).AsQueryable().OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { lastikMarkaDesenOzellikler = lastikMarkaDesenOzellikler.Where(a => a.DisDerinligi.ToString().IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.KatOrani.ToString().IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = lastikMarkaDesenOzellikler.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = lastikMarkaDesenOzellikler.Count, recordsTotal = lastikMarkaDesenOzellikler.Count, data = filter })); }
internal static string SetUp <TModel>(DataTablesOptions <TModel> options) { var optionsBuilder = new StringBuilder(); var msg = string.Empty; InitialOptions(options, ref msg); if (options.Data != null && options.Data.Any()) { optionsBuilder.AppendLine($"'data': {InitialData(options)},"); } optionsBuilder.AppendLine(msg); return(optionsBuilder.ToString()); }
public ActionResult Subeler(DataTablesOptions model) { if (!_userJWTInfo.UserNullOrEmpty()) { var subeler = RolBazliSubeListesi().AsQueryable().Select(a => new { a.FirmaID, a.FirmaAd, a.YetkiliKisi, a.BagliOlduguID, a.FirmaKisaAd, a.VergiTCNo, a.Aktif }).OrderBy(model.Columns[model.Order[0].Column].Data + " " + model.Order[0].Dir).ToList(); if (!string.IsNullOrEmpty(model.Search?.Value)) { subeler = subeler.Where(a => a.FirmaAd.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1 || a.VergiTCNo.IndexOf(model.Search.Value, 0, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); } var filter = subeler.Skip(model.Start).Take(model.Length).ToList(); return(Ok(new { draw = model.Draw, recordsFiltered = subeler.Count, recordsTotal = subeler.Count, data = filter })); } else { return(Ok(new { Error = "Lütfen teknik destek ile iletişime geçiniz. 'Kullanıcı bilgileri session sorunu.'" })); } }
public void ToPagedList_Should_CreatePagedListWithDataForTheNextPage() { //arrange var paginationData = new DataTablesOptions { Length = 5, Start = 5 }; //act var result = _employees.AsQueryable().ToPagedList(paginationData); //assert result.Count.Should().Be(paginationData.Length); result.PageSize.Should().Be(paginationData.Length); result.PageNumber.Should().Be(2); result.TotalCount.Should().Be(_employees.Count); }
public DataTablesResponse <List <Product> > GetProducts([FromBody] DataTablesOptions options, bool related = false) { IQueryable <Product> query = context.Products; List <Product> products; if (!string.IsNullOrWhiteSpace(options.search.value)) { string searchLower = options.search.value.ToLower(); query = query.Where(p => p.Name.ToLower().Contains(searchLower) || p.Description.ToLower().Contains(searchLower)); } if (related) { query = query.Include(p => p.Supplier).Include(p => p.Ratings); products = query.ToList(); products.ForEach(p => { if (p.Supplier != null) { p.Supplier.Products = null; } if (p.Ratings != null) { p.Ratings.ForEach(r => r.Product = null); } }); } else { products = query.ToList(); } return(new DataTablesResponse <List <Product> > { data = products.Skip(options.Start).Take(options.Length).ToList(), recordsTotal = products.Count, recordsFiltered = products.Count }); }
public void ToPagedList_ShouldReturnDataForTheLastPage() { //arrange var sourceCount = _employees.Count; var paginationData = new DataTablesOptions { Length = 5, Start = 15 }; //act var result = _employees.AsQueryable().ToPagedList(paginationData); //assert result.Count.Should().Be(sourceCount - paginationData.Start); result.PageSize.Should().Be(paginationData.Length); result.PageNumber.Should().Be(4); result.TotalCount.Should().Be(sourceCount); }
public void Create_Should_Create_PagedList_With_Data_Only_For_The_First_Page() { //arrange var paginationData = new DataTablesOptions { Length = 3, Start = 0 }; var items = TestSeed.GetItemsForTesting(); //act var result = PagedList <Item> .Create(items.AsQueryable(), paginationData); //assert result.Count.Should().Be(paginationData.Length); result.PageSize.Should().Be(paginationData.Length); result.PageNumber.Should().Be(1); result.TotalCount.Should().Be(items.Count); }
public ActionResult Index(DataTablesParam dataTableParam = null) { if (Request.IsAjaxRequest()) { var cars = db.Cars.AsQueryable(); // swap out sort fields with ones DynamicLinq will handle. var dataTableOptions = new DataTablesOptions(); dataTableOptions.SearchAliases.Add("Name", "Year"); return DataTablesResult.Create(cars, dataTableParam, dataTableOptions, oo => new { Name = oo.FriendlyName, Year = oo.Year.ToString().Substring(2), Id = oo.Id }); } else { return View(); } }
public static IQueryable <TEntity> ApplySearch <TEntity>(this IQueryable <TEntity> query, DataTablesOptions options, IList <IPropertyMapping> mappings) { if (!mappings.Any()) { return(query); } var searchableColumns = options.GetSearchableColums(); if (!searchableColumns.Any()) { return(query); } var expression = GetSearchableProperties(mappings, searchableColumns) .Select(property => BuildLambaExpression <TEntity>(options.Search.Value, property)) .Where(lambda => lambda != null) .Aggregate <Expression <Func <TEntity, bool> >, Expression <Func <TEntity, bool> > >(null, (current, lambda) => current == null ? lambda : current.Or(lambda)); return(expression == null ? query : query.Where(expression)); }
public static PagedList <T> ToPagedList <T>(this IQueryable <T> query, DataTablesOptions options) { return(PagedList <T> .Create(query, options)); }
internal static TagBuilder Script <TModel>(string id, DataTablesOptions <TModel> options) { var getOptions = Options.SetUp(options); return(GetScript(id, getOptions)); }