mirror of
https://github.com/NaN-tic/ionic-tryton-infinite-list.git
synced 2023-12-14 05:23:07 +01:00
initial commit
This commit is contained in:
commit
72de77e563
2 changed files with 148 additions and 0 deletions
33
infinite-list-example.html
Normal file
33
infinite-list-example.html
Normal file
|
@ -0,0 +1,33 @@
|
|||
<!--
|
||||
This is a template view of what an infinite-list has to implement
|
||||
-->
|
||||
|
||||
<ion-header>
|
||||
<!-- Hide back button for this specific view-->
|
||||
<ion-navbar>
|
||||
<ion-title> {{ title }}</ion-title>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
|
||||
<ion-content padding>
|
||||
<div #dataContainer></div>
|
||||
<ion-list>
|
||||
<ion-item *ngFor="let i of list_items">
|
||||
{{i.name}}
|
||||
</ion-item>
|
||||
</ion-list>
|
||||
|
||||
<ion-infinite-scroll (ionInfinite)="doInfinite($event)">
|
||||
<ion-infinite-scroll-content></ion-infinite-scroll-content>
|
||||
</ion-infinite-scroll>
|
||||
|
||||
</ion-content>
|
||||
|
||||
<ion-footer>
|
||||
|
||||
<button (click)="goBack()">
|
||||
Back
|
||||
</button>
|
||||
|
||||
</ion-footer>
|
||||
|
115
infinite-list.ts
Normal file
115
infinite-list.ts
Normal file
|
@ -0,0 +1,115 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { NavController, Events } from 'ionic-angular';
|
||||
|
||||
import { TrytonProvider } from '../ngx-tryton-providers/tryton-provider'
|
||||
|
||||
import { EncodeJSONRead } from '../ngx-tryton-json/encode-json-read'
|
||||
|
||||
@Component({
|
||||
selector: 'infnite-list',
|
||||
templateUrl: 'infinite-list-example.html',
|
||||
|
||||
})
|
||||
/**
|
||||
* Locations page.
|
||||
* This view exposes a infinite scrolling list.
|
||||
* An infinite scrolling list is a list that its not loaded will all the date
|
||||
* but it keeps updating itself with new date once the user scrolls pass
|
||||
* a thereshold.
|
||||
*
|
||||
* DO NOT MODIFY THIS CLASS, EXTEND IT INSTEAD
|
||||
*/
|
||||
export class InfiniteList {
|
||||
|
||||
/**
|
||||
* Method to search information
|
||||
* @type {string}
|
||||
*/
|
||||
method: string;
|
||||
/**
|
||||
* Domain of the method
|
||||
* @type {array}
|
||||
*/
|
||||
domain: any[];
|
||||
/**
|
||||
* Fields to search
|
||||
* @type {Array<string>}
|
||||
*/
|
||||
fields: Array<string>;
|
||||
/**
|
||||
* Default location value
|
||||
* @type {string}
|
||||
*/
|
||||
default_value: string;
|
||||
/**
|
||||
* Current offset, this number will increase each time more data is loaded
|
||||
* @type {number}
|
||||
*/
|
||||
offset: number = 0;
|
||||
/**
|
||||
* Number of entries to gather from the server
|
||||
* @type {number}
|
||||
*/
|
||||
limit: number = 10;
|
||||
|
||||
/**
|
||||
* Items to display
|
||||
* @type {Array<any>}
|
||||
*/
|
||||
list_items: Array<any> = [];
|
||||
|
||||
|
||||
|
||||
constructor(public navCtrl: NavController,
|
||||
public trytond_provider: TrytonProvider,
|
||||
public events: Events) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows a view to have infinite scrolling, meaning that it will not
|
||||
* load all the items from a list at the same time, but will load them as
|
||||
* time goes on.
|
||||
* @param {any} infiniteScroll Infinite scroll event
|
||||
*/
|
||||
doInfinite(infiniteScroll: any) {
|
||||
if (this.list_items.length == 0)
|
||||
infiniteScroll.enable(false);
|
||||
console.log("Begin async op");
|
||||
this.loadData().then(() => {
|
||||
// Disable scroll if list is empty
|
||||
if (this.list_items.length == 0)
|
||||
//infiniteScroll.enable(false);
|
||||
console.log(this.list_items)
|
||||
infiniteScroll.complete();
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads new data into the data array
|
||||
*/
|
||||
loadData() {
|
||||
let json_constructor = new EncodeJSONRead;
|
||||
json_constructor.addNode(this.method, this.domain,
|
||||
this.fields, this.offset, this.limit);
|
||||
|
||||
let json = json_constructor.createJson()
|
||||
|
||||
return new Promise(resolve => {
|
||||
this.trytond_provider.search(json).subscribe(
|
||||
data =>{
|
||||
// We do this so we can be sure of the data type afterwards
|
||||
this.list_items = this.list_items.concat(data[this.method]);
|
||||
console.log("Received data", this.list_items);
|
||||
this.offset+=this.limit;
|
||||
|
||||
console.log("Done");
|
||||
this.events.publish("Data loading finished")
|
||||
resolve(true)
|
||||
},
|
||||
error => {
|
||||
console.log("Error", error)
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue