using Blazored.LocalStorage; using Blazored.Modal; using Blazored.Modal.Services; using Blazorise.DataGrid; using ValblazeProject.Models; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using ValblazeProject.Modals; using ValblazeProject.Services; using ValblazeProject.Components; using System.Collections.ObjectModel; using System.ComponentModel; using Microsoft.AspNetCore.Components.Forms; using System.Linq; using Microsoft.JSInterop; using System.Collections.Specialized; namespace ValblazeProject.Pages { public partial class Inventaire { /******************* Initialisation des attributs *******************/ private List items; private int totalItem; [Inject] public IStringLocalizer Localizer { get; set; } [Inject] public IDataService DataService { get; set; } [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } [Inject] public NavigationManager NavigationManager { get; set; } /// /// Gets or sets the java script runtime. /// [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } [CascadingParameter] public IModalService Modal { get; set; } private DataGrid dataGrid; private string _searchText; private int totalSizeByPage = 10; private bool _trie = false; public ObservableCollection Actions { get; set; } public Item CurrentDragItem { get; set; } public Inventaire() { Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; } private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("Inventaire.AddActions", e.NewItems); } // Drag internal void OnDragEnter() { } internal void OnDragStart() { } internal void OnDrop() { } internal void OnDragLeave() { } /******************* Attribut modifier *******************/ private string search { get { return _searchText; } set { if(_searchText != value ) { _searchText = value; SearchTextChange(); } } } private bool trieActive { get { return _trie; } set { if (_trie != value) { _trie = value; TrieChanged(); } } } /******************* Méthodes *******************/ /// /// Trie par ordre alphabétique /// /// public async Task TrieChanged() { await dataGrid.Reload(); StateHasChanged(); } /// /// Méthode de recherche /// /// public async Task SearchTextChange() { dataGrid.CurrentPage = 1; await dataGrid.Reload(); StateHasChanged(); } /// /// Initialisation des données via un service /// /// /// private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) { return; } if (!e.CancellationToken.IsCancellationRequested) { /* Il est important de découper le processus en 3 étapes: 1. le filtre 2. le tri 3. la pagination */ items = await DataService.List(); // Search if (!string.IsNullOrEmpty(_searchText)) { items = items.Where(i => i.DisplayName.Contains(_searchText)).ToList(); } // Trie if (_trie) { items = items.OrderBy(i => i.DisplayName).ToList(); } // Gestion pagination totalItem = items.Count; int pageCourante = dataGrid.CurrentPage; items = items.Skip((pageCourante - 1) * totalSizeByPage).Take(totalSizeByPage).ToList(); StateHasChanged(); } } /* private async void OnDelete(int id) { var parameters = new ModalParameters(); parameters.Add(nameof(Item.Id), id); var modal = Modal.Show("Delete Confirmation", parameters); var result = await modal.Result; if (result.Cancelled) { return; } await DataService.Delete(id); // Reload the page NavigationManager.NavigateTo("list", true); }*/ } }