@code { private static readonly object _objectLock = new object(); protected List EditItems { get; set; } = GenerateItems(); protected Task> OnEditQueryAsync(QueryPageOptions options) => BindItemQueryAsync(EditItems, options); protected Task OnAddAsync() { return Task.FromResult(new BindItem() { DateTime = DateTime.Now }); } protected Task OnSaveAsync(BindItem item) { // 增加数据演示代码 if (item.Id == 0) { lock (_objectLock) { item.Id = EditItems.Max(i => i.Id) + 1; EditItems.Add(item); } } else { var oldItem = EditItems.FirstOrDefault(i => i.Id == item.Id); oldItem.Name = item.Name; oldItem.Address = item.Address; oldItem.DateTime = item.DateTime; oldItem.Count = item.Count; oldItem.Complete = item.Complete; oldItem.Education = item.Education; } return Task.FromResult(true); } protected Task OnDeleteAsync(IEnumerable items) { items.ToList().ForEach(i => EditItems.Remove(i)); return Task.FromResult(true); } protected Task> BindItemQueryAsync(IEnumerable items, QueryPageOptions options) { //TODO: 此处代码后期精简 if (!string.IsNullOrEmpty(SearchModel.Name)) items = items.Where(item => item.Name?.Contains(SearchModel.Name, StringComparison.OrdinalIgnoreCase) ?? false); if (!string.IsNullOrEmpty(SearchModel.Address)) items = items.Where(item => item.Address?.Contains(SearchModel.Address, StringComparison.OrdinalIgnoreCase) ?? false); if (!string.IsNullOrEmpty(options.SearchText)) items = items.Where(item => (item.Name?.Contains(options.SearchText) ?? false) || (item.Address?.Contains(options.SearchText) ?? false)); // 过滤 var isFiltered = false; if (options.Filters.Any()) { items = items.Where(options.Filters.GetFilterFunc()); // 通知内部已经过滤数据了 isFiltered = true; } // 排序 var isSorted = false; if (!string.IsNullOrEmpty(options.SortName)) { // 外部未进行排序，内部自动进行排序处理 var invoker = SortLambdaCache.GetOrAdd(typeof(BindItem), key => items.GetSortLambda().Compile()); items = invoker(items, options.SortName, options.SortOrder); // 通知内部已经过滤数据了 isSorted = true; } // 设置记录总数 var total = items.Count(); // 内存分页 items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList(); return Task.FromResult(new QueryData() { Items = items, TotalCount = total, IsSorted = isSorted, IsFiltered = isFiltered, IsSearch = !string.IsNullOrEmpty(SearchModel.Name) || !string.IsNullOrEmpty(SearchModel.Address) }); } }