【问题标题】:How to use Bootstrap Collapse in Blazor app如何在 Blazor 应用程序中使用 Bootstrap Collapse
【发布时间】: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');", "");
        }
    }

【问题讨论】:

    标签: twitter-bootstrap blazor


    【解决方案1】:

    您可以创建一个专门用于执行此操作的组件。然而,当 Bootstrap 崩溃时,这不会从打开到关闭的平滑过渡。请注意,它包含在 Bootstrap 卡中 - 如果不需要,使用可以简单地删除它。

    @using Microsoft.AspNetCore.Components
    
    <div class="pb-3">
       <div class="card">
           <div class="card-header">
                <button class="btn btn-link" style="color: black; text-decoration: none" @onclick="Toggle">
                   <b>@CardHeaderTitle</b>
                </button>
                @CardHeader
                <button @onclick="Toggle" type="button" class="btn btn-secondary btn-sm float-right">
                    <span class="@BtnClass"></span>
                </button>
            </div>
            @if (ShowCardBody)
            {
                <div class="card-body">
                    @CardBody
                </div>
            }
        </div>
    </div>
    
    @code {
        [Parameter] public RenderFragment CardBody { get; set; }
        [Parameter] public RenderFragment CardHeader { get; set; }
        [Parameter] public string CardHeaderTitle { get; set; }
    
    
        [Parameter] public bool ShowCardBody { get; set; } = false;
        public string BtnClass => ShowCardBody ? "oi oi-collapse-up" : "oi oi-collapse-down";
        public void Toggle()
        {
            ShowCardBody = !ShowCardBody;
        }
    }
    

    然后你可以像这样使用它:

    <CollapsibleCard CardHeaderTitle="Title">
        <CardHeader>
            ...
        </CardHeader>
        <CardBody>
            ...
        </CardBody>
    </CollapsibleCard>
    

    如果您想使用 Bootstrap 折叠,请尝试使用数据目标解释的方法 here。这以前对我有用。如果您需要进一步的帮助,请告诉我。

    【讨论】:

    • 就是这样。我可以建议一个关于过渡的纯 css 动画吗?
    • 您好,谢谢,我会调查数据目标 - 您还有其他示例吗? - 当我尝试使用该示例时,它不起作用。我试图避免重写 Bootstrap 中已经存在的功能。
    • 您是否记得将 Bootstrap JavaScript 所需的脚本添加到您的 index.html(客户端 Blazor)或 _Host.cshtml(服务器端 Blazor)? Visual Studio 中的 Blazor 模板默认不提供此功能。在这里查看 Bootstrap 4.5 getbootstrap.com/docs/4.5/getting-started/introduction/#js。我只是尝试创建一个新的 Blazor 项目并包含脚本,然后 Bootstrap 文档中的折叠数据目标示例就起作用了。
    • 不,我没有 - 但会尝试。该应用程序无法访问互联网,因此无法使用 CDN 链接。我已经使用 NuGet 安装了 - 你是否有为此添加到 _hosts.cshtml 的示例脚本/链接 - 引导站点缺少此信息。
    【解决方案2】:

    这是我使用的两种解决方案。我对上面的回复表示赞同,因为他们提供了解决问题的信息。

    解决方案 1:
    在这个解决方案中,我使用按钮单击来切换 div 标签的可见性(如上)以显示组件。

    解决方案 2:(我使用的那个)。是下载引导包并将其解压缩到我的 wwwroot 目录中(这包含所有需要的文件,包括 popper)。然后我在 _host.cshtml 中分别添加了一个 .js 和 .css 引用到 bootstrap.bundle.min.js 和 bootstrap.min.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;}
    

    【讨论】:

    • 您是否能够为此行为添加单元测试?我有点纠结这个
    • 我不使用单元测试,但是是的,它可以工作。我用我当前的解决方案更新了我上面的帖子。
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 2020-04-03
    • 2021-07-14
    • 1970-01-01
    • 2016-04-19
    • 2023-02-06
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多