微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Bootstrap 4.5和Bootstrap-Select

如何解决Bootstrap 4.5和Bootstrap-Select

以下是重现我的问题的示例HTML文件。第一个下拉菜单根据需要在屏幕上呈现,但有一个例外。当我添加data-mobile="true"属性时,实时搜索框消失。当不包含data-mobile="true"时,将显示实时搜索,但文本会流到屏幕左侧。为dropright使用类无助于解决该问题,并且看来data-mobile="true"对于使下拉菜单中的文本完全显示在屏幕上而不被截断很有用。

但是,第二个下拉列表是多选的,这是我看到的第二个问题。如果不包含data-mobile="true"(例如<select multiple class="form-control selectpicker" data-live-search="true">),则该框为多选框,具有实时搜索功能,但文本不在屏幕上显示。但是,当我添加data-mobile="true"时,该框将根本不再显示或用作多选。

似乎我缺少有关如何正确使用data-mobile="true"的知识。要么就是该数据属性与我正在使用的其他属性存在错误/冲突。我尝试按照https://developer.snapappointments.com/bootstrap-select/methods/的建议添加以下内容,但没有成功

<script>
    $(document).ready(function () {
        $('.selectpicker').selectpicker('mobile');
    })
</script>

任何人都可以提出一个解决方案,以使我的多选框显示在右侧(如第一个下拉列表中所示),然后还具有实时搜索功能吗?

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <Meta charset="utf-8"> 
    <Meta name="viewport" content="width=device-width,initial-scale=1"> 
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZonixN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> 

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.18/dist/css/bootstrap-select.min.css">
    


<style> 
.sidebar { 
    top: 50px; 
    left: 0px; 
    position: sticky; 
} 
</style> 

<style>
body,html {
    height:100%;
    overflow: hidden;
}

/*
 * Off Canvas sidebar at medium breakpoint
 * --------------------------------------------------
 */
@media screen and (max-width: 992px) {

  .row-offcanvas {
    position: relative;
    -webkit-transition: all 0.25s ease-out;
    -moz-transition: all 0.25s ease-out;
    transition: all 0.25s ease-out;
  }

  .row-offcanvas-left
  .sidebar-offcanvas {
    left: -33%;
  }

  .row-offcanvas-left.active {
    left: 33%;
    margin-left: -6px;
  }

  .sidebar-offcanvas {
    position: absolute;
    top: 0;
    width: 33%;
    height: 100%;
    overflow: auto;
  }
}

/*
 * Off Canvas wider at sm breakpoint
 * --------------------------------------------------
 */
@media screen and (max-width: 34em) {
  .row-offcanvas-left
  .sidebar-offcanvas {
    left: -45%;
  }

  .row-offcanvas-left.active {
    left: 45%;
    margin-left: -6px;
  }
  
  .sidebar-offcanvas {
    width: 45%;
  }
}

.card {
    overflow:hidden;
}

.card-body .rotate {
    z-index: 8;
    float: right;
    height: 100%;
}

.card-body .rotate i {
    color: rgba(20,20,0.15);
    position: absolute;
    left: 0;
    left: auto;
    right: -10px;
    bottom: 0;
    display: block;
    -webkit-transform: rotate(-44deg);
    -moz-transform: rotate(-44deg);
    -o-transform: rotate(-44deg);
    -ms-transform: rotate(-44deg);
    transform: rotate(-44deg);
}

</style>
</head> 
  
<body>

<nav class="navbar fixed-top navbar-expand-md navbar-dark bg-primary mb-3">
    <div class="flex-row d-flex">
        <button type="button" class="navbar-toggler mr-2 " data-toggle="offcanvas" title="Toggle responsive left sidebar">
            <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="#" title="Free Bootstrap 4 Admin Template">Admin Template</a>
    </div>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="navbar-collapse collapse" id="collapsingNavbar">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">Home</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="//www.codeply.com">Link</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="#myAlert" data-toggle="collapse">Alert</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="" data-target="#myModal" data-toggle="modal">About</a>
            </li>
        </ul>
    </div>
</nav>

<div class="container-fluid" id="main">
    <div class="row row-offcanvasrow-offcanvas-left vh-100">
    
        <div class="col-md-3 col-lg-2 overflow-auto sidebar-offcanvas h-100 bg-dark pl-0" id="sidebar" role="navigation">
        
            <ul class="nav flex-column sticky-top pl-0 pt-5 mt-3">
            
                <li class="nav-item"><a class="nav-link" href="#">Test 1</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Test 2</a></li>
                
                <select class="selectpicker" data-live-search="true" data-mobile="true" title="Choose">
                  <option data-tokens="ketchup mustard">Hot Dog,Fries and a Soda</option>
                  <option data-tokens="mustard">Burger,Shake and a Smile</option>
                  <option data-tokens="frosting">Sugar,Spice and all things nice</option>
                </select>
                
                <hr>

                <select multiple class="form-control selectpicker" data-live-search="true">
                <!-- <select multiple class="form-control selectpicker" data-live-search="true" data-mobile="true"> -->
                  <option>This is a lot of text that should take a lot of space</option>
                  <option>2</option>
                  <option>3</option>
                  <option>4</option>
                  <option>5</option>
                </select>
                
            </ul>
        </div>

        <!--/col-->
        <main class="col main pt-5 mt-3 h-100 overflow-auto">
            <h1 class="display-4 d-none d-sm-block">
            Bootstrap Dashboard
            </h1>
            <p class="lead d-none d-sm-block">Plus scrolling sidebar,based on Bootstrap 4</p>

            <div class="alert alert-warning fade collapse" role="alert" id="myAlert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                    <span class="sr-only">Close</span>
                </button>
                <strong>Holy guacamole!</strong> It's free.. this is an example theme.
            </div>
            <div class="row mb-3">
                <div class="col-xl-3 col-sm-6 py-2">
                    <div class="card bg-success text-white h-100">
                        <div class="card-body bg-success">
                            <div class="rotate">
                                <i class="fa fa-user fa-4x"></i>
                            </div>
                            <h6 class="text-uppercase">Users</h6>
                            <h1 class="display-4">134</h1>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-sm-6 py-2">
                    <div class="card text-white bg-danger h-100">
                        <div class="card-body bg-danger">
                            <div class="rotate">
                                <i class="fa fa-list fa-4x"></i>
                            </div>
                            <h6 class="text-uppercase">Posts</h6>
                            <h1 class="display-4">87</h1>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-sm-6 py-2">
                    <div class="card text-white bg-info h-100">
                        <div class="card-body bg-info">
                            <div class="rotate">
                                <i class="fa fa-twitter fa-4x"></i>
                            </div>
                            <h6 class="text-uppercase">Tweets</h6>
                            <h1 class="display-4">125</h1>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-sm-6 py-2">
                    <div class="card text-white bg-warning h-100">
                        <div class="card-body">
                            <div class="rotate">
                                <i class="fa fa-share fa-4x"></i>
                            </div>
                            <h6 class="text-uppercase">Shares</h6>
                            <h1 class="display-4">36</h1>
                        </div>
                    </div>
                </div>
            </div>
            <!--/row-->

            <hr>
        </main>
        <!--/main col-->
    </div>

</div>
<!--/.container-->

</body>
      <!-- Optional JavaScript -->
    <!-- jQuery first,then Popper.js,then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    
    <!-- Latest compiled and minified CSS -->
<script>
$(document).ready(function() {    
  $('[data-toggle=offcanvas]').click(function() {
    $('.row-offcanvas').toggleClass('active');
  });  
});
</script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.18/dist/js/bootstrap-select.min.js"></script>
  
</html> 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。