/// <summary> /// 调用 OnQuery 回调方法获得数据源 /// </summary> protected async Task QueryData() { // 目前设计使用 Items 参数后不回调 OnQueryAsync 方法 if (Items == null) { if (OnQueryAsync == null && DynamicContext != null && typeof(TItem).IsAssignableTo(typeof(IDynamicObject))) { // 动态数据 SelectedRows.Clear(); QueryItems = DynamicContext.GetItems().Cast <TItem>(); if (DynamicContext.OnGetSelectedRows != null) { SelectedRows.AddRange(DynamicContext.OnGetSelectedRows().Cast <TItem>()); } TotalCount = QueryItems.Count(); } else { // 数据集合 await OnQuery(); } } else { RowsCache = null; } async Task OnQuery() { QueryData <TItem>?queryData = null; var queryOption = new QueryPageOptions() { IsPage = IsPagination, PageIndex = PageIndex, PageItems = PageItems, SearchText = SearchText, SortOrder = SortOrder, SortName = SortName, SortList = string.IsNullOrEmpty(SortString) ? null : new List <string>(SortString.Split(",", StringSplitOptions.RemoveEmptyEntries)), Filters = Filters.Values, Searchs = GetSearchs(), AdvanceSearchs = GetAdvanceSearchs(), CustomerSearchs = GetCustomerSearchs(), SearchModel = SearchModel, StartIndex = StartIndex }; if (CustomerSearchModel != null) { queryOption.SearchModel = CustomerSearchModel; } queryData = await InternalOnQueryAsync(queryOption); RowsCache = null; Items = null; QueryItems = queryData.Items; TotalCount = queryData.TotalCount; IsAdvanceSearch = queryData.IsAdvanceSearch; // 处理选中行逻辑 ProcessSelectedRows(); // 分页情况下内部不做处理防止页码错乱 if (!queryOption.IsPage) { ProcessPageData(queryData, queryOption); } if (IsTree) { await ProcessTreeData(); } void ProcessSelectedRows() { // 判断模型是否有 [Key] Id 等可识别字段尝试重构 var rows = new List <TItem>(); if (HasKeyAttribute) { // 更新选中行逻辑 foreach (var item in SelectedRows) { var key = Utility.GetKeyValue <TItem, object>(item); if (key != null) { var row = QueryItems.FirstOrDefault(i => Utility.GetKeyValue <TItem, object>(i).ToString() == key.ToString()); if (row != null) { rows.Add(row); } } } } else { foreach (var row in SelectedRows) { if (QueryItems.Any(i => i == row)) { rows.Add(row); } } } SelectedRows = rows; } void ProcessPageData(QueryData <TItem> queryData, QueryPageOptions queryOption) { var filtered = queryData.IsFiltered; var sorted = queryData.IsSorted; var searched = queryData.IsSearch; // 外部未处理 SearchText 模糊查询 if (!searched && queryOption.Searchs.Any()) { QueryItems = QueryItems.Where(queryOption.Searchs.GetFilterFunc <TItem>(FilterLogic.Or)); TotalCount = QueryItems.Count(); } // 外部未处理自定义高级搜索 内部进行高级自定义搜索过滤 if (!IsAdvanceSearch && queryOption.CustomerSearchs.Any()) { QueryItems = QueryItems.Where(queryOption.CustomerSearchs.GetFilterFunc <TItem>()); TotalCount = QueryItems.Count(); IsAdvanceSearch = true; } // 外部未过滤,内部自行过滤 if (!filtered && queryOption.Filters.Any()) { QueryItems = QueryItems.Where(queryOption.Filters.GetFilterFunc <TItem>()); TotalCount = QueryItems.Count(); } // 外部未处理排序,内部自行排序 // 先处理列头排序 再处理默认多列排序 if (!sorted) { if (queryOption.SortOrder != SortOrder.Unset && !string.IsNullOrEmpty(queryOption.SortName)) { var invoker = Utility.GetSortFunc <TItem>(); QueryItems = invoker(QueryItems, queryOption.SortName, queryOption.SortOrder); } else if (queryOption.SortList != null && queryOption.SortList.Any()) { var invoker = Utility.GetSortListFunc <TItem>(); QueryItems = invoker(QueryItems, queryOption.SortList); } } } async Task ProcessTreeData() { KeySet.Clear(); if (HasKeyAttribute) { CheckExpandKeys(TreeRows); } if (KeySet.Count > 0) { TreeRows = new List <TableTreeNode <TItem> >(); foreach (var item in QueryItems) { var node = new TableTreeNode <TItem>(item) { HasChildren = CheckTreeChildren(item), }; node.IsExpand = IsExpandRow(node); if (node.IsExpand) { await RestoreIsExpand(node); } TreeRows.Add(node); } } else { TreeRows = QueryItems.Select(item => new TableTreeNode <TItem>(item) { HasChildren = CheckTreeChildren(item) }).ToList(); } } } }