高亮显示选定的按钮并取消其他按钮的高亮显示

huangapple go评论49阅读模式
英文:

Blazor highlight selected button and remove highlight from others

问题

以下是您要翻译的代码部分:

I'm using a custom object `TestObject` that that has an Id (int), Text(string) and other Details.

These are populated using `@foreach` which then renders a component for each `TestObject`.

When the button is selected, that button should be highlighted by applying a style in the component, any other button's highlight removed and the 'other details' shown on the page.

The 'other details' are switching correctly, but I cannot get the button to highlight properly, and other buttons to remove highlight (1st one is highlighted by default).

I have tried passing in a bool whether the button should be highlighted or not, including whether it's selected in the `TestObject` object and passing in the selected button's `Id` but none of these are updating the buttons. The parent page is calculating this correctly but not passing to the components to re-render.

TestObject.cs

public class TestObject
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Details { get; set; }
    public bool Selected { get; set; }
}

TestComponent.razor

<button @onclick="Click" class="test" style="@styles">
    @Text
</button>

@code {
    [Parameter] public int Id { get; set; }
    [Parameter] public string Text { get; set; }
    [Parameter] public bool Selected { get; set; }
    [Parameter] public int SelectedId { get; set; }
    [Parameter] public EventCallback<int> CallBack { get; set; }

    private string styles = "";

    protected override async Task OnInitializedAsync()
    {
        CheckIfSelected();
    }
    private void Click()
    {
        CallBack.InvokeAsync(Id);
        StateHasChanged();
    }
    private void CheckIfSelected()
    {
        //if (Selected)
        if(Id == SelectedId)
        {
            styles = "background-color: grey;";
        }
        else
        {
            styles = "background-color: white;";
        }
    }
}

TestParent.razor

<div style="border: 1px solid black; background-color: white;">
    @foreach (var T in TestObjects)
    {
        <TestComponent Id="@T.Id" Text="@T.Text" Selected="@T.Selected" SelectedId="@SelectedTestObject.Id" CallBack="SelectObject" />
    }

    <hr />
    <div>
        Details: <br />
        @SelectedTestObject.Details;
    </div>
</div>

@code {
    private List<Models.TestObject> TestObjects = new();
    private TestObject SelectedTestObject = new();

    protected override async Task OnInitializedAsync()
    {
        TestObjects.Add(new TestObject
            {
                Id = 1,
                Text = "Test 1",
                Details = "Test 1 Details"
            });
        TestObjects.Add(new TestObject
            {
                Id = 2,
                Text = "Test 2",
                Details = "Test 2 Details"
            });
        TestObjects.Add(new TestObject
            {
                Id = 3,
                Text = "Test 3",
                Details = "Test 3 Details"
            });

        SelectedTestObject = TestObjects[0];
    }

    private void SelectObject(int SelectedId)
    {
        SelectedTestObject = TestObjects.Where(t => t.Id == SelectedId).FirstOrDefault();
        StateHasChanged();
    }
}

希望这对您有所帮助!

英文:

I'm using a custom object TestObject that that has an Id (int), Text(string) and other Details.

These are populated using @foreach which then renders a component for each TestObject.

When the button is selected, that button should be highlighted by applying a style in the component, any other button's highlight removed and the 'other details' shown on the page.

The 'other details' are switching correctly, but I cannot get the button to highlight properly, and other buttons to remove highlight (1st one is highlighted by default).

I have tried passing in a bool whether the button should be highlighted or not, including whether it's selected in the TestObject object and passing in the selected button's Id but none of these are updating the buttons. The parent page is calculating this correctly but not passing to the components to re-render.

TestObject.cs

public class TestObject
{
public int Id { get; set; }
public string Text { get; set; }
public string Details { get; set; }
public bool Selected { get; set; }
}

TestComponent.razor

&lt;button @onclick=&quot;Click&quot; class=&quot;test&quot;style=&quot;@styles&quot;&gt;
@Text
&lt;/button&gt;
@code {
[Parameter] public int Id { get; set; }
[Parameter] public string Text { get; set; }
[Parameter] public bool Selected { get; set; }
[Parameter] public int SelectedId{ get; set; }
[Parameter] public EventCallback&lt;int&gt; CallBack { get; set; }
private string styles = &quot;&quot;;
protected override async Task OnInitializedAsync()
{
CheckIfSelected();
}
private void Click()
{
CallBack.InvokeAsync(Id);
StateHasChanged();
}
private void CheckIfSelected()
{
//if (Selected)
if(Id == SelectedId)
{
styles = &quot;background-color: grey;&quot;;
}
else
{
styles = &quot;background-color: white;&quot;;
}
}
}

TestParent.razor

