首页 > 解决方案 > 选择并从不同的复选框中获取多个数据

问题描述

我需要一些关于我的复选框和过滤项目的帮助。

我有 5 个复选框,每个复选框过滤不同的值。1 将根据类别将所有内容归还给我,其余的将归还给我。现在我需要用户能够选择多个类别。我能够获取每个类别的数据,但我不知道如何获取多个类别的数据。一旦用户完成了他的选择,他就会按下一个按钮,该按钮应该过滤项目并重定向到一个页面。

private static int? _userDiscount;
        private static int _parentCategoryId;
        private static List<Article> _allArticlesForPurchase;
        private static List<Category> _allCategories;
        private static List<CategoryGroup> _allUserCategoryGroups;
        private static List<Article> _allUserArticles;
        private static UserOrganization _userOrganization;
        private bool _filterAllItems;
        private bool _filterBeginnerItems;
        private bool _filterIntermediateItems;
        private bool _filterAdvancedItems;
        private bool _filterUpperIntermediateItems;
        public ICommand FilterItemsCommand { get; private set; }
        public bool IsBusy { get; set; }
        public bool FilterAllItems
        {
            set
            {

                _filterAllItems = value;
                NotifyPropertyChanged();

                if (_filterAllItems)

                    _parentCategoryId = -1;

                FillArticles();
                if (FilterAllItems == true)
                {
                    FilterBeginnerItems = false;
                    FilterIntermediateItems = false;
                    FilterUpperIntermediateItems = false;
                    FilterAdvancedItems = false;
                }

                //var result = FilterArticlesForPurchase.Where(x => x.ArticleCategoryId == _parentCategoryId).ToList();


            }
            get => _filterAllItems;
        }

        public bool FilterBeginnerItems
        {
            set
            {
                _filterBeginnerItems = value;
                NotifyPropertyChanged();

                if (_filterBeginnerItems)
                {
                    _parentCategoryId = 1;
                    FilterAllItems = false;
                    FillArticles();


                }

            }
            get => _filterBeginnerItems;
        }

        public bool FilterIntermediateItems
        {
            set
            {
                _filterIntermediateItems = value;
                NotifyPropertyChanged();

                if (_filterIntermediateItems)
                {
                    _parentCategoryId = 2;
                    FilterAllItems = false;
                    FillArticles();


                }

            }
            get => _filterIntermediateItems;
        }
        public bool FilterUpperIntermediateItems
        {
            set
            {
                _filterUpperIntermediateItems = value;
                NotifyPropertyChanged();

                if (_filterUpperIntermediateItems)
                {
                    _parentCategoryId = 3;
                    FilterAllItems = false;
                    FillArticles();


                }

            }
            get => _filterUpperIntermediateItems;
        }

        public bool FilterAdvancedItems
        {
            set
            {
                _filterAdvancedItems = value;
                NotifyPropertyChanged();

                if (_filterAdvancedItems)
                {
                    _parentCategoryId = 4;
                    FilterAllItems = false;
                    FillArticles();



                }

            }
            get => _filterAdvancedItems;
        }


        public ObservableCollection<ArticleDetailData> FilterArticlesForPurchase { get; } = new ObservableCollection<ArticleDetailData>();
        public FilterArticlesForPurchaseViewModel()
            : base()
        {
            ;

            Task.Run(async () => await LoadAllDataForArticlesAndCategories()).Wait();
            FilterItemsCommand = new Command(async () => await FilterItems());
        }

        private async Task LoadAllDataForArticlesAndCategories()
        {
            if (LangUpNetworkDelegate.IsOnline)
            {
                await FillCategories();
                if (LangUpLoggedUser.LoggedIn)
                {
                    await FillUsersArticles();

                    var userOrganizationApiResponse = await AVAT.App.ApiFactory.TryGetMainUserOrganization();
                    _userOrganization = userOrganizationApiResponse.MatchResult<UserOrganization>(default(UserOrganization), org => org);

                    if (_userOrganization != null &&
                        DateTime.ParseExact(_userOrganization.OrganizationPromocode.DateOfExpiration, "dd/MM/yyyy",
                            null) >= DateTime.Now)
                    {
                        _userDiscount = _userOrganization.OrganizationPromocode.DiscountPercentage;
                    }
                }
                else
                {
                    await FillAnonymousArticles();
                }
                foreach (var group in _allUserCategoryGroups)
                {
                    var listOfArticles = new List<Article>();
                    var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == group.Id).ToList();
                    foreach (var category in allGroupCategories)
                    {
                        var categoryArticles = _allUserArticles.Where(m => m.CategoryId == category.Id).ToList();
                        listOfArticles.AddRange(categoryArticles);
                    }
                    group.GroupArticles = listOfArticles;
                }
             FillArticles();
            }
            else
            {

                NotificationService.ShowToast("");
            }


        }
        private async void FillArticles()
        {
            var allArticles = new List<Article>();
            allArticles = FindAllArticlesForPurchase(allArticles);
            var downloadedArticles = LangUpDataSaverLoader.DeserializeAllOptimizationData();

            foreach (var article in allArticles)
            {
                    var filename = string.Format(SharedConstants.ArticleImageUrl, SharedConstants.ApiBaseUri, article.Id);
                    var rating = article.FinalRate == -1 ? "-.-" : ((float)article.FinalRate).ToString("0.0");
                    if (downloadedArticles.DownloadedArticles.Any(m => m.Id == article.Id))
                    {
                        filename = article.Id.ArticleImageFile();
                    }
                    var detail = new ArticleDetailData()
                    {
                        Author = article.Author,
                        Avatar = filename,
                        BackgroundImage = filename,
                        Description = article.Description,
                        Body = article.Description,
                        Section = article.Category,
                        Id = article.Id,
                        Subtitle = article.Description,
                        Title = article.NameCz,
                    }
                    FilterArticlesForPurchase.Add(detail);
                }
        }
        private static List<Article> FindAllArticlesForPurchase(List<Article> allArticles)
        {
            if (_parentCategoryId != -1)
            {
                foreach (var categoryGroup in _allUserCategoryGroups)
                {
                    var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == categoryGroup.Id).ToList();

                    if (_parentCategoryId != -1)
                    {
                        foreach (var category in allGroupCategories)
                        {
                            if (category.ParentId == _parentCategoryId && _parentCategoryId != -1)
                            {
                                var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id).ToList();
                                allArticles.AddRange(categoryArticles);
                            }
                        }
                    }
                }
            }
            else
            {
                allArticles = _allArticlesForPurchase;
            }

            return allArticles;
        }


        private async Task FilterItems()
        {

           await Application.Current.MainPage.Navigation.PushAsync(new ArticlesForPurchaseFiltered());
        }

