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; using Microsoft.AspNetCore.Components.Web; using System.Text.Json; namespace ValblazeProject.Pages { public partial class Inventaire { /******************* Initialisation des attributs *******************/ [Inject] public IStringLocalizer Localizer { get; set; } [Inject] public IDataService DataService { get; set; } /// /// Gets or sets the java script runtime. /// [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } private List items; private int totalItem; public List Jitems; private DataGrid dataGrid; private string _searchText; private int totalSizeByPage = 5; private bool _trie = false; // Gestion logs public ObservableCollection Actions { get; set; } public Item CurrentDragItem { get; set; } public Inventaire() { Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; string fileName = "Inventory.json"; string jsonString = File.ReadAllText(fileName); this.Jitems = JsonSerializer.Deserialize>(jsonString)!; } private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("Inventaire.AddActions", e.NewItems); } // Drag protected override async Task OnAfterRenderAsync(bool firstRender) { base.OnAfterRenderAsync(firstRender); if (!firstRender) { return; } StateHasChanged(); } /******************* 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) { 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(); } } } }