&lt;div style=&quot;border: 1px solid black; background-color: white;&quot;&gt;
@foreach (var T in TestObjects)
{
&lt;TestComponent Id=&quot;@T.Id&quot; Text=&quot;@T.Text&quot; Selected=&quot;@T.Selected&quot; SelectedId=&quot;@SelectedTestObject.Id&quot; CallBack=&quot;SelectObject&quot; /&gt;
}
&lt;hr /&gt;
&lt;div&gt;
Details: &lt;br /&gt;
@SelectedTestObject.Details;
&lt;/div&gt;
&lt;/div&gt;
@code {
private List&lt;Models.TestObject&gt; TestObjects = new();
private TestObject SelectedTestObject = new();
protected override async Task OnInitializedAsync()
{
TestObjects.Add(new TestObject
{
Id = 1,
Text = &quot;Test 1&quot;,
Details = &quot;Test 1 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 2,
Text = &quot;Test 2&quot;,
Details = &quot;Test 2 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 3,
Text = &quot;Test 3&quot;,
Details = &quot;Test 3 Details&quot;
});
SelectedTestObject = TestObjects[0];
}
private void SelectObject(int SelectedId)
{
SelectedTestObject = TestObjects.Where(t =&gt; t.Id == SelectedId).FirstOrDefault();
StateHasChanged();
}
}

答案1

得分: 1

这是对您正在使用的设置的快速修复,生成的设置可能不是最佳的,但它基于您尝试实现的内容。

这是您的TextComponent.razor

&lt;button @onclick=&quot;Click&quot; class=&quot;test&quot; style=&quot;background-color: @(Selected? &quot;grey&quot; : &quot;white&quot;);&quot;&gt;
@Text
&lt;/button&gt;
[Parameter] public int Id { get; set; }
[Parameter] public string Text { get; set; }
[Parameter] public bool Selected { get; set; }
[Parameter] public EventCallback&lt;int&gt; CallBack { get; set; }
private void Click()
{
CallBack.InvokeAsync(Id);
}

这是您的TestParent.razor

&lt;div style=&quot;border: 1px solid black; background-color: white;&quot;&gt;
@foreach (var T in TestObjects)
{
&lt;TestComponent Id=&quot;@T.Id&quot; Text=&quot;@T.Text&quot; Selected=@(T.Id==selectedId) CallBack=&quot;SelectObject&quot; /&gt;
}
&lt;hr /&gt;
&lt;div&gt;
Details: &lt;br /&gt;
@SelectedTestObject.Details;
&lt;/div&gt;

</div>

private List&lt;TestObject&gt; TestObjects = new();
private TestObject SelectedTestObject = new();
private int selectedId = 1;
protected override async Task OnInitializedAsync()
{
TestObjects.Add(new TestObject
{
Id = 1,
Text = &quot;Test 1&quot;,
Details = &quot;Test 1 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 2,
Text = &quot;Test 2&quot;,
Details = &quot;Test 2 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 3,
Text = &quot;Test 3&quot;,
Details = &quot;Test 3 Details&quot;
});
SelectedTestObject = TestObjects[0];
}
private void SelectObject(int id)
{
selectedId = id;
SelectedTestObject = TestObjects.FirstOrDefault(x =&gt; x.Id == id);
}
英文:

This is a quick fix to the setup you are using, the resulting setup wont be best but its based on what you are trying to achieve

This is your TextComponent.razor

&lt;button @onclick=&quot;Click&quot; class=&quot;test&quot; style=&quot;background-color: @(Selected? &quot;grey&quot; : &quot;white&quot;);&quot;&gt;
@Text
&lt;/button&gt;
[Parameter] public int Id { get; set; }
[Parameter] public string Text { get; set; }
[Parameter] public bool Selected { get; set; }
[Parameter] public EventCallback&lt;int&gt; CallBack { get; set; }
private void Click()
{
CallBack.InvokeAsync(Id);
}

This is your TestParent.razor

&lt;div style=&quot;border: 1px solid black; background-color: white;&quot;&gt;
@foreach (var T in TestObjects)
{
&lt;TestComponent Id=&quot;@T.Id&quot; Text=&quot;@T.Text&quot; Selected=@(T.Id==selectedId) CallBack=&quot;SelectObject&quot; /&gt;
}
&lt;hr /&gt;
&lt;div&gt;
Details: &lt;br /&gt;
@SelectedTestObject.Details;
&lt;/div&gt;

</div>

private List&lt;TestObject&gt; TestObjects = new();
private TestObject SelectedTestObject = new();
private int selectedId = 1;
protected override async Task OnInitializedAsync()
{
TestObjects.Add(new TestObject
{
Id = 1,
Text = &quot;Test 1&quot;,
Details = &quot;Test 1 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 2,
Text = &quot;Test 2&quot;,
Details = &quot;Test 2 Details&quot;
});
TestObjects.Add(new TestObject
{
Id = 3,
Text = &quot;Test 3&quot;,
Details = &quot;Test 3 Details&quot;
});
SelectedTestObject = TestObjects[0];
}
private void SelectObject(int id)
{
selectedId = id;
SelectedTestObject = TestObjects.FirstOrDefault(x =&gt; x.Id == id);
}

huangapple
  • 本文由 发表于 2023年6月6日 16:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定