Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 how to make ion-list grow from bottom upwards?

Here is the gist of my code:

<ion-content>
  <ion-list>
    <ion-item *ngFor="let item of array">{{item.name}}</ion-item>
  </ion-list>
</ion-content>
<ion-footer>
  <ion-searchbar (ionInput)="search()"></ion-searchbar>
</ion-footer>

The idea is to have a searchbar at the bottom of the screen, with a list above it that changes depending on the searchbar input. However, this code will make the list populate top-down with a lot of empty space between it and the searchbar if there are few items. I'd like the list to hug the searchbar (basically aligned to ion-content's bottom), while still remaining scrollable inside ion-content. What are some ways to do this?

like image 747
Ray Zhang Avatar asked Jan 05 '23 21:01

Ray Zhang


2 Answers

Working Plunker

To push the ion-list element to the bottom you need to:

  • Set view encapsulation to none
  • Style the ion-list's container to display: flex and flex-direction: column
  • Style the ion-list element using margin-top: auto

No change to your html template should be necessary, and here is a good answer explaining how to push content to the bottom of a container.

Component Decorator

@Component({
  selector: 'page-home',
  templateUrl: 'pages/home/home.html',
  styles: [`
    .scroll-content {
      display: flex;
      flex-direction: column;
    }
    .scroll-content ion-list {
      margin-top: auto;
      margin-bottom: 0;
    }
  `],
  encapsulation: ViewEncapsulation.None
})
like image 73
adriancarriger Avatar answered Jan 13 '23 09:01

adriancarriger


@adriancarriger's answer is great, but a small improvement can be made. Since you're pushing the new elements to the bottom of the list, I guess that it would be nice to always scroll the content to the bottom, to make the new items always visible. That could be easily achieved by adding just a few lines of code:

// First add ViewChild and Content imports
import { ViewChild, Component, ViewEncapsulation } from '@angular/core';
import { NavController, Content } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'pages/home/home.html',
  styles: [`
    .scroll-content {
      display: flex;
      flex-direction: column;
    }
    .scroll-content ion-list {
      margin-top: auto;
      margin-bottom: 0;
    }
  `],
  encapsulation: ViewEncapsulation.None
})
export class HomePage {
  @ViewChild(Content) content: Content; // <-- get a reference of the content

  count = 4; // for demo
  appName = 'Ionic App';
  array = [
    { name: 'item-1' },
    { name: 'item-2' },
    { name: 'item-3' }
  ];

  constructor(private navController: NavController) {

  }
  search() {
    console.log('searching...');
  }
  add(number) {
    let itemNumber = this.count;
    this.array.push({ name: 'item-' + itemNumber });
    this.scrollToBottom(); // <- when the new item is pushed, scroll to the bottom to show it
    this.count += number;
  }

  scrollToBottom(){
    // use the content's dimension to obtain the current height of the scroll
    let dimension = this.content.getContentDimensions();

    // scroll to it (you can also set the duration in ms by passing a third parameter to the scrollTo(x,y,duration) method.
    this.content.scrollTo(0, dimension.scrollHeight);
  }
}
like image 31
sebaferreras Avatar answered Jan 13 '23 10:01

sebaferreras