GithubHelp home page GithubHelp logo

Comments (7)

MarvinKlein1508 avatar MarvinKlein1508 commented on June 1, 2024 3

This implementation is even better and more easily manipulatable.

<FluentSelect TOption="Country"
              Label="Country"
              Width="100%;">
    <FluentOption Value="null" Selected="@(Input.CountryId is null)" OnSelect="(val) => Input.CountryId = null">--- Select ---</FluentOption>
    @foreach (var item in _countries)
    {
        <FluentOption TOption="Country" Value="@item.CountryId.ToString()" Selected="@(Input.CountryId == item.CountryId)" OnSelect="(val) => Input.CountryId = item.CountryId">@item.Name</FluentOption>
    }
</FluentSelect>

from fluentui-blazor.

vnbaaij avatar vnbaaij commented on June 1, 2024

You can do either this :

<FluentSelect TOption="Country"
              Label="Country"
              Items="@_countries"
              OptionValue="@(p => p.CountryId.ToString())"
              OptionText="@(p => p.Name)" >
</FluentSelect>

@code {
    public class Country
    {
        public int? CountryId { get; set; }
        public string? Name { get; set; }
    }

    public class MyModel
    {
        public int? CountryId { get; set; } // For new MyModels, no country is preselected
        /* Other properties */
    }

    private List<Country> _countries = new List<Country>
    {
        new Country { CountryId = null, Name="Select a country"},
        new Country { CountryId = 1, Name = "USA" },
        new Country { CountryId = 2, Name = "Canada" },
        new Country { CountryId = 3, Name = "Mexico" }
    };

}

Or

<FluentSelect Label="Country">
              <FluentOption Value="null">Select a country</FluentOption>
              <FluentOption Value="1">USA</FluentOption>
              <FluentOption Value="2">Canada</FluentOption>
              <FluentOption Value="3">Mexico</FluentOption>
</FluentSelect>

from fluentui-blazor.

MarvinKlein1508 avatar MarvinKlein1508 commented on June 1, 2024

@vnbaaij thanks for clarification. But your examples are missing the bind mechanismn. Could you specify how to bind it to MyModel.CountryId correctly?

from fluentui-blazor.

vnbaaij avatar vnbaaij commented on June 1, 2024

Ah, that was because there was no bind in your code as well :)

You can just use @bind-Value="@SelectedValue" or @bind-SelectedOption="@SelectedPerson" as shown in the example on the demo site:

@inject DataSource Data
<FluentSelect TOption="Person"
              Label="Select a person"
              Items="@Data.People.WithVeryLongName()"
              Id="people-listbox"
              Width="200px"
              Height="250px"
              OptionValue="@(p => p.PersonId.ToString())"
              OptionText="@(p => p.LastName + ", " + p.FirstName)"
              @bind-Value="@SelectedValue"
              @bind-SelectedOption="@SelectedPerson" />

<p>
    Selected value: @SelectedValue <br />
    Selected person (strongly typed): @SelectedPerson?.ToString()
</p>

@code
{
    Person? SelectedPerson;
    string? SelectedValue = "4";
}

from fluentui-blazor.

MarvinKlein1508 avatar MarvinKlein1508 commented on June 1, 2024

@vnbaaij my problem here is that I need to bind to the CountryId of MyModel I need the Id in integer not as string. Is there any easy way to bind to int directly?

from fluentui-blazor.

vnbaaij avatar vnbaaij commented on June 1, 2024

You either need to parse it yourself (@bind-Value), or get it from the TOption (@bind-SelectedOption). Like with SelectedPerson.PersonId in the example above

from fluentui-blazor.

MarvinKlein1508 avatar MarvinKlein1508 commented on June 1, 2024

I think I got it. I can use the SelectedOptionChanged EventCallback

<FluentSelect TOption="Country"
              Label="Country"
              Width="100%;"
              Items="@_countries"
              OptionValue="@(p => p.CountryId.ToString())"
              OptionText="@(p => p.Name)"
              OptionSelected="(option) => Input.CountryId == option.CountryId"
              SelectedOptionChanged="(option) => Input.CountryId = option.CountryId > 0 ? option.CountryId : null" />

@code {
    public class Country
    {
        public int CountryId { get; set; }
        public string? Name { get; set; }
    }

    public class MyModel
    {
        public int? CountryId { get; set; } // For new MyModels, no country is preselected
        /* Other properties */
    }

    private List<Country> _countries = new List<Country>
    {
        new Country { CountryId = -1, Name="Select a country"},
        new Country { CountryId = 1, Name = "USA" },
        new Country { CountryId = 2, Name = "Canada" },
        new Country { CountryId = 3, Name = "Mexico" }
    };
}

Another problem is that the ID itself of the country cannot be null, only from the MyModel itself.

from fluentui-blazor.

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.