AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79108169
Accepted
thecodeiackiller
thecodeiackiller
Asked: 2024-10-21 06:26:05 +0800 CST2024-10-21 06:26:05 +0800 CST 2024-10-21 06:26:05 +0800 CST

如何维护 Blazor 表单中多个级联下拉菜单的单独状态?

  • 772

链接到 GitHub 以快速访问最小可重现示例(下面也提供了代码): https: //github.com/thecodeiackiller/cascadingSelectListProblem

  • #1)我有一份锻炼清单。
  • #2)我有 2 个枚举 (WorkoutType 和 ExerciseType),可以过滤锻炼列表。
  • #3)我有一个 EditForm,它允许用户: 选择 WorkoutType(LowerBody 或 UpperBody),它会过滤锻炼列表,并 选择 ExerciseType(Main、Secondary、Accessory),它会进一步过滤列表。

注意:对于那些不熟悉健身领域的人来说,在给定的锻炼中,您通常有一种锻炼类型(腿部锻炼、上身锻炼等),并且在该锻炼中,您有运动类型(主要/较重的运动、次要运动和辅助运动(锻炼小肌肉)。

预期按时间顺序排列的用户流量:

    1. 选择了 WorkoutType 并过滤了练习列表(我相信我的表格已经成功做到了这一点)。
    1. 对于 EditForm 中的第一行,用户选择一个 ExerciseType(主要、次要、附件)并且列表再次被过滤。
    1. 用户从现在经过两次筛选的列表中选择一项练习的名称。仍在第一行。
    1. 用户导航至第二行。
    1. 用户选择锻炼类型
    1. 用户从列表中选择新的练习名称(应根据第二行所选的练习类型进行过滤)。

问题:第二行的练习名称列表没有更新。练习名称列表与第一个练习相同,尽管练习类型不同,这应该会改变显示的练习。

我当前实现的AddWorkout.razor组件:

@using stateManagementMinimumReproducibleExample.Repositories
@using stateManagementMinimumReproducibleExample.Models
@page "/addworkout"
@rendermode InteractiveServer
@inject UserExerciseRepository userExerciseRepository

<EditForm Model="userExercise">
    <label for="workoutType"> Workout Type: </label>
    <InputSelect id="workoutType" @bind-Value="userExercise.WorkoutType" @bind-Value:after="GetExercisesByWorkoutType">
        @foreach (var type in Enum.GetValues(typeof(WorkoutType)))
        {
            <option value="@type">@type</option>
        }
    </InputSelect>

    @for (int i = 0; i < numberOfExercisesInWorkout; i++)
    {
        var j = i;
        if (j >= exerciseListForSelectedDay.Count)
        {
            exerciseListForSelectedDay.Add(new UserExercise());
        }

        <div>
            <label for="exerciseType"> Exercise Type: </label>
            <InputSelect id="exerciseType" @bind-Value="exerciseListForSelectedDay[j].ExerciseType" @bind-Value:after="GetExercisesByExerciseType">
                @foreach (var type in Enum.GetValues(typeof(ExerciseType)))
                {
                    <option value="@type">@type</option>
                }
            </InputSelect>

            <label for="exerciseName"> Exercise: </label>
            <InputSelect id="exerciseName" @bind-Value="exerciseListForSelectedDay[j].ExerciseId">
                @foreach (var exercise in listOfExerciseByExerciseType)
                {
                    <option value="@exercise.Id">@exercise.Name</option>
                }
            </InputSelect>
        </div>
    }
</EditForm>


@code {
    private int numberOfExercisesInWorkout = 2;

    private UserExercise userExercise = new();

    private List<UserExercise> exerciseListForSelectedDay = new();

    public List<Exercise> listOfExercisesByWorkoutType = new();

    public List<Exercise> listOfExerciseByExerciseType = new();

    public async Task GetExercisesByWorkoutType()
    {
            listOfExercisesByWorkoutType = await   userExerciseRepository.GetExercisesBasedOnWorkoutType(userExercise.WorkoutType);
            StateHasChanged();
    }

    public void GetExercisesByExerciseType()
    {
            listOfExerciseByExerciseType = userExerciseRepository.GetExercisesFromWorkoutListBasedOnExerciseType(listOfExercisesByWorkoutType, userExercise.ExerciseType);;
    }
}

我考虑过创建一个字典来存储锻炼中每项练习的练习列表的可能性。此外,我还考虑将 List 作为属性存储在我的 UserExercise 类中,然后将过滤后的练习列表添加到该属性中。我不确定我应该走哪条路,或者是否存在任何潜在的特定于 Blazor 的状态管理替代方案。

上面的代码应该能给你足够的信息来诊断问题。如果你需要直观的了解,以下是在分叉和克隆 GitHub 存储库后快速重现问题的步骤:

  • 启动应用程序
  • 导航至 /addworkout 端点
  • 选择锻炼类型(整个表格)
  • 选择锻炼类型(第一行)
  • 选择一项练习(第一行)
  • 选择锻炼类型(第二行)
  • 选择一项练习(第二行)

此时,您将看到,由于选择了与第一行不同的 ExerciseType,因此第二行的筛选列表尚未更新。我如何才能让第二行(及以后)的适当练习列表出现?谢谢。

blazor
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    Jalpa Panchal
    2024-10-21T16:31:06+08:002024-10-21T16:31:06+08:00

    您可以尝试以下代码来解决这个问题:

    添加锻炼.razor:

    @using stateManagementMinimumReproducibleExample.Repositories
    @using stateManagementMinimumReproducibleExample.Models
    @page "/addworkout"
    @rendermode InteractiveServer
    @inject UserExerciseRepository userExerciseRepository
    
    <EditForm Model="userExercise">
        <label for="workoutType"> Workout Type: </label>
        <InputSelect id="workoutType" @bind-Value="userExercise.WorkoutType" @bind-Value:after="GetExercisesByWorkoutType">
            @foreach (var type in Enum.GetValues(typeof(WorkoutType)))
            {
                <option value="@type">@type</option>
            }
        </InputSelect>
    
        @for (int i = 0; i < numberOfExercisesInWorkout; i++)
        {
            var j = i;
            if (j >= exerciseListForSelectedDay.Count)
            {
                exerciseListForSelectedDay.Add(new UserExercise());
            }
    
            <div>
                <label for="exerciseType"> Exercise Type: </label>
                <InputSelect id="exerciseType" @bind-Value="exerciseListForSelectedDay[j].ExerciseType" @bind-Value:after="() => GetExercisesByExerciseType(j)">
                    @foreach (var type in Enum.GetValues(typeof(ExerciseType)))
                    {
                        <option value="@type">@type</option>
                    }
                </InputSelect>
    
                <label for="exerciseName"> Exercise: </label>
                <InputSelect id="exerciseName" @bind-Value="exerciseListForSelectedDay[j].ExerciseId">
                    @foreach (var exercise in exerciseListForSelectedDay[j].FilteredExercises)
                    {
                        <option value="@exercise.Id">@exercise.Name</option>
                    }
                </InputSelect>
            </div>
        }
    </EditForm>
    
    @code {
        private int numberOfExercisesInWorkout = 2;
        private UserExercise userExercise = new();
        private List<UserExercise> exerciseListForSelectedDay = new();
    
        public List<Exercise> listOfExercisesByWorkoutType = new();
    
        public async Task GetExercisesByWorkoutType()
        {
            listOfExercisesByWorkoutType = await userExerciseRepository.GetExercisesBasedOnWorkoutType(userExercise.WorkoutType);
            // Update each exercise list based on the new workout type
            foreach (var exercise in exerciseListForSelectedDay)
            {
                exercise.FilteredExercises = userExerciseRepository.GetExercisesFromWorkoutListBasedOnExerciseType(listOfExercisesByWorkoutType, exercise.ExerciseType);
            }
            StateHasChanged();
        }
    
        public async Task GetExercisesByExerciseType(int index)
        {
            // Filter the exercises based on the ExerciseType for the specific row (index)
            var selectedExerciseType = exerciseListForSelectedDay[index].ExerciseType;
            exerciseListForSelectedDay[index].FilteredExercises = userExerciseRepository.GetExercisesFromWorkoutListBasedOnExerciseType(listOfExercisesByWorkoutType, selectedExerciseType);
            await InvokeAsync(StateHasChanged);
        }
    }
    

    用户练习.cs:

    namespace stateManagementMinimumReproducibleExample.Models
    {
        public class UserExercise
        {
            public int Id { get; set; }
            public int ExerciseId { get; set; }
            public ExerciseType ExerciseType { get; set; }
            public WorkoutType WorkoutType { get; set; }
    
            // Add this property to store the filtered exercises for each row
            public List<Exercise> FilteredExercises { get; set; } = new List<Exercise>();
        }
    
    }
    

    所以基本上,在您的代码中,您正在尝试全局更新列表,并且它使用单个列表,因此您看到相同的练习。为了避免这个问题,您可以为行创建单独的过滤列表。

    结果:

    在此处输入图片描述

    • 1

相关问题

  • 如何在 EditForm Blazor 组件内获取 SfListView 模板的正确上下文?

  • Blazor - 清除所有选择选项而不重新获取数据

  • 在 Blazor WASM 中操作 index.html

  • 在输入中输入值后,Blazor 与文本输入的绑定不起作用

  • 具有组件到组件数据传递的 Blazor 页面在加载时引发事件错误

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve