ng-bootstrap - Typeahead dropdown width

旧街凉风 提交于 2019-12-01 17:34:23

问题


I started using the ng-bootstrap Typeahead component and I'm pretty happy with that.

One thing I would like to achieve is to get the dropdown items to have the same width as the input field, while the default behavior applies a width accordingly to the text length. It should be basic CSS...

I created a basic Example in Plunker.

As you can note, the applied style is ignored:

.dropdown-menu { width: 100%;}

While if I use browser dev tools, and apply the same it is applied.

Any idea on how to achieve the result, by using CSS?


回答1:


Add encapsulation: ViewEncapsulation.None to the component

import {Component, ViewEncapsulation} from '@angular/core';

@Component({
  selector: 'ngbd-typeahead-template',
  templateUrl: 'src/typeahead-template.html',
  styleUrls: ['src/typeahead-template.css'],
  encapsulation: ViewEncapsulation.None
})

See updated plunker

Without ViewEncapsulation.None, the styles applied in this component will only effect this component and not any other component on this page.

Read this for more information




回答2:


For me works ng-deep. Looks more safe and scoped:

::ng-deep .dropdown-menu { width: 100%; }



回答3:


@Nandita's answer is correct, directly apply a width to dropdown menu won't affect.

And you want the dropdown menu to have same width as input, so you should add below CSS to her answer:

.dropdown-menu { width: 300px;}

Check result: https://next.plnkr.co/edit/YvOymCLAwYgU3VmJ




回答4:


This code 100% work, but with the class .dropdown-menu any other dropdown will be changed

::ng-deep .dropdown-menu { width: 100%; }

So I just used this code with ngb-typeahead- as the ID:

::ng-deep [id^="ngb-typeahead-"]{ 
width: 100%!important; 
white-space: nowrap!important;
overflow: hidden!important;
text-overflow: ellipsis!important;} 



回答5:


Using scss should do the trick. Find the parent div in your dom, and give it a class 'dropdown-wrapper'.

.dropdown-wrapper {
  .dropdown-menu {
    width: 90%;
  }
}

Add this to your global scss. Cheers!



来源:https://stackoverflow.com/questions/51416897/ng-bootstrap-typeahead-dropdown-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!