Angular Material选择下拉框带有固定的最后一个元素

huangapple go评论86阅读模式
英文:

Angular material select dropdown with fixed last element

问题

我目前正在使用Angular Material的select进行工作。

不幸的是,我还没有找到一个实现方式,可以将一个元素固定在列表底部,就像下面的例子一样:

https://jsfiddle.net/vyLk778k/

另外,我不能使用Bootstrap,所以基本上只能使用Angular Material的方法。所以列表项是可滚动的,除了最后一个始终可见且固定在列表末尾。以下是我目前的代码:

<mat-form-field appearance="outline">
  <mat-label>{{ 'Please select one' }}</mat-label>
  <mat-select formControlName="userSelection">
    <span *ngFor="let item of user.usersAvailable">
      <mat-option [value]="item.userName" style="min-height: 67px; padding: 0px 15px; line-height: 1em;">
        <div class="row">
          <div class="col-12">
            <div class="p-0">
              {{item.userName}}
            </div>
          </div>
          <div class="col-12">
            <div class="p-0">
              {{item.userCountry}}
            </div>
          </div>
        </div>
      </mat-option>
    </span>
    <!-- 这是我需要固定的元素 -->
    <mat-option>
      {{ 'Create new user' }}
    </mat-option>
    <!-- 结束需要固定的元素 -->
  </mat-select>
  <mat-error>
    <ag-show-errors [control]="usersForm.controls.userSelection"></ag-show-errors>
  </mat-error>
</mat-form-field>

我希望能够将选择下拉列表的最后一个元素固定住。

英文:

I'm currently working with Angular Material select.

Unfortunately I haven't found an implementation that allows me to fix an element on the bottom of the list as follows example:

https://jsfiddle.net/vyLk778k/

Also, I can't use bootstrap so basically the approach must be with angular material.
So the list items are scrollable except the last one which always should be visible and fixed at the end of the list. Here's what I got so far:

&lt;mat-form-field appearance=&quot;outline&quot;&gt;
            &lt;mat-label&gt;{{ &#39;Please select one&#39; }}&lt;/mat-label&gt;
            &lt;mat-select formControlName=&quot;userSelection&quot;&gt;
              &lt;span *ngFor=&quot;let item of user.usersAvailable&quot;&gt;
                &lt;mat-option [value]=&quot;item.userName&quot;
                  style=&quot;min-height: 67px; padding: 0px 15px; line-height: 1em;&quot;&gt;

                  &lt;div class=&quot;row&quot;&gt;
                    &lt;div class=&quot;col-12&quot;&gt;
                      &lt;div class=&quot;p-0&quot;&gt;
                        {{item.userName}}
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;col-12&quot;&gt;
                      &lt;div class=&quot;p-0&quot;&gt;
                        {{item.userCountry}}
                      &lt;/div&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;

                &lt;/mat-option&gt;
              &lt;/span&gt;
              &lt;!-- This is the element I need to be fixed --&gt;
              &lt;mat-option&gt;
                {{ &#39;Create new user&#39; }}
              &lt;/mat-option&gt;
              &lt;!-- End element I need to be fixed --&gt;
            &lt;/mat-select&gt;
            &lt;mat-error&gt;
              &lt;ag-show-errors [control]=&quot;usersForm.controls.userSelection&quot;&gt;
              &lt;/ag-show-errors&gt;
            &lt;/mat-error&gt;
          &lt;/mat-form-field&gt;

I'm expecting to get the last element of the select dropdown fixed.

答案1

得分: 0

请将以下内容翻译为中文:

添加类似以下样式的 SCSS 样式:

/* 将以下内容添加到组件的 CSS/SCSS 文件中 */
::ng-deep .mat-select-panel .mat-option:last-child {
    position: sticky;
    bottom: 0;
    background-color: white;
    border-top: 1px solid rgba(0, 0, 0, 0.12);
    font-weight: bold;
}

然后更新你的 HTML 如下:

<mat-form-field appearance="outline">
<mat-label>{{ 'Please select one' }}</mat-label>
<mat-select formControlName="userSelection">
    <span *ngFor="let item of user.usersAvailable">
    <mat-option [value]="item.userName"
                style="min-height: 67px; padding: 0px 15px; line-height: 1em;">
        <!-- 在此处添加你现有的选项内容 -->
    </mat-option>
    </span>
    <!-- 这是我需要修复的元素 -->
    <mat-option>
    {{ 'Create new user' }}
    </mat-option>
    <!-- 结束我需要修复的元素 -->
</mat-select>
<mat-error>
    <ag-show-errors [control]="usersForm.controls.userSelection">
    </ag-show-errors>
</mat-error>
</mat-form-field>

希望这对你有用!

英文:

add scss style like this one :

/* Add this to your component&#39;s CSS/SCSS file */
::ng-deep .mat-select-panel .mat-option:last-child {
    position: sticky;
    bottom: 0;
    background-color: white;
    border-top: 1px solid rgba(0, 0, 0, 0.12);
    font-weight: bold;
}

then update your HTML to be :

&lt;mat-form-field appearance=&quot;outline&quot;&gt;
&lt;mat-label&gt;{{ &#39;Please select one&#39; }}&lt;/mat-label&gt;
&lt;mat-select formControlName=&quot;userSelection&quot;&gt;
    &lt;span *ngFor=&quot;let item of user.usersAvailable&quot;&gt;
    &lt;mat-option [value]=&quot;item.userName&quot;
                style=&quot;min-height: 67px; padding: 0px 15px; line-height: 1em;&quot;&gt;
        &lt;!-- Your existing option content here --&gt;
    &lt;/mat-option&gt;
    &lt;/span&gt;
    &lt;!-- This is the element I need to be fixed --&gt;
    &lt;mat-option&gt;
    {{ &#39;Create new user&#39; }}
    &lt;/mat-option&gt;
    &lt;!-- End element I need to be fixed --&gt;
&lt;/mat-select&gt;
&lt;mat-error&gt;
    &lt;ag-show-errors [control]=&quot;usersForm.controls.userSelection&quot;&gt;
    &lt;/ag-show-errors&gt;
&lt;/mat-error&gt;
&lt;/mat-form-field&gt;

I hope this works for you!

huangapple
  • 本文由 发表于 2023年8月9日 06:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定