如何在 Blazor 应用程序中使用 Bootstrap Collapse

安迪

我只是想使用 .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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Blazor 服务器应用程序中使用 Bootstrap-select

如何在Blazor客户端应用程序中使用Bootstrap模态?

如何将此 Bootstrap Carousel 变成可重用的 Razor 组件以在 Blazor 应用程序中使用

如何在Blazor中使用Bootstrap Carousel

如何在 Blazor 应用程序中使用 SASS 生成的引导程序将引导程序样式应用于输入?

如何在 Blazor WASM 应用程序中使用动态路由部分

如何使用 JavaScript 关闭 Bootstrap Collapse?

如何在Angular2应用程序中使用Bootstrap下拉菜单?

如何在我的Typescript和React应用程序中使用@ types / bootstrap

如何在 Blazor 应用程序中使我的计时器泄漏证明?

如何使用Docker使用IIS托管Blazor应用程序?

如何在Blazor Web应用程序内加载jQuery?

如何在Blazor应用程序中加载.gltf

如何在Vue JS 3应用程序中使用Bootstrap 4添加外部CSS和CSS?

如何在带有SystemJs模块系统的Angular 4应用程序中使用ngx-bootstrap

如何在blazor服务器应用程序的组件中使用时间跨度并转换为12小时格式

在 kemal 应用程序中使用 Bootstrap

如何在代码中使用Blazor组件?

首先如何使Bootstrap Collapse隐藏内容?

如何在 Angular 应用程序中集成 Bootstrap CSS

如何将 Bootstrap 添加到 React 应用程序并在其中使用类

如何在Meteor应用程序中更新Bootstrap(并使用LESS)

在Blazor应用程序中使用Azure Active Directory时如何自定义ASP Net Core 3.1中的注销页面

如何在 Blazor 服务器应用程序中呈现父子 rowspan HTML 表?

如何在 Blazor 应用程序的 Razor 页面中添加多个模型?

如何在 Blazor B2C 应用程序启动期间禁用身份验证?

如何在Blazor客户端应用程序的服务中调用HttpClient

.NET Core Blazor应用程序:如何在页面之间传递数据?

如何在Blazor应用程序中仅对经过身份验证的用户响应图像数据?