Files
mapo-core/MP.Stats/Components/ModalSearchMacc.razor.cs
T

153 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Components;
using MP.Data.DbModels;
using MP.Stats.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Components
{
public partial class ModalSearchMacc
{
#region Public Properties
[Parameter]
public List<AutocompleteModel> AllRecords { get; set; } = null!;
[Parameter]
public EventCallback<bool> EC_CloseSearch { get; set; }
[Parameter]
public EventCallback<List<string>> EC_ListUpdated { get; set; }
[Parameter]
public int MaxSelection { get; set; } = 6;
#endregion Public Properties
#region Protected Properties
protected string CssClear
{
get => currList == null || currList.Count == 0 ? "btn-outline-secondary" : "btn-primary";
}
protected string CssReset
{
get => string.IsNullOrEmpty(SearchVal) ? "btn-outline-secondary" : "btn-primary";
}
protected List<AutocompleteModel> ListRecords { get; set; } = null!;
protected List<AutocompleteModel> SearchRecords { get; set; } = null!;
protected string ListSelected
{
get => currList != null && currList.Count > 0 ? string.Join(", ", currList) : "-- nessuna selezione --";
}
protected int numSelected
{
get => currList.Count;
}
protected string SearchVal
{
get => _searchVal;
set
{
if (_searchVal != value)
{
_searchVal = value;
ReloadData();
}
}
}
#endregion Protected Properties
#region Protected Methods
protected void ClearSel()
{
currList.Clear();
}
protected override void OnParametersSet()
{
ReloadData();
}
protected void ReloadData()
{
// ricerca
SearchRecords = AllRecords
.Where(x => string.IsNullOrEmpty(SearchVal) || x.LabelField.Contains(SearchVal, System.StringComparison.InvariantCultureIgnoreCase))
.ToList();
totalCount = SearchRecords.Count;
// paginazione
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
protected void ResetSearch()
{
SearchVal = "";
ReloadData();
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
ReloadData();
}
protected void SetPage(int newNum)
{
currPage = newNum;
ReloadData();
}
protected async Task ToggleSel(string newVal)
{
if (!currList.Contains(newVal))
{
currList.Add(newVal);
}
else
{
currList.Remove(newVal);
}
// riordino
currList = currList.OrderBy(x => x).ToList();
#if false
// sollevo evento notifica
await EC_ListUpdated.InvokeAsync(currList);
#endif
}
#endregion Protected Methods
#region Private Fields
private string _searchVal = "";
private List<string> currList = new List<string>();
private int currPage = 1;
private int numRecord = 10;
private int totalCount = 0;
#endregion Private Fields
#region Private Methods
private async Task SearchMaccToggle()
{
await EC_ListUpdated.InvokeAsync(currList);
await EC_CloseSearch.InvokeAsync(true);
}
#endregion Private Methods
}
}