Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Child component Blazor keeping data alive after rerender

$
0
0

Hello i am developing a Maui Blazor App but is just behave as blazor using net 8

I have several nested components, when i fecth data i got the initial payload, then i can modify this data delete or adding more data, the problem occurs when i try to reset to the initial payload clicking search bottom again without committing changes to the data. the modified data still remains, i would like the data resets to the inital payload

Here is the code of the most top component

@page "/request-management"<Preload LoadingText="Ejecutando..." /><div class="title"><h5>Manejar Pedidos</h5></div><RequestSearch @ref="_requestSearch" OnRequestUserScanSearch="OnRequestUserScanSearch" OnRequestSearch="OnRequestSearch" />@if(_dataRequestSearches is not null && _dataRequestSearches.Count > 0){    foreach(var request in _dataRequestSearches)    {<RequestView @key="request.IDPED" @ref="_requestView" request="request" _users="_users" OnRequestReload="OnRequestReload"/>    }}@code {    [Inject] protected ToastService ToastService { get; set; }    [Inject] protected PreloadService PreloadService { get; set; }    private RequestSearch _requestSearch { get; set; }    private List<DataRequestSearch> _dataRequestSearches = new List<DataRequestSearch>();    public List<DataUser> _users { get; set; }    private RequestView _requestView { get; set; }    private void OnRequestUserScanSearch(List<DataUser> data)    {        _users = data;    }    private async Task OnRequestSearch(List<DataRequestSearch> data)    {        _dataRequestSearches.Clear();        _dataRequestSearches.AddRange(data);        StateHasChanged();    }    private async Task OnRequestReload()    {        await _requestSearch.Search();    }}

RequestView.razor

