简体   繁体   中英

Ionic: How to display ion-item on multiple lines?

THE SITUATION:

I am using Ionic to build an app.

I need to display a list of info regarding some people. To obtain that i am using the ionic list <ion-list> along with <ion-item> since the layout it offers it is exactly what i need.

The only problem is that each <ion-item> seems to be forced to stay on a single line, cutting the extra text it contains, as shown it the picture:

在此处输入图片说明

THE CODE:

<ion-list>
    <ion-item class="item"> Name: <b> {{ person.name }} </b> </ion-item>
    <ion-item class="item"> Email: <b> {{ person.email }} </b> </ion-item>
    <ion-item class="item"> Title: <b> {{ person.title }} </b> </ion-item>
    <ion-item class="item"> Bio: <b> {{ person.bio }} </b> </ion-item>
</ion-list>

PLUNKER:

Here is a plunker that recreates the situation. You can try to resize the browser, or the internal windows, and you can see how ion-item cut out the extra content.

http://plnkr.co/edit/Qx9fYRpiATK4lgj5g5Rk?p=preview

THE QUESTION:

How can i display the extra content in a <ion-item> element?
Is it possible to display the content in multiple lines?

For Ionic 2 users, you can use text-wrap attribute as:

<ion-item text-wrap>
  Multiline text that should wrap when it is too long to fit on one line in the item.
</ion-item>

You can also see the Utility Attributes Documentation for attributes that can be added to ion-item to transform the text.

EDIT: Although marked as accepted, this answer was written for an early version of Ionic. Odds are, you'll want one of the answers below for the newer versions.

Class item-text-wrap should help you out, like this:

<ion-item class="item item-text-wrap">
  bio: <b> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </b>
</ion-item>

In Ionic v4 you can attach the text-wrap attribute to the ion-label component inside an ion-item . For Instance:

<ion-item>
  <ion-label text-wrap>
    Multiline text that should wrap when it is too long to fit on one line in the item.
  </ion-label>
</ion-item>

EDIT for Ionic v5: Ionic CSS attributes are deprecated and will not work in v5 . replace attribute tags (eg, <ion-label text-wrap> ) with ionic classes (eg, <ion-label class="ion-text-wrap"> ). For example:

<ion-item>
  <ion-label class="ion-text-wrap">
    Multiline text that should wrap when it is too long to fit on one line in the item.
  </ion-label>
</ion-item>

This will also work for v4

You should overwrite the default CSS added to the specific <ion-item> , for example, change:

<ion-item class="item">
    bio: <b> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </b>
</ion-item>

To:

<ion-item class="item" style="white-space: normal;">
    bio: <b> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </b>
</ion-item>

Those solutions only work if you have 2 lines... If you have many lines and you want to show them all, add this to the scss of the component:

.item-block {
    height: auto;
}

.item-ios.item-block .item-inner {
    height: auto;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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