Code Monkey home page Code Monkey logo

Comments (6)

chrissainty avatar chrissainty commented on May 24, 2024

I prefer not to add an EventCallback as I think it’s best to let the consuming developer decide if they want to call StateHasChanged rather than forcing it on them.

Could you provide some example code of what you’re attempting?

from typeahead.

aelmen avatar aelmen commented on May 24, 2024

Hi!

Sure. I bet this is an easy task, I just need help to figure it out.

index.razor
`@page "/"

@if (Equals(SelectedProjekt, null).Equals(false))
{

@SelectedProjekt.DisplayName

} @code{ /// /// Here I need an event for ui refresh when selectedprojekt has changed. /// Data.Projekt SelectedProjekt { get; set; } } `

ProjektComponent.razor

`
@Inject Data.Services.ProjektService ProjektService
<BlazoredTypeahead SearchMethod="@find" MinimumLength="3" Placeholder="Sök" @bind-Value="SelectedProjekt">

@context.Kund.Name - @context.DisplayName


@context.Kund.Name - @context.DisplayName


@if (Equals(SelectedProjekt, null).Equals(false))
{
if (ShowSelectedDetails)
{

@SelectedProjekt.Kund.Name
@SelectedProjekt.Kund.Notis
@SelectedProjekt.DisplayName
} } @code { [Parameter] protected Data.Projekt SelectedProjekt { get; set; } /// /// /// [Parameter] public bool ShowSelectedDetails { get; set; } /// /// /// /// /// private Task> Find(string searchText) { return ProjektService.Find(searchText); } } `

from typeahead.

chrissainty avatar chrissainty commented on May 24, 2024

Ok I think this should work or at least get you most of the way there.

In your ProjektComponent you need to add the below into the code section:

private Data.Projekt _selectedProject;
[Parameter] protected Data.Projekt SelectedProjekt
{
    get => _selectedProject;
    set
    {
        _selectedProject = value;
        OnProjectChanged?.Invoke(_selectedProject);
    }
}
[Parameter] Func<Data.Projekt> OnProjectChanged { get; set; }

Then in your parent component do this:

<ProjektComponent OnProjectChanged="HandleProjectChanged" />

@if (Equals(SelectedProjekt, null).Equals(false))
{

    @SelectedProjekt.DisplayName

}

@code{
    /// Here I need an event for ui refresh when selectedprojekt has changed.
    Data.Projekt SelectedProjekt { get; set; }

    private async Task HandleProjectChanged(Data.Projekt selectedProjekt)
    {
        SelectedProjekt = selectedProjekt;
        StateHasChanged();
    }

}

Let me know how you get on.

from typeahead.

aelmen avatar aelmen commented on May 24, 2024

Since the Func<Data.Projekt> did not allow me to pass any args when doing invoke I switched to use an Event Handler.

In Child Component

   /// <summary>
    /// Private Placeholder
    /// </summary>
    private Data.Projekt _selectedProjekt;
    /// <summary>
    /// Data Binding
    /// </summary>
    [Parameter]
    protected Data.Projekt SelectedProjekt
    {
        get => _selectedProjekt;
        set
        {
            _selectedProjekt = value;
            OnProjectChanged?.Invoke(this, _selectedProjekt);
        }
    }
    /// <summary>
    /// Event Handler
    /// </summary>
    [Parameter] EventHandler<Data.Projekt> OnProjectChanged { get; set; }

In Parent Component

<ProjektComponent ShowSelectedDetails="true" OnProjectChanged="HandleProjectChanged" >
</ProjektComponent>

@if (Equals(SelectedProjekt, null).Equals(false))
{
    <div class="jumbotron">
        <p>@SelectedProjekt.DisplayName</p>
    </div>
}
@code{
    /// <summary>
    /// 
    /// </summary>
    protected Data.Projekt SelectedProjekt { get; set; }
    /// <summary>
    /// Event Handler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="projekt"></param>
    void HandleProjectChanged(object sender, Data.Projekt projekt)
    {
        SelectedProjekt = projekt;
        StateHasChanged();
    }
}

This seem work like a charm. I have no idéa of if this is a good implementation or not. Any suggestions?

I belive you can close this issue now since there is no need for an event on the result template.

from typeahead.

chrissainty avatar chrissainty commented on May 24, 2024

Glad to hear you got it sorted, I’m not sure why you had an issue with using Func<> though.

from typeahead.

aelmen avatar aelmen commented on May 24, 2024

from typeahead.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.