@inject IProductService ProductService@inject IRequestService RequestService<Preload LoadingText="Ejecutando..." /><ConfirmDialog @ref="dialog" /><div class="request-container"><div class="request-id"><p># @request.IDPED</p></div><div class="request-status"><p><strong>Estatus:&nbsp;</strong></p><div class="status"><p>@GetStatusName(request.ORSTS)</p></div></div><div class="request-center"><div><p><strong>Desde:</strong> @request.DesdeCentro.WERKS - @request.DesdeCentro.NAME1</p></div><div><p><strong>Hasta:</strong> @request.HaciaCentro.WERKS - @request.HaciaCentro.NAME1</p></div></div>    @if (request.PedidoTiendaItems is not null && request.PedidoTiendaItems.Count > 0)    {<RequestItems @ref="_requestItems" request="request" _users="_users"             OnRequestItemStoreChangeLoteAdd="OnRequestItemStoreChangeLoteAdd"            OnRequestItemStoreChangeLoteRemove="OnRequestItemStoreChangeLoteRemove"            OnRequestItemStoreChangeSinLoteAdd="OnRequestItemStoreChangeSinLoteAdd"            OnRequestItemStoreChangeSinLoteRemove = "OnRequestItemStoreChangeSinLoteRemove" />    }<div class="request-date"><div><p><strong>Fecha Creacion:</strong> @request.DATEC</p></div><div><p><strong>Fecha Actualizacion:</strong> @request.DATEC</p></div></div><div class="actions"><button @onclick="() => OnShowModalClick()">Confirmar @(_requestItemsStoreLote.ITEMS.Count + _requestItemsStoreSinLote.ITEMS.Count)</button>        @if(request.ORSTS == (int)RequestStatusEnum.Status.PENDIENTE)        {<button style="margin-left: 10px;" @onclick="() => RequestCancel(request.IDPED)">Anular</button>                   }</div></div><Modal @ref="modal" title="Resumen de Materiales a Confirmar" Size="ModalSize.Large"><BodyTemplate>        @if ((_requestItemsStoreLote is null || _requestItemsStoreLote.ITEMS.Count == 0) && (_requestItemsStoreSinLote is null || _requestItemsStoreSinLote.ITEMS.Count == 0))        {<p>"Error debe seleccionar items a confirmar."</p>        }        else        {            @foreach (var item in _requestItemsStoreLote.ITEMS)            {<p><strong>Pedido Id: </strong>@_requestItemsStoreLote.IDPED, <strong>Material: </strong>@item.MATNR, <strong>Cantidad: </strong>@item.CANTP, <strong>Usuario: </strong>@item.UASIG </p>            }            @foreach (var item in _requestItemsStoreSinLote.ITEMS)            {<p><strong>Pedido Id: </strong>@_requestItemsStoreSinLote.IDPED, <strong>Material: </strong>@item.MATNR, <strong>Cantidad: </strong>@item.CANTP, <strong>Usuario: </strong>@item.UASIG </p>            }        }</BodyTemplate><FooterTemplate><Button Color="ButtonColor.Dark" @onclick="OnHideModalClick">Cerrar</Button><Button Color="ButtonColor.Dark" @onclick="InsertItems">Confirmar</Button></FooterTemplate></Modal>@code {    [Inject] protected ToastService ToastService { get; set; }    [Inject] protected PreloadService PreloadService { get; set; }    [Parameter]    public DataRequestSearch request { get; set; }    [Parameter]    public List<DataUser> _users { get; set; }    [Parameter]    public EventCallback OnRequestReload { get; set; }    private ConfirmDialog dialog = default!;    private Modal modal = default!;    private bool showItems = false;    private string message = "Mostrar";    private RequestItemsStoreDTO _requestItemsStoreLote { get; set; } = new RequestItemsStoreDTO();    private RequestItemsStoreDTO _requestItemsStoreSinLote { get; set; } = new RequestItemsStoreDTO();    public RequestItems _requestItems { get; set; }    protected override void OnInitialized()    {         _requestItemsStoreLote.IDPED = request.IDPED;        _requestItemsStoreSinLote.IDPED = request.IDPED;    }    private async Task ToggleContentAsync()    {        showItems = !showItems;        message = message == "Ocultar" ? "Mostrar" : "Ocultar";    }    private string GetStatusName(int status)    {        int index = 1;        foreach (var name in Enum.GetNames(typeof(RequestStatusEnum.Status)))        {            if (index == status)            {                return name;            }            index++;        }        return string.Empty;    }    private void OnRequestItemStoreChangeLoteAdd(ITEM_DTO item)    {        _requestItemsStoreLote.ITEMS.Add(item);    }    private void OnRequestItemStoreChangeLoteRemove(ITEM_DTO item)    {        _requestItemsStoreLote.ITEMS.Remove(item);    }    private void OnRequestItemStoreChangeSinLoteAdd(ITEM_DTO item)    {        _requestItemsStoreSinLote.ITEMS.Add(item);    }    private void OnRequestItemStoreChangeSinLoteRemove(ITEM_DTO item)    {        _requestItemsStoreSinLote.ITEMS.Remove(item);    }    private async Task OnShowModalClick()    {        await modal.ShowAsync();    }    private async Task OnHideModalClick()    {        await modal.HideAsync();    }    private RequestItemsStore TransoformFronDTOS()    {        var item = new RequestItemsStore();        if (_requestItemsStoreLote.ITEMS.Count > 0)        {            item.IDPED = _requestItemsStoreLote.IDPED;            item.ORSTS = 3;            foreach (var item2 in _requestItemsStoreLote.ITEMS)            {                item.ITEMS.Add(new ITEM { MATNR= item2.MATNR, CANTP = item2.CANTP, UASIG = item2.UASIG });            }        }        if (_requestItemsStoreSinLote.ITEMS.Count > 0)        {            item.IDPED = _requestItemsStoreSinLote.IDPED;            item.ORSTS = 3;            foreach (var item2 in _requestItemsStoreSinLote.ITEMS)            {                item.ITEMS.Add(new ITEM { MATNR = item2.MATNR, CANTP = item2.CANTP, UASIG = item2.UASIG });            }        }        return item;    }    private async Task InsertItems()    {        if (_requestItemsStoreLote.ITEMS.Count == 0 && _requestItemsStoreSinLote.ITEMS.Count == 0)        {            return;        }        PreloadService.Show();        var result = await ProductService.InsertItemsByRequestAsync(TransoformFronDTOS());        if (result.error == false)        {            ToastService.Notify(new(ToastType.Success, result.text +", " + result.text2));            await OnRequestReload.InvokeAsync();        }        else        {            ToastService.Notify(new(ToastType.Danger, result.text +", " + result.text2));        }        PreloadService.Hide();    }    private async Task RequestCancel(int requestId)    {        var options = new ConfirmDialogOptions {             IsVerticallyCentered = true,             YesButtonText = "Si",            YesButtonColor = ButtonColor.Danger,            NoButtonText = "No",            NoButtonColor = ButtonColor.Dark        };        var confirmation = await dialog.ShowAsync(           title: "Esta seguro de querer anular este pedido?",           message1: "Esto anulara le pedido actual.",           message2: "Quiere proceder?",           confirmDialogOptions: options);        if (confirmation)        {            PreloadService.Show();            var result = await RequestService.ChangeResquestStatusAsync(requestId, (int)RequestStatusEnum.Status.ANULADO);            if (result.error == false)            {                ToastService.Notify(new(ToastType.Success, result.text));                await OnRequestReload.InvokeAsync();            }            else            {                ToastService.Notify(new(ToastType.Danger, result.text));            }            PreloadService.Hide();        }        else        {            ;        }    }}

