【发布时间】:2021-04-16 01:27:01
【问题描述】:
我只是想在 Visual Studio 中使用 .Net 5/Blazor 服务器应用程序创建一个小型原型应用程序,我需要在 Blazor 组件中具有 Bootstrap 折叠功能 - 这将在展开时包含一个单独的 Blazor 组件。
我尽量避免在原型中使用 JavaScript 或 jQuery。但是,我可以看到在某些情况下,例如 Bootstrap Collapse,我可能需要这样做。如果可能的话,我希望有组件的本地代码,但如果需要,可以有一个包含常用帮助函数的 .js 文件。
下面的代码给出了一个示例,其中包含一些关于我如何做到这一点的想法 - 有没有人对如何让它发挥作用有任何想法。
<div class="btn btn-primary" @onclick="ViewDetails" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="details">
this is some text for a button
</div>
<div class="collapse" id="details">
this is the details screen for this record
</div>
@code {
private async void ViewDetails()
{
// Button click indicates an event so should be able to get redraw to occur.
// Some mechanism to change the toggle state without calling javascript (dhtml style)
// Use a boolean to change the ARIA state or something like that
_viewDetails = (_viewDetails == false) ? true : false;
this.StateHasChanged();
// Or, call inline jquery to toggle control (preferred) something like.
await _JSRuntime.InvokeVoidAsync("$('#details).collapse('toggle');", "");
}
}
【问题讨论】: