public async Task GetPageSortTest(string fieldName, SortType sortType, int[] ids) { //Arrange var teamProjects = Builder <ProjectTeam> .CreateListOfSize(10) .All().WithFactory(i => new ProjectTeam(_testData.Projects[i].Id, 1)) .Build(); var context = ContextHelper.CreateContext(_dbConnection, false); context.RemoveRange(_testData.ProjectTeams); await context.AddRangeAsync(teamProjects); await context.SaveChangesAsync(); var field = new FieldSort(fieldName, sortType); var pageOptions = new PageOptions { PageNumber = 0, PageSize = 10, SortFields = new[] { field } }; //Act var projects = (await _service.GetPage(_currentUser, 1, pageOptions)).ToArray(); //Assert for (int i = 0; i < ids.Length; i++) { Assert.AreEqual(ids[i], projects[i].Id); } }
protected override IQueryable <Group> SortByField(IQueryable <Group> query, FieldSort field, bool isAscending) { if (field.Is(nameof(VmGroup.Name))) { query = query.SortBy(x => x.Name, isAscending); } else if (field.Is(nameof(VmGroup.Description))) { query = query.SortBy(x => x.Description, isAscending); } else if (field.Is(nameof(VmGroup.IsRemoved))) { query = query.SortBy(x => x.IsRemoved, isAscending); } else if (field.Is(nameof(VmGroup.OwnerFio))) { query = query .SortBy(p => p.Owner.LastName, isAscending) .SortBy(p => p.Owner.FirstName, isAscending) .SortBy(p => p.Owner.MiddleName, isAscending); } return(query); }
public override void Compare(string compare, FieldSort defaultsort) { switch (this._tag) { case TableTag.TableHeaderCell: string format = "<a href=\"{0}\" target=\"_self\">{1}</a>"; string text = this._cellHeader.Text; if (this.OrderField == compare) { if (this._cellHeader.CssClass != null && this._cellHeader.CssClass.Length > 0) { this._cellHeader.CssClass = this._cellHeader.CssClass + " s"; } else { this._cellHeader.CssClass = "s"; } text = text + icon[(int)this.Sort]; text = string.Format(format, string.Format(this.OrderingEvent, this.OrderField, (((int)this.Sort) + 1) % 2), text); } else { text = string.Format(format, string.Format(this.OrderingEvent, compare, (int)defaultsort), text); } this._cellHeader.Text = text; this._row.Cells.AddAt(this._index - 1, this._cellHeader); break; } }
public static void WriteFieldSort(Utf8JsonWriter writer, FieldSort fieldSort, JsonSerializerOptions options, IElasticsearchClientSettings settings) { writer.WriteStartObject(); writer.WritePropertyName(settings.Inferrer.Field(fieldSort.Field)); writer.WriteStartObject(); if (fieldSort.Order.HasValue) { writer.WritePropertyName("order"); JsonSerializer.Serialize(writer, fieldSort.Order.Value, options); } if (fieldSort.Missing is not null) { writer.WritePropertyName("missing"); JsonSerializer.Serialize(writer, fieldSort.Missing, options); } if (!string.IsNullOrEmpty(fieldSort.Format)) { writer.WritePropertyName("format"); writer.WriteStringValue(fieldSort.Format); } if (fieldSort.Mode.HasValue) { writer.WritePropertyName("mode"); JsonSerializer.Serialize(writer, fieldSort.Mode.Value, options); } if (fieldSort.NumericType.HasValue) { writer.WritePropertyName("numeric_type"); JsonSerializer.Serialize(writer, fieldSort.NumericType.Value, options); } if (fieldSort.Nested is not null) { writer.WritePropertyName("nested"); JsonSerializer.Serialize(writer, fieldSort.Nested, options); } if (fieldSort.UnmappedType.HasValue) { writer.WritePropertyName("unmapped_type"); JsonSerializer.Serialize(writer, fieldSort.UnmappedType.Value, options); } if (fieldSort.IgnoreUnmapped.HasValue) { writer.WritePropertyName("ignore_unmapped"); JsonSerializer.Serialize(writer, fieldSort.IgnoreUnmapped.Value, options); } writer.WriteEndObject(); writer.WriteEndObject(); }
internal FieldSort ToFieldSort() { var fieldSort = new FieldSort(_field) { // Nested = // HARD IgnoreUnmapped = _ignoreUnmappedFields, UnmappedType = _unmappedType }; return(fieldSort); }
public DefaultTableStrategy(string field, FieldSort sort, Action <System.Web.UI.WebControls.Table> builder) { this._tag = TableTag.Table; this._table = new System.Web.UI.WebControls.Table(); if (builder != null) { builder(this._table); } this.OrderField = field; //排序字段 this.Sort = sort; //排序方式 this.DefaultSort = FieldSort.Desc; //默认排序 this.OrderingEvent = "javascript:pSort('{0}',{1})"; //默认排序事件 }
public DefaultTableStrategy(string field, FieldSort sort) { this._tag = TableTag.Table; this._table = new System.Web.UI.WebControls.Table(); //this._table.CellPadding = 0; //this._table.CellSpacing = 1; //this._table.BorderWidth = 0; //this._table.Attributes.Add("width", "100%"); this.OrderField = field; //排序字段 this.Sort = sort; //排序方式 this.DefaultSort = FieldSort.Desc; //默认排序 this.OrderingEvent = "javascript:pSort('{0}',{1})"; //默认排序事件 this.icon = new string[] { "▲", "▼" }; }
public static FieldSort BuildFieldSort(DataModel.Search.Sort.FieldSort fieldSort) { FieldSort.Builder builder = FieldSort.CreateBuilder(); builder.SetFieldName(fieldSort.FieldName); builder.SetOrder(BuildSortOrder(fieldSort.Order)); builder.SetMode(BuildSortMode(fieldSort.Mode)); if (fieldSort.NestedFilter != null) { builder.SetNestedFilter(BuildNestedFilter(fieldSort.NestedFilter)); } return(builder.Build()); }
public async Task <IActionResult> List(FieldSort currentSort, int currentPage = 1, string sortBy = "") { if (TempData["ElementErrorShort"] != null) { ModelState.AddModelError((string)TempData["ElementErrorShort"], (string)TempData["ElementErrorLong"]); } var categories = await appContext.Categories.ToListAsync(); int totalItems = categories.Count; var model = new CategoryViewModel { CurrentPage = currentPage, CurrentSort = currentSort, ItemOnPages = itemOnPages, TotalItems = totalItems, SortBy = sortBy }; if (String.IsNullOrEmpty(sortBy)) { if (currentSort == FieldSort.nameDescend) { model.Items = categories.OrderByDescending(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } else { model.Items = categories.OrderBy(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } } if (sortBy == "name") { model.CurrentPage = 1; if (currentSort == FieldSort.nameAscend) { model.CurrentSort = FieldSort.nameDescend; model.Items = categories.OrderByDescending(x => x.Name).Take(itemOnPages).ToList(); } else { model.CurrentSort = FieldSort.nameAscend; model.Items = categories.OrderBy(x => x.Name).Take(itemOnPages).ToList(); } } return(View(model)); }
public static IFieldSort GetDefaultSort(this TermNode node, IQueryVisitorContext context) { var elasticContext = context as IElasticQueryVisitorContext; if (elasticContext == null) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } string field = elasticContext.GetNonAnalyzedFieldName(node.Field, "sort"); var fieldType = elasticContext.GetFieldType(field); var sort = new FieldSort { Field = field, UnmappedType = fieldType == FieldType.None ? FieldType.Keyword : fieldType, Order = node.IsNodeOrGroupNegated() ? SortOrder.Descending : SortOrder.Ascending }; return(sort); }
private void HandleOrderByExpression(ReadOnlyCollection <Expression> arguments, SortDirectionConstants sortDir) { if (arguments.Count == 2) { var unaryExpr = arguments[1] as UnaryExpression; if (unaryExpr != null) { var fieldSort = new FieldSort(); fieldSort.Name = ExpressionHelper.GetMemberName(unaryExpr.Operand); fieldSort.SortDirection = sortDir; //Do not throw errow for now, just week it out //if (this.Query.FieldSorts.FindIndex(x => x.Name == fieldSort.Name) >= 0) // throw new InvalidOperationException($"Duplicate sort field \"{fieldSort.Name}\" was encountered."); //Only add if not exists if (this.Query.FieldSorts.FindIndex(x => x.Name == fieldSort.Name) == -1) { this.Query.FieldSorts.Add(fieldSort); } return; // Success } var constantExpr = arguments[1] as ConstantExpression; if (constantExpr != null) { var fieldSort = new FieldSort(); fieldSort.Name = (string)constantExpr.Value; fieldSort.SortDirection = sortDir; this.Query.FieldSorts.RemoveAll(x => x.Name == fieldSort.Name); this.Query.FieldSorts.Add(fieldSort); return; // Success } throw new InvalidOperationException("Invalid OrderBy expression arguments."); } }
private IList <ISort> GetSort(string sort) { var list = new List <ISort>(); if (string.IsNullOrEmpty(sort)) { return(null); } var sortArray = sort.Split(' '); var sortField = new FieldSort() { Field = $"{sortArray[0]}.keyword", Order = (sortArray[1] == "asc") ? SortOrder.Ascending : SortOrder.Descending, UnmappedType = FieldType.Text }; list.Add(sortField); return(list); }
protected override IQueryable <GoalMessage> SortByField(IQueryable <GoalMessage> query, FieldSort field, bool isAscending) { if (field.Is(nameof(VmGoalMessage.CreationDate))) { query = query.SortBy(x => x.CreationDate, isAscending); } else if (field.Is(nameof(VmGoalMessage.Text))) { query = query.SortBy(x => x.Text, isAscending); } else if (field.Is(nameof(VmGoalMessage.GoalTitle))) { query = query.SortBy(x => x.Goal.Title, isAscending); } else if (field.Is(nameof(VmGoalMessage.LastEditDate))) { query = query.SortBy(x => x.LastEditDate, isAscending); } else if (field.Is(nameof(VmGoalMessage.OwnerFullName))) { query = query .SortBy(x => x.Owner.LastName, isAscending) .SortBy(x => x.Owner.FirstName, isAscending) .SortBy(x => x.Owner.MiddleName, isAscending); } else if (field.Is(nameof(VmGoalMessage.IsRemoved))) { query = query.SortBy(x => x.IsRemoved, isAscending); } return(query); }
/// <summary> /// 获取排序规则 orderBy 模式 a desc,b asc /// </summary> /// <param name="orderBy"></param> /// <returns></returns> protected List <FieldSort> GetSortList(string orderBy) { var sorts = GetSortInfoArray(orderBy); List <FieldSort> sortFieldList = new List <FieldSort>(); foreach (var singleOrderInfo in sorts) { string[] singleField = GetSingleSortFieldAndOrderTypeArray(singleOrderInfo); string field = singleField[0]; string orderType = singleField[1]; SortOrder sortOrder = SortOrder.Descending; if (orderType.ToLower() == "asc") { sortOrder = SortOrder.Ascending; } FieldSort sortField = new FieldSort() { Field = field, Order = sortOrder }; sortFieldList.Add(sortField); } return(sortFieldList); }
public async Task GetPageSortByDateTest(int pageNumber, int pageSize, SortType sortType, int[] expectedIds) { //Arrange var sortField = new FieldSort(nameof(VmProject.CreationDate), sortType); var pageOptions = new PageOptions { PageNumber = pageNumber, PageSize = pageSize, SortFields = new[] { sortField } }; //Act var resultScopes = (await _service.GetPage(_currentUser, pageOptions)).ToArray(); //Assert Assert.LessOrEqual(expectedIds.Length, resultScopes.Length); for (var i = 0; i < expectedIds.Length; i++) { Assert.AreEqual(expectedIds[i], resultScopes[i].Id); } }
public async Task GetPageWithSortingTest(int pageNumber, int pageSize, string filter, string fieldName, SortType sortType, int[] expectedIds) { //Arrange var sortField = new FieldSort(fieldName, sortType); var pageOptions = new PageOptions { PageNumber = pageNumber, PageSize = pageSize, Filter = filter, SortFields = new[] { sortField } }; //Act var resultScopes = (await _service.GetPage(_currentUser, pageOptions)).ToArray(); //Assert Assert.LessOrEqual(expectedIds.Length, resultScopes.Length); for (var i = 0; i < expectedIds.Length; i++) { Assert.AreEqual(expectedIds[i], resultScopes[i].Id); } }
/// <summary> /// 比较 /// </summary> /// <param name="compare"></param> /// <param name="defaultsort"></param> /// <returns></returns> public Table Compare(string compare, FieldSort defaultsort) { this._ts.Compare(compare, defaultsort); return(this); }
public OrderingFilter(FieldSort sortBy, ResultOrderType orderBy) { FieldSort = sortBy; Order = orderBy; }
public async Task <IActionResult> List(FieldSort currentSort, int currentPage = 1, string sortBy = "") { if (TempData["ElementErrorShort"] != null || TempData["ElementErrorLong"] != null) { ModelState.AddModelError((string)TempData["ElementErrorShort"], (string)TempData["ElementErrorLong"] ?? (string)TempData["ElementErrorShort"]); } var units = await appContext.Units.ToListAsync(); var totalItems = units.Count; var model = new UnitViewModel { CurrentPage = currentPage, CurrentSort = currentSort, ItemOnPages = itemOnPages, TotalItems = totalItems, SortBy = sortBy }; #region Pagination if (String.IsNullOrEmpty(sortBy)) { switch (currentSort) { case FieldSort.shortNameAscend: model.Items = units.OrderBy(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); break; case FieldSort.shortNameDescend: model.Items = units.OrderByDescending(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); break; case FieldSort.fullNameAscend: model.Items = units.OrderBy(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); break; case FieldSort.descriptionDescend: model.Items = units.OrderByDescending(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); break; default: model.Items = units.OrderBy(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); break; } return(View(model)); } #endregion #region Sorting if (sortBy == "shortName") { if (currentSort == FieldSort.shortNameAscend) { model.CurrentSort = FieldSort.shortNameDescend; model.Items = units.OrderByDescending(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } else { model.CurrentSort = FieldSort.shortNameAscend; model.Items = units.OrderBy(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } } else if (sortBy == "fullName") { if (currentSort == FieldSort.fullNameAscend) { model.CurrentSort = FieldSort.fullNameDescend; model.Items = units.OrderByDescending(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } else { model.CurrentSort = FieldSort.fullNameAscend; model.Items = units.OrderBy(x => x.Name).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } } else { model.CurrentSort = FieldSort.shortNameAscend; model.Items = units.OrderBy(x => x.ShortName).Skip((currentPage - 1) * itemOnPages).Take(itemOnPages).ToList(); } #endregion return(View(model)); }
public abstract void Compare(string compare, FieldSort defaultsort);
/// <summary> /// 兼容模式 /// </summary> /// <param name="field"></param> /// <param name="sort"></param> public Table(string field, FieldSort sort) { this._ts = new DefaultTableStrategy(field, sort) { }; }