RequestsItems

<div class="items"><div class="header"><p><strong>Items: &nbsp;</strong></p><button @onclick="ToggleContentAsync">@message</button></div><Collapse @ref="collapse"><div  class="collapse-container">            @foreach (var item in request.PedidoTiendaItems)            {     <div class="item"><div class="item-matnr"><p><strong>MATNR: </strong> @item.MATNR</p></div><div class="item-quantity"><p><strong>Cantidad: </strong>@item.CANTP</p></div><div class="actions"><RequestItem @ref="_requestItem" _pedidoTiendaItem="@item" _users="_users" _initialData="@request.PedidoTiendaFinalItems"                        OnRequestItemStoreChangeLoteAdd="OnRequestItemChangedLoteAdd"                        OnRequestItemStoreChangeLoteRemove="OnRequestItemChangedLoteRemove"                        OnRequestItemStoreChangeSinLoteAdd="OnRequestItemChangedSinLoteAdd"                        OnRequestItemStoreChangeSinLoteRemove="OnRequestItemChangedSinLoteRemove" /></div></div>            }</div></Collapse></div>@code {    [Parameter]    public DataRequestSearch request { get; set; }    [Parameter]    public List<DataUser> _users { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeLoteAdd { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeLoteRemove { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeSinLoteAdd { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeSinLoteRemove { get; set; }    private RequestItem _requestItem { get; set; }     Collapse collapse = new Collapse();    private string message = "Mostrar";    private async Task ToggleContentAsync()    {        await collapse.ToggleAsync();        message = message == "Ocultar" ? "Mostrar" : "Ocultar";    }    private async Task OnRequestItemChangedLoteAdd(ITEM_DTO item)    {        await OnRequestItemStoreChangeLoteAdd.InvokeAsync(item);    }    private async Task OnRequestItemChangedLoteRemove(ITEM_DTO item)    {        await OnRequestItemStoreChangeLoteRemove.InvokeAsync(item);    }    private async Task OnRequestItemChangedSinLoteAdd(ITEM_DTO item)    {        await OnRequestItemStoreChangeSinLoteAdd.InvokeAsync(item);    }    private async Task OnRequestItemChangedSinLoteRemove(ITEM_DTO item)    {        await OnRequestItemStoreChangeSinLoteRemove.InvokeAsync(item);    }}

RequestItem

@inject IProductService ProductService<Preload LoadingText="Ejecutando..." /><div class="actions">    @if(_requestItemsStoreLote.ITEMS.Count > 0)    {<button @onclick="OnShowModalClick">Manejar (@_requestItemsStoreLote.ITEMS.Count - @_requestItemsStoreLote.ITEMS.Sum(x => x.CANTP))</button>    }    else if (_requestItemsStoreSinLote.ITEMS.Count > 0)    {<button @onclick="OnShowModalClick">Manejar (@_requestItemsStoreSinLote.ITEMS.Count - @_requestItemsStoreSinLote.ITEMS.Sum(x => x.CANTP))</button>    }    else    {<button @onclick="OnShowModalClick">Manejar (0 - 0)</button>    }</div><Modal @ref="modal" title="Item" Size="ModalSize.ExtraLarge"><BodyTemplate><div class="product-grid"><div class="image"><img src="@_image" /></div><div class="code"><p><strong>Codigo: </strong>@_pedidoTiendaItem?.Producto?.MATNR</p></div><div class="name"><p><strong>Nombre: </strong>@_pedidoTiendaItem?.Producto?.MAKTG</p></div><div class="quantity"><p><strong>Cantidad: </strong>@_pedidoTiendaItem?.CANTP</p></div></div><hr /><div>            @if (_pedidoTiendaItem.ProdConLote is not null && _pedidoTiendaItem.ProdConLote.Count > 0)            {<div class="lotes"><div><p><strong>Lotes: </strong></p></div><div class="lotes-container">                        @foreach (var lote in _pedidoTiendaItem.ProdConLote)                        {                            @if(lote.VENCIDO == 0)                            {<RequestLotes _MATNR="@_pedidoTiendaItem?.Producto?.MATNR" _lote="lote" _users="_users" OnSelectQuantityLotes="OnSelectQuantityLotes" />                            }                        }</div></div>            }            else if (_pedidoTiendaItem.ProdSinLote is not null)            {<div class="lote"><div><p><strong>Sin Lote:</strong></p></div><RequestSinLote _lote="@_pedidoTiendaItem.ProdSinLote" _users="_users" OnSelectQuantitySinLotes="OnSelectQuantitySinLotes" /></div>            }<hr />            @if(_pedidoTiendaItem.ArticulosBodegas is not null && _pedidoTiendaItem.ArticulosBodegas.Count > 0)            {<div class ="bodegas"><div><p><strong>Bodegas: </strong></p></div>                    @foreach(var item in _pedidoTiendaItem.ArticulosBodegas)                    {<div class="bodega"><div><p><strong>Piso: </strong>@item.Bodega?.FLOOR &nbsp;</p></div><div><p><strong>AISLE: </strong>@item.Bodega?.AISLE &nbsp;</p></div><div><p><strong>Columna: </strong>@item.Bodega?.COLUM &nbsp;</p></div><div><p><strong>Rack: </strong>@item.Bodega?.RACKS &nbsp;</p></div><div><p><strong>Paleta: </strong>@item.Bodega?.PALET</p></div></div>                    }</div>            }<hr /><div class="items-store"><p><strong>Asignados @(_requestItemsStoreSinLote.ITEMS.Sum(x => x.CANTP) + _requestItemsStoreLote.ITEMS.Sum(x => x.CANTP))</strong></p>                @if (_requestItemsStoreLote.ITEMS is not null && _requestItemsStoreLote.ITEMS.Count > 0)                {                    @foreach (var item in _requestItemsStoreLote.ITEMS)                    {<div class="item-store"><p>Pedido: @_pedidoTiendaItem.IDPED - Codigo: @item.MATNR - CHARG: @item.CHARG - Cantidad: @item.CANTP - Usuario: @item.UASIG</p><button @onclick="() => RemoveItemStore(item)">X</button></div>                    }                }                @if (_requestItemsStoreSinLote.ITEMS is not null && _requestItemsStoreSinLote.ITEMS.Count > 0)                {                    @foreach (var item in _requestItemsStoreSinLote.ITEMS)                    {<div class="item-store"><p>Pedido: @_pedidoTiendaItem.IDPED - Codigo: @item.MATNR - Cantidad: @item.CANTP - Usuario: @item.UASIG</p><button @onclick="() => RemoveItemStore(item)">X</button></div>                    }                }</div></div></BodyTemplate><FooterTemplate><Button Color="ButtonColor.Secondary" @onclick="OnHideModalClick">Cerrar</Button></FooterTemplate></Modal>@code {    [Inject] protected ToastService ToastService { get; set; }    [Inject] protected PreloadService PreloadService { get; set; }    [Parameter]    public PedidoTiendaItem _pedidoTiendaItem { get; set; }    [Parameter]    public List<DataUser> _users { get; set; }    [Parameter]    public List<PedidoTiendaFinalItem> _initialData { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeLoteAdd { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeLoteRemove { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeSinLoteAdd { get; set; }    [Parameter]    public EventCallback<ITEM_DTO> OnRequestItemStoreChangeSinLoteRemove { get; set; }    private Modal modal = default!;    private string _image = @"/images/no_image.png";    private RequestItemsStoreDTO _requestItemsStoreLote = new RequestItemsStoreDTO();    private RequestItemsStoreDTO _requestItemsStoreSinLote = new RequestItemsStoreDTO();    protected override async Task OnInitializedAsync()    {        _requestItemsStoreLote.IDPED = _pedidoTiendaItem.IDPED;        _requestItemsStoreSinLote.IDPED = _pedidoTiendaItem.IDPED;        await InsertInitialData();    }    private async Task InsertInitialData()    {        foreach (var item in _initialData)        {            if(item.CHARG is not null &&                !string.IsNullOrEmpty(item.CHARG) &&                 !_requestItemsStoreLote.ITEMS.Any(x => x.CHARG == item.CHARG && x.MATNR == item.MATNR) &&                _pedidoTiendaItem.MATNR == item.MATNR)            {                var aux = new ITEM_DTO { MATNR = item.MATNR, CHARG = item.CHARG, CANTP = item.CANTP, UASIG = item.UASIG };                _requestItemsStoreLote.ITEMS.Add(aux);                await OnRequestItemStoreChangeLoteAdd.InvokeAsync(aux);            }            else if (!_requestItemsStoreSinLote.ITEMS.Any(x => x.MATNR == item.MATNR) && _pedidoTiendaItem.MATNR == item.MATNR)            {                var aux = new ITEM_DTO { MATNR = item.MATNR, CHARG = item.CHARG, CANTP = item.CANTP, UASIG = item.UASIG };                _requestItemsStoreSinLote.ITEMS.Add(aux);                await OnRequestItemStoreChangeSinLoteAdd.InvokeAsync(aux);            }        }    }    private async Task LoadImageOfProduct()    {        if(_pedidoTiendaItem?.Producto?.MATNR == null)        {            return;        }        var result = await ProductService.GetImageOfProductAsync(_pedidoTiendaItem.Producto.MATNR);        if(result.error == false && !string.IsNullOrEmpty(result.data))        {            _image = result.data;        }    }    private async Task OnShowModalClick()    {        PreloadService.Show();        await LoadImageOfProduct();        PreloadService.Hide();        await modal.ShowAsync();    }    private async Task OnHideModalClick()    {        await modal.HideAsync();    }    private async Task OnSelectQuantityLotes(ITEM_DTO item)    {        if (_requestItemsStoreLote.ITEMS.Any(x => x.CHARG == item.CHARG))        {            ToastService.Notify(new(ToastType.Danger, $"Error item: {item.MATNR} con CHARG: {item.CHARG} ya existente."));            return;        }        int loteMax = _pedidoTiendaItem.ProdConLote.Where(x => x.CHARG == item.CHARG).FirstOrDefault().CLABS;        int previousSum = _requestItemsStoreLote.ITEMS.Where(x => x.CHARG == item.CHARG).Sum(x => x.CANTP);        if (previousSum + item.CANTP > loteMax)        {            ToastService.Notify(new(ToastType.Danger, "Error la cantidad de materiales supera a la cantidad de materiales disponibles en el lote."));            return;        }        if (_requestItemsStoreLote.ITEMS.Sum(x => x.CANTP)  + item.CANTP > _pedidoTiendaItem.CANTP)        {            ToastService.Notify(new(ToastType.Danger, "Error la cantidad de materiales supera a la cantidad de materiales del item."));            return;        }        _requestItemsStoreLote.ITEMS.Add(item);        await OnRequestItemStoreChangeLoteAdd.InvokeAsync(item);    }    private async Task OnSelectQuantitySinLotes(ITEM_DTO item)    {        if(_requestItemsStoreSinLote.ITEMS.Any(x => x.MATNR == item.MATNR))        {            ToastService.Notify(new(ToastType.Danger, $"Error item con {item.MATNR} ya existente."));            return;        }        int sinloteMax = (_pedidoTiendaItem.ProdSinLote.LABST - (_pedidoTiendaItem.ProdSinLote.RESERVADOS ?? default(int)));        int previousSum = _requestItemsStoreSinLote.ITEMS.Where(x => x.CHARG == string.Empty).Sum(x => x.CANTP);        if(previousSum + item.CANTP > sinloteMax)        {            ToastService.Notify(new(ToastType.Danger, "Error la cantidad de materiales supera a la cantidad de materiales disponibles."));            return;        }        if (_requestItemsStoreSinLote.ITEMS.Sum(x => x.CANTP) > _pedidoTiendaItem.CANTP)        {            ToastService.Notify(new(ToastType.Danger, "Error la cantidad de materiales supera a la cantidad de materiales disponibles."));            return;        }        _requestItemsStoreSinLote.ITEMS.Add(item);        await OnRequestItemStoreChangeSinLoteAdd.InvokeAsync(item);    }    private async Task RemoveItemStore(ITEM_DTO item)    {           if(item.CHARG == string.Empty || string.IsNullOrEmpty(item.CHARG))        {            _requestItemsStoreSinLote.ITEMS.Remove(item);            await OnRequestItemStoreChangeSinLoteRemove.InvokeAsync(item);        }        else        {            _requestItemsStoreLote.ITEMS.Remove(item);            await OnRequestItemStoreChangeLoteRemove.InvokeAsync(item);        }    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>