Angular:如何隐藏 PrimeNG 下拉所选选项?

发布于 2025-01-18 12:52:50 字数 697 浏览 1 评论 0原文

我需要我的 p-dropdown 隐藏先前选择的选项。

例如,我有选项abc。如果我先前选择了a,则仅BC应显示在下拉列表中。然后,如果我选择b,现在只能显示 a a c 。

//component.ts
this.planOptions = [
  { name: 'A'},
  { name: 'B'},
  { name: 'C'}
]; 
//component.html
<p-dropdown id="plan_option"
  [options]="planOptions
  [(ngModel)]="selectedPlanOption"
  optionLabel="name">
</p-dropdown>       

I need my p-dropdown to hide the previously selected option.

For example, I have options A, B, and C. If I previously selected A, only B and C should show up in the dropdown list. Then if I select B, now only A and C should show up.

//component.ts
this.planOptions = [
  { name: 'A'},
  { name: 'B'},
  { name: 'C'}
]; 
//component.html
<p-dropdown id="plan_option"
  [options]="planOptions
  [(ngModel)]="selectedPlanOption"
  optionLabel="name">
</p-dropdown>       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热鲨 2025-01-25 12:52:50

您应该创建一个只包含可能选项的PlanOptions的副本

TS文件

copyOfPlanOption = planOption;
chosenOption: 'A';
const index = copyOfPlanOption.indexOf(chosenOption);
if (index > -1) {
  copyOfPlanOption.splice(index, 1);
}

HTML文件

 <p-dropdown [style]="{'minWidth':'100%', 'width': '100px', 'height':'50px'}" id="plan_option" appendTo="body" [options]="copyOfPlanOption" [(ngModel)]="selectedPlanOption" placeholder=" "  optionLabel="name"></p-dropdown>       

You should create a copy of planOptions that contains only the possible options

TS File

copyOfPlanOption = planOption;
chosenOption: 'A';
const index = copyOfPlanOption.indexOf(chosenOption);
if (index > -1) {
  copyOfPlanOption.splice(index, 1);
}

HTML File

 <p-dropdown [style]="{'minWidth':'100%', 'width': '100px', 'height':'50px'}" id="plan_option" appendTo="body" [options]="copyOfPlanOption" [(ngModel)]="selectedPlanOption" placeholder=" "  optionLabel="name"></p-dropdown>       
中性美 2025-01-25 12:52:50

您可以将选项拼接,然后用CSS隐藏&lt; li&gt;

planOptions.splice(index, 1)

然后

.p-dropdown-panel .p-dropdown-items .p-dropdown-item.p-highlight {
          padding: 0px;
}

You can splice the options and then hide the <li> with css.

planOptions.splice(index, 1)

Then

.p-dropdown-panel .p-dropdown-items .p-dropdown-item.p-highlight {
          padding: 0px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文