【问题标题】:Selected value not showing when selecting from mat-select element with two radio groups inside从内部有两个单选组的 mat-select 元素中选择时未显示所选值
【发布时间】:2019-02-13 20:06:55
【问题描述】:

我正在尝试创建一个包含两个无线电组的下拉列表。它看起来像这样:

Sort By:   
  Option 1
  Option 2
  Option 3

Order:
  Ascending
  Descending

下拉菜单的图片:

左:我正在努力实现的图像右:当我进行选择时会发生什么(占位符值保留):

我有我想要的样式,但我遇到了一个问题,当我进行选择时,下拉列表中没有填充所选值。我想在“排序方式:”单选组中显示该值。

sample.component.html

<mat-form-field class="c-table-header__sort" (click)="sort()">
  <mat-select class="c-table-header__sort-select" placeholder="Sort">
    <mat-option class="c-table-header__sort-select-option">
      <div>Sort by</div>
      <mat-radio-group class="c-table-header__sort-select-option-rgroup__sort" #sortOptionGroup [(ngModel)]="radioData">
        <mat-radio-button name="order" class="c-table-header__sort-select-option-rgroup__sort-rbutt"
          *ngFor="let sort of sortOptions.options" [value]="sort">{{sort}}
        </mat-radio-button>
      </mat-radio-group>
      <div>Order</div>
      <mat-radio-group class="c-table-header__sort-select-option-rgroup__order" #sortOrderGroup [(ngModel)]="radioTwoData">
        <mat-radio-button name="sort" class="c-table-header__sort-select-option-rgroup__order-rbutt"
          *ngFor="let sort of sortOptions.order" [value]="sort">{{sort}}
        </mat-radio-button>
      </mat-radio-group>
    </mat-option>
  </mat-select>
</mat-form-field>

sample.component.ts(相关部分)

Component({
    ...
    encapsulation: ViewEncapsulation.None
})
export class BillPayScheduledDashboardComponent implements OnInit {

...

radioData;
radioTwoData; 

...   

    sort() { 
        console.log(this.radioData);
        console.log(this.radioTwoData);
    }

}

sample.component.css(相关部分)

.c-table-header__sort-select-option{
    min-width: 75px;
    min-height: 500px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.c-table-header__sort-select-option-rgroup__sort,
.c-table-header__sort-select-option-rgroup__order{
    display: flex;
    flex-direction: column;
}

在我写这篇文章的时候,我也想知道把这一切都放在一个表格中是否可能/更容易。欣赏任何见解/解决方案。

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    如果您想将mat-select 的placeholder 值设置为当前选定的单选按钮值,您可以这样做。

    在您的组件中:

    radioData = '';
    radioTwoData = ''; 
    
    getPlaceholder(): string {
      if (!this.radioData && !this.radioTwoData) {
        return 'Sort';
      } else {
        return this.radioData + ' ' + this.radioTwoData;
      }
    }
    

    在你的模板中:

    <mat-select class="c-table-header__sort-select" [placeholder]="getPlaceholder()">
    

    这会将所选选项显示为您的mat-select 中的占位符。如果您想在placeholder 下方的选择元素中显示选定的值,则需要使用[value] 绑定mat-options,但从您的问题看来,这不是您想要的。

    Here 是所有这一切的堆叠闪电战。

    【讨论】:

    • 感谢您的回复。价值其实就是我想要的!我尝试在我的组件中访问 mat-option 的 value 属性:'@ViewChild('sortMatOption') sortMatOption: ElementRef;'然后使用 'this.sortMatOption.nativeElement.value = this.radioData;' 设置它但这似乎不起作用。这是正确的方法吗?
    • 我也尝试将 mat-option 的 [value] 绑定到 radioData,但这似乎也不起作用
    • 我已经更新了stackblitz,看看吧。目前只有一个单选按钮组显示为值,是否需要同时显示?
    • 嗯,我有点困惑。您是说内部 ngFor 中的 'let option of sortOptions[0].options' 而不是 'let option of optionGroup.options' 吗?另外,您能否解释一下为什么使用 ng-container 以及为什么将 mat-option 放在无线电组内部而不是外部?是的,如果您可以同时包含单选按钮组及其值,我们将不胜感激。
    • ngFors这样应该没问题,我把数据模型改了一下,看看。 ng-container 是一个结构指令,我在这里使用它来包装 div 和单选按钮组,因为我们正在迭代它们,例如创建多个divs 和单选按钮组。如果您将完整的单选组包装在 mat-option 中,则完整的组是一种选择,但我认为如果每个单选按钮都是一种选择会更好。
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2016-02-06
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多