using Blazorise.DataGrid; using ValblazeProject.Models; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using ValblazeProject.Services; using ValblazeProject.Components; using System.Collections.ObjectModel; using Microsoft.JSInterop; using System.Collections.Specialized; 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; } public List items { get; set; } = new List(); private int totalItem; // List inventaire sauvegarder public List Jitems; private DataGrid dataGrid; private string _searchText; private int totalSizeByPage = 5; private bool _trie = false; // Gestion logs public ObservableCollection Actions { get; set; } // Drag Item public Item CurrentDragItem { get; set; } /******************* 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 *******************/ /// /// Constructeur /// public Inventaire() { Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; string fileName = "Inventory.json"; string jsonString = File.ReadAllText(fileName); this.Jitems = JsonSerializer.Deserialize>(jsonString)!; } /// /// method that call the javascript /// /// /// private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); } /// /// 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(); Actions.Add(new InventoryAction { Action = "Sort by Name" }); } // Gestion pagination totalItem = items.Count; int pageCourante = dataGrid.CurrentPage; items = items.Skip((pageCourante - 1) * totalSizeByPage).Take(totalSizeByPage).ToList(); StateHasChanged(); } } } }