更新

public void GetUserDecision()
        {
            List<int> userDecision = new List<int>();
            if (FilterAllItems) userDecision.Add(_parentCategoryId = -1);
            if (FilterBeginnerItems) userDecision.Add(_parentCategoryId = 1);
            if (FilterIntermediateItems) userDecision.Add(_parentCategoryId = 2);
            if (FilterUpperIntermediateItems) userDecision.Add(_parentCategoryId = 3);
            if (FilterAdvancedItems) userDecision.Add(_parentCategoryId = 4);
            UserDecisionResult = userDecision;


        }


        private static List<Article> FindAllArticlesForPurchase(List<Article> allArticles)
        {
            var result = UserDecisionResult;

            if (_parentCategoryId != -1)
            {
                foreach (var categoryGroup in _allUserCategoryGroups)
                {
                    var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == categoryGroup.Id).ToList();

                    if (_parentCategoryId != -1)
                    {
                        foreach (var category in allGroupCategories)
                        {
                            if (category.ParentId == _parentCategoryId && _parentCategoryId != -1)
                            {

                                //var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id).ToList();
                                var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id && result.Contains(m.CategoryId)).ToList(); 
                                allArticles.AddRange(categoryArticles);
                            }
                        } 
                    }
                }
            }
            else
            {
                allArticles = _allArticlesForPurchase;
            }

            return allArticles;
        }

标签: xamarin.forms

解决方案


您可以使用 LINQ 按多项选择进行过滤

// these are the ids selected by the user
var list = new List<int>() { 1, 2, 3 };

// this will return all articles whose CategoryID is in list
var result = FilterArticlesForPurchase.Where(list.Contains(x.ArticleCategoryId).ToList();

推荐阅读