* Fixed Focus issues in Grid

This commit is contained in:
2025-07-03 07:21:26 +02:00
parent 82cb7a3dbe
commit 4cfb422ad4
2 changed files with 140 additions and 55 deletions

View File

@@ -37,7 +37,7 @@
CssClass="e-outline"/>
<label for="textBox" class="form-label">Zeskanowana wartość:</label>
<SfTextBox ID="scannedValue" ValueChange="ScanValue" @bind-Value="ScannedValue"
CssClass="e-outline" @ref="_scanner" />
CssClass="e-outline" @ref="_scanner"/>
</CardContent>
</SfCard>
<SfGrid @ref="_grid"
@@ -53,7 +53,8 @@
<ToolbarItem Type="ItemType.Button" Text="Zapisz zmiany" Id="SaveButton"
PrefixIcon="e-icons e-save" OnClick="SaveChanges"/>
<ToolbarItem Type="ItemType.Button" Text="Podziel Linię" Id="SplitLineButton"
PrefixIcon="e-icons e-split-horizontal" OnClick="ShowSplitDialog" Disabled="IsDisabled"/>
PrefixIcon="e-icons e-split-horizontal" OnClick="ShowSplitDialog"
Disabled="IsDisabled"/>
<ToolbarItem Type="ItemType.Button" Id="Generuj XLS i Wyślij" PrefixIcon="e-icons e-export-xls"
Text="Generuj XLS i Wyślij" OnClick="ExportXls"/>
</ToolbarItems>
@@ -72,7 +73,8 @@
HeaderText="Ilość w Dostawie" Width="80"></GridColumn>
<GridColumn Field=@nameof(WzRowMeyleDto.PalletNumber) AllowEditing="true"
TextAlign="TextAlign.Center" HeaderText="Nr Palety" Width="100"></GridColumn>
<GridColumn Field=@nameof(WzRowMeyleDto.PartNumberSl) AllowEditing="true" TextAlign="TextAlign.Center"
<GridColumn Field=@nameof(WzRowMeyleDto.PartNumberSl) AllowEditing="true"
TextAlign="TextAlign.Center"
HeaderText="Nr Partii SL" Width="80"></GridColumn>
<GridColumn Field=@nameof(WzRowMeyleDto.PartNumber) AllowEditing="true" TextAlign="TextAlign.Center"
HeaderText="Nr Partii Meyle" Width="80"></GridColumn>
@@ -114,15 +116,17 @@
<DialogButton Content="OK" IsPrimary="true" OnClick="@HideModal"/>
</DialogButtons>
</SfDialog>
<SfDialog Width="500px" Title="Błąd" IsModal="true" @bind-Visible="VisibilityValidation" AllowPrerender="true">
<DialogTemplates>
<Content>
<p>Błąd skanowania! Wystąpił jeden z wyjątków (Zeskanowana wartość '<b>@ScannedValue</b>'):</p>
<ul>
<li><p>Zeskanowano niepoprawny Numer Partii SL (nieistniejący w tabeli)</p></li>
<li><p>Zeskanowano niepoprawny numer Partii Meyle (niezaczynający się od <b>@($"{DateTime.Now.Year - 2000}X")</b>)</p></li>
<li><p>Numer Palety nie jest większy niż 0 (aktualnie wybrany numer palety: '<b>@PalletNumber</b>')</p></li>
<li><p>Zeskanowano niepoprawny numer Partii Meyle (niezaczynający się od
<b>@($"{DateTime.Now.Year - 2000}X")</b>)</p></li>
<li><p>Numer Palety nie jest większy niż 0 (aktualnie wybrany numer palety:
'<b>@PalletNumber</b>')</p></li>
</ul>
</Content>
</DialogTemplates>
@@ -131,10 +135,24 @@
</DialogButtons>
</SfDialog>
<SfDialog Width="500px" Title="Podziel Linię" IsModal="true" @bind-Visible="VisibilityLineSplitter" AllowPrerender="true">
<SfDialog Width="500px" Title="Błąd" IsModal="true" @bind-Visible="VisibilityPalletNumber"
AllowPrerender="true">
<DialogTemplates>
<Content>
<label for="textBox" class="form-label">Podziel linię <b>@SelectedRow?.FaIndex</b> podając ilość sztuk dla nowej linii:</label>
<p>Błąd skanowania! <b>Wybierz NUMER PALETY większy niż 0</b> (Aktualnie '@PalletNumber'):</p>
</Content>
</DialogTemplates>
<DialogButtons>
<DialogButton Content="OK" IsPrimary="true" OnClick="@HideModal"/>
</DialogButtons>
</SfDialog>
<SfDialog Width="500px" Title="Podziel Linię" IsModal="true" @bind-Visible="VisibilityLineSplitter"
AllowPrerender="true">
<DialogTemplates>
<Content>
<label for="textBox" class="form-label">Podziel linię <b>@SelectedRow?.FaIndex</b> podając ilość
sztuk dla nowej linii:</label>
<SfTextBox ID="newQuantity" Type="InputType.Number" @bind-Value="@NewQuantity"
CssClass="e-outline"/>
</Content>
@@ -165,13 +183,14 @@
private List<WzRowMeyleDto> SelectedRows { get; set; } = new();
private string WzNumber { get; set; } = string.Empty;
private bool _isValid;
private bool Visibility { get; set; }
private bool VisibilityValidation { get; set; }
private bool VisibilityLineSplitter { get; set; }
public bool VisibilityPalletNumber { get; set; }
private string? EmailAddresses { get; set; } = string.Empty;
private string PalletNumber { get; set; } = "0";
@@ -187,6 +206,7 @@
Visibility = false;
VisibilityValidation = false;
VisibilityLineSplitter = false;
VisibilityPalletNumber = false;
LastScannedValue = ScannedValue;
ScannedValue = string.Empty;
@@ -200,9 +220,9 @@
{
_wzHeader = await WarehouseService.GetWzHeaderByIdAsync(WzHeader);
WzRowsMeyle = (await WarehouseService.GetWzRowsByWzHeaderId(WzHeader)).ToList();
TransactionModelsByPartNumber = await GetTransactionModels();
EmailAddresses = _wzHeader.EmailAddresses;
WzNumber = _wzHeader.WzNumbers ?? string.Empty;
@@ -213,7 +233,7 @@
}
private async Task SaveChanges()
{
{
if (!string.IsNullOrWhiteSpace(EmailAddresses))
{
await WarehouseService.AddEmailsToWzHeaderAsync(WzHeader, EmailAddresses);
@@ -258,21 +278,21 @@
if (string.IsNullOrWhiteSpace(obj.Value)) return;
if (int.Parse(PalletNumber) <= 0)
{
ShowValidationMessage();
VisibilityPalletNumber = true;
return;
}
ScannedValue = obj.Value.Trim();
StateHasChanged();
TransactionModelsByPartNumber.TryGetValue(obj.Value.Trim(), out List<TransactionModel>? materialTransactionsByPartNumber);
TransactionModel? materialTransactionByPartNumber = materialTransactionsByPartNumber?.FirstOrDefault();
if (materialTransactionByPartNumber == null && IsValidScannedValue(obj.Value))
{
FillMeylePartNumber(ScannedValue);
LastScannedValue = ScannedValue;
ScannedValue = string.Empty;
@@ -290,7 +310,7 @@
ShowValidationMessage();
return;
}
LastScannedValue = ScannedValue;
ScannedValue = string.Empty;
@@ -306,13 +326,12 @@
return scannedValue.StartsWith(format);
}
private void FillMeylePartNumber(string scannedValue)
{
if (SelectedRow != null)
private async Task FillMeylePartNumber(string scannedValue)
{
var rowIndex = WzRowsMeyle.FindIndex(x => x.FaIndex == SelectedRow?.FaIndex && x.Quantity == SelectedRow?.Quantity);
if (SelectedRow != null && !SelectedRows.Any())
{
var rowIndex = WzRowsMeyle.FindIndex(x => x.FaIndex == SelectedRow?.ItemNumber && x.Quantity == SelectedRow?.Quantity);
FocusGridRow(rowIndex);
SelectedRow.PartNumber = scannedValue;
ChangedRecords.Add(SelectedRow);
}
@@ -323,65 +342,92 @@
ChangedRecords.Add(wzRowMeyleDto);
}
SaveChanges();
InvokeAsync(StateHasChanged);
_grid.Refresh();
_scanner.FocusAsync();
await SaveChanges();
ChangedRecords.Clear();
SelectedRows.Clear();
FocusGridRow(rowIndex);
await _scanner.FocusAsync();
}
private void FillFaPartNumberAndPalletNumber(TransactionModel materialTransactionByPartNumber, string scannedValue)
private async Task FillFaPartNumberAndPalletNumber(TransactionModel materialTransactionByPartNumber, string scannedValue)
{
var rowIndex = WzRowsMeyle.FindIndex(x => x.FaIndex == materialTransactionByPartNumber.ItemNumber && x.Quantity == materialTransactionByPartNumber.Quantity);
await _grid.ClearSelectionAsync();
await _grid.Refresh();
await InvokeAsync(StateHasChanged);
List<int> selectedIndices = new List<int>();
int palletNumber = int.Parse(PalletNumber);
int rowIndex = WzRowsMeyle.FindIndex(x => x.FaIndex == materialTransactionByPartNumber.ItemNumber && x.Quantity == materialTransactionByPartNumber.Quantity);
switch (rowIndex)
{
case -1 when int.Parse(PalletNumber) <= 0:
ShowValidationMessage();
LastScannedValue = ScannedValue;
ScannedValue = string.Empty;
return;
case -1:
{
SelectedRows = WzRowsMeyle.Where(x => x.FaIndex == materialTransactionByPartNumber.ItemNumber).ToList();
if (SelectedRows.Count == 0)
{
ShowValidationMessage();
return;
}
foreach (WzRowMeyleDto wzRowMeyleDto in SelectedRows)
rowIndex = WzRowsMeyle.FindIndex(x => x.FaIndex == SelectedRows.First().FaIndex && x.Quantity == SelectedRows.First().Quantity);
var validCombinations = FindCombinations(SelectedRows, (int?)materialTransactionByPartNumber.Quantity ?? 0);
foreach (var combination in validCombinations)
{
wzRowMeyleDto.PartNumberSl = scannedValue;
ChangedRecords.Add(wzRowMeyleDto);
foreach (var record in combination)
{
record.PartNumberSl = scannedValue;
ChangedRecords.Add(record);
int index = WzRowsMeyle.IndexOf(record);
if (index >= 0)
{
selectedIndices.Add(index);
Console.WriteLine($"Dodano indeks: {index}"); // Debugowanie
}
}
}
SelectedRows.Clear();
SelectedRows.AddRange(ChangedRecords);
break;
}
default:
{
FocusGridRow(rowIndex);
SelectedRow = WzRowsMeyle[rowIndex];
selectedIndices.Add(rowIndex);
SelectedRow.PartNumberSl = ScannedValue;
SelectedRow.PalletNumber = int.Parse(PalletNumber);
SelectedRow.PartNumberSl = scannedValue;
SelectedRow.PalletNumber = palletNumber;
if (ChangedRecords.All(x => x.TransactionNumber != SelectedRow.TransactionNumber))
{
ChangedRecords.Add(SelectedRow);
}
break;
}
}
SaveChanges();
InvokeAsync(StateHasChanged);
_grid.Refresh();
_scanner.FocusAsync();
await SaveChanges();
ChangedRecords.Clear();
if (selectedIndices.Any())
{
await _grid.SelectRowsAsync(selectedIndices.ToArray());
}
await _grid.Refresh();
await InvokeAsync(StateHasChanged);
FocusGridRow(rowIndex);
await _scanner.FocusAsync();
}
private void FocusGridRow(int rowIndex)
@@ -391,10 +437,10 @@
int pageSize = _grid.PageSettings.PageSize;
int targetPage = (rowIndex / pageSize) + 1;
_grid.GoToPageAsync(targetPage);
rowIndex %= pageSize;
}
_grid.SelectRowAsync(rowIndex);
_grid.ScrollIntoViewAsync(rowIndex: rowIndex);
_grid.FocusAsync();
@@ -438,18 +484,20 @@
OrderNumber = SelectedRow.OrderNumber,
PalletNumber = SelectedRow.PalletNumber,
WzNumber = SelectedRow.WzNumber,
TransactionNumber = SelectedRow.TransactionNumber += 10000
TransactionNumber = SelectedRow.TransactionNumber += 10000,
PartNumberSl = SelectedRow.PartNumberSl,
PartNumber = SelectedRow.PartNumber
};
WzRowsMeyle.Add(splitRow);
SelectedRow.Quantity -= newQuantity;
ChangedRecords.Add(SelectedRow);
await WarehouseService.CreateWzRowsMeyleAsync(new List<WzRowMeyleDto> { splitRow });
await UpdateRows(ChangedRecords);
}
}
VisibilityLineSplitter = false;
}
@@ -464,4 +512,32 @@
SelectedRow = obj.Data;
}
private List<List<WzRowMeyleDto>> FindCombinations(List<WzRowMeyleDto> records, int targetSum)
{
var result = new List<List<WzRowMeyleDto>>();
var currentCombination = new List<WzRowMeyleDto>();
void Backtrack(int start, int currentSum)
{
if (currentSum == targetSum)
{
result.Add(new List<WzRowMeyleDto>(currentCombination));
return;
}
for (int i = start; i < records.Count; i++)
{
if (currentSum + records[i].Quantity <= targetSum)
{
currentCombination.Add(records[i]);
Backtrack(i + 1, currentSum + records[i].Quantity ?? 0);
currentCombination.RemoveAt(currentCombination.Count - 1);
}
}
}
Backtrack(0, 0);
return result;
}
}