Forráskód Böngészése

Фильтры и сортировка

lyudmila.archackowa 9 hónapja
szülő
commit
e4978479a0

+ 1 - 1
AvaloniaApplicationBase/ViewModels/MainWindowViewModel.cs

@@ -21,7 +21,7 @@ namespace AvaloniaApplicationBase.ViewModels
             set => this.RaiseAndSetIfChanged(ref us, value); 
         }
 
-        ShowCatsPageViewModel showCatsVM = new ShowCatsPageViewModel(); // объект VM с кодом для просмотром информации о котах
+        ShowCatsPageViewModel showCatsVM = new ShowCatsPageViewModel(myConnection); // объект VM с кодом для просмотром информации о котах
         
         // свойсво для изменения выше созданной переменной
         public ShowCatsPageViewModel ShowCatsVM 

+ 90 - 1
AvaloniaApplicationBase/ViewModels/ShowCatsPageViewModel.cs

@@ -9,8 +9,97 @@ namespace AvaloniaApplicationBase.ViewModels
 {
 	public class ShowCatsPageViewModel : ReactiveObject
 	{
+        CatBaseContext myConnection;
+        private List<CatTable> ct;
+
+        public ShowCatsPageViewModel(CatBaseContext myConnection)
+        {
+            this.myConnection = myConnection;
+            CT = myConnection.CatTables.Include(x => x.GenderNavigation).Include(x => x.TraitCatTables).ThenInclude(x => x.IdTraitNavigation).ToList();
+
+        }
+
         // Include - äëÿ òàáëèö, íåïîñðåäñòâåííî ñâÿçàííûõ ñâÿçàííûõ äðóã ñ äðóãîì
         // ThenInclude - äëÿ ïîñëåäîâàòëåüíîãî ñîåäèíåíèÿ òàáëèö, èçíà÷àëüíî äðóã ñ äðóãîì íå ñâÿçàííûìè (ïîäðîáíåå â ôàéëå â moodle)
-        public List<CatTable> CT => MainWindowViewModel.myConnection.CatTables.Include(x => x.GenderNavigation).Include(x => x.TraitCatTables).ThenInclude(x => x.IdTraitNavigation).ToList();
+
+        public List<CatTable> CT { get => ct; set => this.RaiseAndSetIfChanged(ref ct,value); }
+
+        // ñîðòèðîâêà ïî èìåíè êîòà
+        public List<string> OrdersName => new List<string>() { "Ïî âîçðàñòàíèþ", "Ïî óáûâàíèþ" };
+
+        public string OrderByName 
+        { 
+            get => orderByName;
+            set
+            {
+                orderByName = value;
+                AllFilters();
+            }
+        }
+
+        string orderByName = "Ïî âîçðàñòàíèþ";
+
+
+        // ôèëüòðàöèÿ ïî ïîëó
+        public List<string> GenderList
+        {
+            get
+            {
+                List<string> genders = new List<string>() { "ëþáîé" };
+                genders.AddRange(myConnection.GenderTables.Select(x => x.Gender).ToList());
+                return genders;
+            }
+        }
+
+        public string SelectedGender 
+        { 
+            get => selectedGender;
+            set
+            {
+                selectedGender = value;
+                AllFilters();
+            }
+        }
+
+        
+
+        string selectedGender = "ëþáîé";
+
+        // ïîèñê ïî èìåíè
+        string nameFind;
+        public string NameFind
+        {
+            get => nameFind;
+            set
+            {
+                nameFind = value;
+                AllFilters();
+            }
+        }
+
+        void AllFilters()
+        {
+            CT = myConnection.CatTables.Include(x => x.GenderNavigation).Include(x => x.TraitCatTables).ThenInclude(x => x.IdTraitNavigation).ToList();
+            switch (orderByName)
+            {
+                case "Ïî âîçðàñòàíèþ":
+                    CT = CT.OrderBy(x=>x.Name).ToList();
+                    break;
+                case "Ïî óáûâàíèþ":
+                    CT = CT.OrderByDescending(x=>x.Name).ToList(); 
+                    break;
+            }
+
+            if (selectedGender!="ëþáîé")
+            {
+                CT = CT.Where(x=>x.GenderNavigation.Gender==selectedGender).ToList();
+            }
+
+            if (!string.IsNullOrWhiteSpace(nameFind))
+            {
+                CT = CT.Where(x=>x.Name.ToLower().Contains(nameFind.ToLower())).ToList();
+            }
+        }
+
     }
 }

+ 13 - 2
AvaloniaApplicationBase/Views/ShowCatsPage.axaml

@@ -8,8 +8,19 @@
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="AvaloniaApplicationBase.Views.ShowCatsPage">
 	
-	<Grid RowDefinitions="*,9*">
-		<Button Grid.Row="0" Command="{Binding NewCatPage}">Добавить кота</Button>
+	<Grid RowDefinitions="*,6*">
+		<StackPanel>
+			<Button Grid.Row="0" Command="{Binding NewCatPage}">Добавить кота</Button>
+			<StackPanel Orientation="Horizontal">
+				<TextBlock>сортировка по имени</TextBlock>
+				<ComboBox ItemsSource="{Binding ShowCatsVM.OrdersName}" SelectedValue="{Binding ShowCatsVM.OrderByName}"></ComboBox>
+				<TextBlock>Фильтр по полу</TextBlock>
+				<ComboBox ItemsSource="{Binding ShowCatsVM.GenderList}" SelectedValue="{Binding ShowCatsVM.SelectedGender}"></ComboBox>
+				<TextBlock>Поиск по имени</TextBlock>
+				<TextBox Text="{Binding ShowCatsVM.NameFind}"></TextBox>
+			</StackPanel>
+		</StackPanel>
+		
 		<ListBox ItemsSource="{Binding ShowCatsVM.CT}" Grid.Row="1">
 			<ListBox.ItemTemplate>
 				<DataTemplate>