首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图标添加到ion- ion-select中的选项

将图标添加到ion- ion-select中的选项
EN

Stack Overflow用户
提问于 2017-05-23 02:18:11
回答 5查看 8.6K关注 0票数 9

可以将图标添加到ion-select中的ion-option吗?就像这样

代码语言:javascript
复制
<ion-select  name="type">
        <ion-option value="bar"><ion-icon name="stats"></ion-icon>Bar</ion-option>
        <ion-option value="pie"><ion-icon name="pie"></ion-icon>Pie</ion-option>
      </ion-select>
EN

回答 5

Stack Overflow用户

发布于 2019-04-22 02:56:22

你可以试试这个..。

这对我很有效..查看此站点:https://www.alt-codes.net/star_alt_code.php

我使用的代码是:✰

所以我的代码看起来像这样:

代码语言:javascript
复制
<ion-item>
            <ion-label>Member's Overall Rating</ion-label>
            <ion-select [(ngModel)]="newreport.memberstatus">
            <ion-option value="&#10032;">&#10032;</ion-option>
            <ion-option value="&#10032;&#10032;">&#10032;&#10032;</ion-option>
            <ion-option value="&#10032;&#10032;&#10032;">&#10032;&#10032;&#10032;</ion-option>
            <ion-option value="&#10032;&#10032;&#10032;&#10032;">&#10032;&#10032;&#10032;&#10032;</ion-option>
            <ion-option value="&#10032;&#10032;&#10032;&#10032;&#10032;">&#10032;&#10032;&#10032;&#10032;&#10032;</ion-option>
            </ion-select>
            </ion-item>

结果是这样的(我手机上的屏幕截图导致我手机上的im测试):

screenshot of what the results would be like

票数 2
EN

Stack Overflow用户

发布于 2018-03-14 06:01:22

这对我很有效。

使用CSS修改每个选项的按钮标记中的内部跨度,删除半径并指定一个背景图像

选项保持相同的顺序,这就是属性选择器适用于我的原因

代码语言:javascript
复制
.alert-radio-icon{
    border-radius: 0 !important;
    border: none !important;
    height: 40px !important;
    width: 40px !important;
    background-size: 40px 40px !important;
    background-repeat: no-repeat ;
}

    [id^="alert-input-"][id$="-0"] .alert-radio-icon{
        background-image: url("../assets/images/menu/flag_ni.png") !important;
    }
     [id^="alert-input-"][id$="-1"] .alert-radio-icon{
        background-image: url("../assets/images/menu/flag_gt.png") !important;
    }
    .alert-radio-inner{
        background-color: transparent !important;
    }
票数 1
EN

Stack Overflow用户

发布于 2020-07-29 22:02:04

IonSelect中的IonIcons:

ion-select-option只返回字符串,然而,从技术上讲,Ion-Select所做的一切只是显示一个看起来像下拉菜单的界面。

简而言之,在本例中,您可以做的是创建一个popover组件,通过将dropdown放在可点击的ion-item中,禁用dropdown,使其看起来已启用(对于UX),当ion-item被单击时单击下拉,然后使用该引用在该位置打开popover组件。

所以,我在这样做的时候做了一些考虑:

  1. 我不想重新创建下拉框和图标,我只想更改它显示的内容

  1. 我也希望图标是可点击的

我最终得到的解决方案(请注意,这也是使用ReactiveForms):

menu-options.interface.ts

代码语言:javascript
复制
export interface MenuOptions {
  key: string;
  fn: () => {};
}

MenuOptions将保存要显示的值和按下按钮时要运行的函数

main.component.ts

代码语言:javascript
复制
constructor(private cd: ChangeDetectorRef){}

menuOptions: MenuOptions[] = [
    {
      key: "Text",
      fn: () =>
        this._showAlert("Select this option if you want to type in values"),
    },
    {
      key: "List",
      fn: () =>
        this._showAlert(
          "Select this option if you want to use a pre-defined list of values"
        ),
    },
    {
      key: "Textbox",
      fn: () =>
        this._showAlert(
          "Select this option if you want an area to type a block of text"
        ),
    },
];

// Find location of where popover is to appear and click the element
// which will then trigger the (click) event
onSelectionClick(event: any) {
    let element: HTMLElement = document.getElementById('popoverLoc') as HTMLElement;
    element.click();
}


async openPopover(event: any) {
    const popover = await this.popover.create({
      component: SelectionTypePopoverComponent,
      componentProps: {
        menuOptions: this.menuOptions
      },
      event,
      translucent: true,
    });

    await popover.present();

    const {data} = await popover.onWillDismiss();

    // Get return from popover and set selection value
    this.itemFeatureForm.get('selectionType').setValue(data['selectionType']);

        // If you hardcode the menu options directly in the popover component and pass them back
        // you will need to trigger change detection manually and set array to have 1 value
        // otherwise your dropdown wont appear properly
        // menuOptions: MenuOptions[] =  [{ key: '', fn: () => '' }] 
        //this.cd.detectChanges();
    }

main.component.html

代码语言:javascript
复制
      <ion-item button (click)="onSelectionClick($event)" detail="false">
        <ion-label class="enableItem">Selection Type</ion-label>
        <ion-select
          formControlName="selectionType"
          class="enableItem"
          id="popoverLoc"
          (click)="openPopover($event)"
        >
          <ion-select-option
            *ngFor="let option of menuOptions"
            [value]="option.key"
            >{{ option.key }}</ion-select-option
          >
        </ion-select>
      </ion-item>

  1. 包装离子标签/离子-在可点击的离子项目中选择。这将是第一次点击时注册的内容。

确保您的离子选择被禁用:selectionType: new FormControl({value: '', disabled: true}),

使您的离子选择看起来已启用的

  1. enableItem类:.enableItem {opacity: 1 !important;}

  1. 设置id来标记离子选择

的位置

popover.component.html

代码语言:javascript
复制
<ion-list>
  <div *ngFor="let option of menuOptions; let i = index">
    <ion-grid class="ion-no-padding">
      <ion-row class="ion-no-padding">
        <ion-col class="ion-no-padding">
          <ion-item button detail="false" (click)="onSelection(i)">
            <ion-label>{{ option.key }}</ion-label>
          </ion-item>
        </ion-col>
        <ion-col class="ion-no-padding" size="2">
          <ion-item class="ion-no-padding">
            <ion-button class="ion-no-padding" fill="clear" (click)="onInfo(i)">
              <ion-icon
                name="information-circle-outline"
                slot="icon-only"
              ></ion-icon>
            </ion-button>
          </ion-item>
        </ion-col>
      </ion-row>
    </ion-grid>
  </div>
</ion-list>

确保设置图标列的大小

popover.component.ts

代码语言:javascript
复制
onSelection(index: number) {
    this.popoverCtrl.dismiss({
      selectionType: this.menuOptions[index].key,
    });
}

onInfo(index: number) {
    this.menuOptions[index].fn();
}

我还没有在物理设备上做测试,但当我做了测试时,如果这不起作用,我会更新,但目前,通过这个模式,我可以在下拉列表中显示我想要显示的任何东西,并且仍然使用ionic templating...great的能力、责任和所有这些。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44119567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档