我只是想使用 .Net 5/Blazor 服务器应用程序在 Visual Studio 中创建一个小型原型应用程序,我需要在 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');", "");
}
}
这是我使用的两种解决方案。我已经对上面的回复投了赞成票,因为他们提供了解决问题的信息。
解决方案 1:
在此解决方案中,我使用按钮单击来切换 div 标签的可见性(如上)以显示组件。
解决方案2:(我使用的那个)。是下载引导程序包并将其解压缩到我的 wwwroot 目录(其中包含所有需要的文件,包括 popper)。然后我在 _host.cshtml 中分别向 bootstrap.bundle.min.js 和 bootstrap.min.css 添加了一个 .js 和 .css 引用。
<!-- using a bootstrap container with a unique id e.g. entity@1 -->
<div class=container-fluid collapse" id="entity@(myid)">
<div class="row">
<div class="col-lg-12">
<!-- the component that is displayed when the collapse is toggled -->
<MyComponent componentId=@myid />
</div>
</div>
</div>
The component is
@if (componentId > 0)
{
<!-- do razor code -->
}
[Parameter] public Int32 componentId {get; set;}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句