Giter Club home page Giter Club logo

Comments (1)

w26ak avatar w26ak commented on May 24, 2024

Looks like all MatSelectItem, MatSelectValue and MatSelect have the same issue. The main problem is that the value of selected item text is updated using JavaScript and not using C#. As a result one of the ways to fix it is to manually call JavaScript. Not ideal but solves the problem.

Below are two possible solutions I came up with (version 2.10.0):

  1. Extension method for manual update of selected text:
public static class MatSelectItemExtensions
{
    public static void UpdateSelectedText<T>(this MatSelectItem<T> select)
    {
        Type selectType = typeof(MatSelectItem<T>);
        Type itemType = typeof(T);

        // Get currently selected item
        PropertyInfo currentValueProperty = selectType.GetProperty("CurrentValue", BindingFlags.NonPublic | BindingFlags.Instance);
        T currentValue = (T)currentValueProperty.GetValue(select);

        // Call OnValueChanged which accepts old value and new value. Old value does not seem to be used so we can just provide null
        MethodInfo onValueChangedMethod = selectType.GetMethod("OnValueChanged", BindingFlags.NonPublic | BindingFlags.Instance, new Type[] { itemType, itemType });
        onValueChangedMethod.Invoke(select, new object[] { null, currentValue });
    }
}
  1. Custom class derived from MatSelectItem with extra method for manual update of selected text:
public class MatSelectItemFixed<T> : MatSelectItem<T>
{
    internal MatBlazorSwitchT<int> switchTK = MatBlazorSwitchT<int>.Get();

    public void UpdateSelectedText()
    {
        CallAfterRender(async () =>
        {
            await JsInvokeAsync<object>("matBlazor.matSelect.setValue", Ref, switchTK.FormatValueAsString(GetKeyFromValue(CurrentValue), null));
        });
    }
}

For version 2.8.0 the solution is more or less the same:

public static class MatSelectItemExtensions
{
    public static void UpdateSelectedText<T>(this MatSelectItem<T> select)
    {
        // Call OnValueChanged which accepts bool value that indicates whether value is changed
        Type selectType = typeof(MatSelectItem<T>);
        MethodInfo onValueChangedMethod = selectType.GetMethod("OnValueChanged", BindingFlags.NonPublic | BindingFlags.Instance);
        onValueChangedMethod.Invoke(select, new object[] { true });
    }
}

from matblazor.

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.