简体   繁体   English

Angular2 Ngb-datepicker 宽度调整

[英]Angular2 Ngb-datepicker width adjustment

i have a question regarding angular2 ngb-datepicker width.我有一个关于 angular2 ngb-datepicker 宽度的问题。 i am using two datepicker's in my app.我在我的应用程序中使用了两个日期选择器。 one is bigger and other is smaller and my question is how to differentiate these two and give widths and make it responsive.一个更大,另一个更小,我的问题是如何区分这两者并给出宽度并使其响应。

Thanks谢谢

I added to div tag "q-datepicker" class我添加到 div 标签“q-datepicker”类

<div class="q-datepicker position-relative w-100">
                <input
                    formControlName="From"
                    class=" form-control rounded-corner bg-white primary-font-color"
                    placement="bottom"
                    placeholder=""
                    [minDate]=""
                    ngbDatepicker
                    #d="ngbDatepicker"
                    readonly=""
                />
                <button
                    type="button"
                    (click)="d.toggle()"
                >
                    <i class="far fa-calendar-alt"></i>
                </button>
            </div>

then add the following styles in style.css然后在 style.css 中添加以下样式

.q-datepicker{
ngb-datepicker{
  left:0 !important;
  width: 350px;
  .ngb-dp-months{
    .ngb-dp-month{
      width: 100%;
      .ngb-dp-weekday{
        width: 2.5rem;
        margin-right: .5rem;
      }
      .ngb-dp-week{
        .ngb-dp-day{
            width: 2.5rem;
            margin-right: .5rem;
            div{
                width: 100%;
            }
        }
      }
    }
  }
}

} }

You can do any styling on instance level using CSS - just specify your custom class on the ngb-datepicker node您可以使用 CSS 在实例级别进行任何样式设置 - 只需在 ngb-datepicker 节点上指定您的自定义类

<ngb-datepicker class="my-datepicker-1"/>

Once you set it up that way you can overwrite any styles in the underlying structure.以这种方式设置后,您可以覆盖底层结构中的任何样式。 It is pretty self explanatory (use Inspect element in your browser).它是不言自明的(在浏览器中使用Inspect element )。

You will see something like that (CSS notation)你会看到类似的东西(CSS 符号)

.my-datepicker-1
  .ngb-dp-header //where you pick month and year
  .ngb-dp-content
    .ngb-dp-month 
      .ngb-dp-week.ngb-dp-weekdays //row with names of the weekdays
      .ngb-dp-week //row with days
        .ngb-dp-day

and you can style pretty much everything there.你可以在那里设计几乎所有的东西。

So for example if you want to have day cells bigger (originally 2rem) you can do the following:因此,例如,如果您想让 day 单元格更大(最初为 2rem),您可以执行以下操作:

.my-datepicker-1 .ngb-dp-day {
  width: 3rem;
  height: 3rem;
  line-height: 3rem;
  text-align: center;
}

and you are good to go.你很高兴去。

If you need to style specific dates depending on the data you can also provide ng-template with html prescription on what should be put inside the div.ngb-dp-day and that way provide additional styling hooks for later use in your stylesheet.如果您需要根据数据设置特定日期的样式,您还可以为ng-template提供关于应该放在div.ngb-dp-day内容的 html 处方,这样就可以提供额外的样式钩子供以后在样式表中使用。

In your template code:在您的模板代码中:

<ngb-datepicker class="my-datepicker-1" [dayTemplate]="myDay" />

<ng-template #myDay let-custom="custom">
  <div class="my-day-1" [class.special-date]="isSpecialDay(date)" [class.custom]="custom">
    {{ date.day }}
  </div>
</ng-template>

and that way bind to data in your controller.并以这种方式绑定到控制器中的数据。 If isSpecialDay(date) returns true for given date your div gets special-date in its class list sequence ( <div class="my-day-1 special-date'> ) and you can use it for extra styling.如果isSpecialDay(date)为给定的日期返回true您的DIV获得special-date在同类产品列表序列( <div class="my-day-1 special-date'>你可以用它额外的造型。

Fortunately, it's a piece of cake using CSS.幸运的是,使用 CSS 是小菜一碟。

1) Set the width of your choice: 1)设置您选择的宽度:

ngb-datepicker {
    width: 400px!important;
}

2) Make the datepicker responsive: 2)使日期选择器响应:

.ngb-dp-month {
    width: 100%;
}
.ngb-dp-weekdays,
.ngb-dp-week {
    justify-content: space-evenly;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM