hi I'am new in ionic and i working with ionic 2. my problem is :
I have write the functions in my about.ts this function run well (i have test this in the constructor of the page) but when i call this in about.html, nothing of this functions run. (sorry i don't speak english well)
that is my about.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import {Contacts, Contact} from 'ionic-native';
import { AlertController } from 'ionic-angular';
//, ContactField
#Component({
selector: 'page-about',
templateUrl: 'about.html'
})
export class AboutPage {
private customColor:string[] = ["#f44336", "#3f51b5", "#2196f3", "#009688", "#4caf50"];
// index qui nous permet de changer de couleur
private indexColor:number = 0;
// les contactes trouvés
public allContacts:Contact[];
public findItem:string;
constructor(public navCtrl: NavController,public alertCtrl: AlertController) {
this.findContact('');
this.openModal();
}
/** Suppression d'un contact */
public delete(contactToDelete:Contact):void{
contactToDelete.remove().then(()=>this.findContact(''));
}
public getCustomColor():string{
let color:string = this.customColor[this.indexColor];
this.indexColor++;
if(this.indexColor === 5){
this.indexColor = 0;
}
return color;
}
/** Ouverture de la modale pour la création de contact */
public openModal():void{
//let modal = Modal.create(CreateContact);
// this._navController.present(modal);
// modal.onDismiss(data => {
// this.allContacts.push(data);
// });
let alert = this.alertCtrl.create({
title: 'soty',
subTitle: 'rost',
buttons: ['OK']
});
alert.present();
}
/** Méthode pour effectuer la recherche de contact */
public findContact(value:any){
let alert = this.alertCtrl.create({
title: value,
subTitle: value,
buttons: ['OK']
});
alert.present();
let fn = value === undefined ? '' :value;
Contacts.find(['displayName', 'phoneNumbers'], {
filter:fn,
hasPhoneNumber:true
}).then(data => {
this.allContacts = data;
});
}
/** Création des initiales sur 2 lettres */
public getCustomInitial(value):string{
let names:string[] = value.split(' ');
let firstName = '';
let secondName = '';
if(names[0] != undefined){
firstName = names[0].substring(0,1);
}
if(names[1] != undefined){
secondName = names[1].substring(0,1);
}
return firstName + secondName;
}
}
that is my about.html
<ion-header>
<ion-navbar>
<ion-title>
Gestion des contacts
</ion-title>
<ion-buttons start>
<button (click)="openModal()">
<ion-icon ios="ios-add" md="md-add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding class="page1">
<ion-item>
<ion-input
type="text"
placeholder="rechercher"
[(ngModel)]="findItem"
(Change)="findContact($event)"></ion-input>
</ion-item>
<ion-list>
<ion-item-sliding *ngFor="let contact of allContacts">
<ion-item class="bloc-contact">
<ion-avatar item-left>
<span
class="initial" [style.background] = "getCustomColor()">
{{ getCustomInitial(contact.displayName) }}
</span>
</ion-avatar>
<h2>{{contact.displayName}}</h2>
<div *ngFor="let phone of contact.phoneNumbers">
<ion-item *ngIf="phone.type == 'mobile'" class="line">
<ion-icon ios="ios-phone-portrait" md="md-phone-portrait"></ion-icon>
Mobile
<ion-badge teal item-right>{{phone.value}}</ion-badge>
</ion-item>
<ion-item *ngIf="phone.type == 'home'" class="line">
<ion-icon ios="ios-home" md="md-home"></ion-icon>
Home
<ion-badge green item-right>{{phone.value}}</ion-badge>
</ion-item>
</div>
</ion-item>
<ion-item-options>
<button danger (click) = "delete(contact)">
<ion-icon name="trash"></ion-icon>
Delete
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
somebody can help me please
there is no problem with openModal() and the way you call it; but you must use this for your input event:
<ion-input type="text" placeholder="rechercher" [(ngModel)]="findItem" (input)='findContact($event.target.value)'>
Related
i m new in ionic and i want to load my web api data in grid ionic so below is my code.
grid.html
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Grid Demo</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content></ion-refresher-content>
</ion-refresher>
<ion-grid>
<ion-row *ngFor="let product of products">
<ion-col width-50 >
<h1>{{product.title}}</h1>
</ion-col>
</ion-row>
</ion-grid>
<ion-infinite-scroll (ionInfinite)="doInfinite($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
grid.ts
import { Component } from '#angular/core';
import { NavController, NavParams, LoadingController } from 'ionic-angular';
import { ProductListProvider } from '../../providers/product/product'
#Component({
templateUrl: 'grid.html',
providers:[ProductListProvider]
})
export class GridHttpPage {
public response: any;
public products: any =[];
public count: any;
public loader: any;
public page: number = 0;
public isLoading: boolean =true;
public totalItem: number ;
doRefresh(refresher) {
console.log('Begin async operation', refresher);
setTimeout(() => {
this.page=0;
this.loadData(true);
console.log('Async operation has ended');
refresher.complete();
}, 2000);
}
constructor(public navCtrl: NavController, public navParams: NavParams, public personListProvider: ProductListProvider, public loadingCtrl: LoadingController) {
this.count = 0;
this.loadData(this.isLoading);
}
presentLoading() {
this.loader = this.loadingCtrl.create({
// spinner: 'hide',
content: "Please wait...",
// duration: 3000,
// showBackdrop: true,
// enableBackdropDismiss: true,
// dismissOnPageChange: true
});
this.loader.onDidDismiss(() => {
// console.log('Dismissed loading');
});
this.loader.present();
}
loadData(isLoading) {
if(isLoading==true)
{
this.presentLoading();
}
this.page ++;
this.personListProvider.load(this.page)
.then(data => {
this.response = data;
this.totalItem = this.response.listing.total;
//this.products = this.response.listing.data;
for (let i = 0; i < this.response.listing.data.length; i++) {
this.products.push(this.response.listing.data[i]);
// console.log(this.response.listing.data[i]);
}
if(isLoading==true)
{
this.loader.dismiss();
}
console.log(this.response.listing);
console.log(this.products);
console.log(this.totalItem);
});
}
doInfinite(infiniteScroll) {
console.log('Begin async operation');
setTimeout(() => {
// for (let i = 0; i < 30; i++) {
// this.items.push( this.items.length );
// }
if(this.products.length <= this.totalItem )
{
this.loadData(false);
}
console.log('Async operation has ended');
infiniteScroll.complete();
}, 500);
}
}
When i run above code i get following type of output Refer screenshots.
GridView
It display single column list i want to display in 2 column so any idea how can i display in two column in gridview ionic?
I got solution
Make change in grid.ts file
rows: any;
in loadata funtion
loadData(isLoading) {
if(isLoading==true)
{
this.presentLoading();
}
this.page ++;
this.personListProvider.load(this.page)
.then(data => {
this.response = data;
this.totalItem = this.response.listing.total;
for (let i = 0; i < this.response.listing.data.length; i++) {
this.products.push(this.response.listing.data[i]);
}
this.rows = Array.from(Array(Math.ceil(this.products.length / 2)).keys());
if(isLoading==true)
{
this.loader.dismiss();
}
console.log(this.response.listing);
console.log(this.products);
console.log(this.totalItem);
});
}
in grid.html change as follows
<ion-grid>
<ion-row *ngFor="let i of rows">
<ion-col *ngFor="let product of products | slice:(i*2):(i+1)*2" width-50 (click)="openDetailPage(product)">
<ion-card>
<ion-avatar item-left>
<img src="{{product.medium_image}}" />
</ion-avatar>
<ion-card-content>
<ion-card-title>
<h6>{{product.title}}</h6>
<p> <b>Price: </b> {{product.price}}</p>
</ion-card-title>
</ion-card-content>
</ion-card>
</ion-col>
</ion-row>
</ion-grid>
I have a script that retrieve some datas and store it in an array :
export class ProductsByArea {
public products: any;
public area: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
private easyStockService: EasyStockService,
private imageProvider: ProductImageProvider,
private platform: Platform
)
{
//this.area = navParams.get('area');
this.easyStockService.getArea(navParams.get('area')).then((doc) => {
this.area = doc;
this.getProducts(this.area._id).then((products) => {
let datas: any;
datas = products;
this.products = datas;
});
});
}
private getProducts(area: any){
let datas: any = [];
let products: Array<any> = [];
return new Promise((resolve) => {
this.easyStockService.getProducts(area).then((results) => {
datas = results;
let uploadDirectory = this.imageProvider.getUploadDirectory();
// Boucle sur les produits pour récupérer les données cohérentes
datas.forEach((data) => {
let product: any = {};
product.ean = data._id;
product.title = data.title;
if(data.image != 'no-image.png')
product.image = uploadDirectory + data._id + '.jpg';
else
product.image = 'assets/images/no-image.png';
product.stock = this._computeStock(data.inStock);
products.push(product);
})
resolve(products);
}, (error) => {
console.log('Erreur lors de la récupération des produits dans la zone : ' + area);
});
});
and...
private _computeStock(inStock:any){
let averagePrice: number = 0;
let totalStock: number = 0;
let lastPurchase: any = null;
let lastOutDate: any = null;
let totalValue: number = 0;
inStock.forEach((stock) => {
averagePrice += parseFloat(stock.totalPrice) / parseInt(stock.initialQuantity);
totalStock += parseInt(stock.initialQuantity);
lastPurchase = moment(stock.purchaseDate).format('DD-MM-YYYY');
let out = stock.out;
if(out.length){
totalStock -= out.quantity;
}
});
totalValue = totalStock * (averagePrice / inStock.length);
return {'totalStock': totalStock, 'lastPurchaseDate': lastPurchase, 'lastOutDate': lastOutDate, 'totalValue': totalValue};
}
Datas are correctly retrieved, but when i want to display them in a view, i got this kind of screen :
The view code is :
<ion-content>
<ion-list inset id="products-by-area">
<ion-item-sliding *ngFor="let product of products">
<ion-item class="item-thumbnail-left item-icon-right item-complex">
<ion-thumbnail item-left>
<img src="{{ product.image }}" title="{{ product.title }}" />
</ion-thumbnail>
<h2>{{ product.title }}</h2>
<ion-note item-end>
{{ product.ean }}
</ion-note>
</ion-item>
<ion-item-options side="right" *ngIf="product.stock.totalStock > 0">
<button ion-button color="warning">
<ion-icon name="remove-circle"></ion-icon>
{{ 'remove' | translate }}
</button>
<button ion-button color="danger" (click)="toTrash(product)";>
<ion-icon name="trash"></ion-icon>
{{ 'trash' | translate }}
</button>
<button ion-button color="success">
<ion-icon name="add-circle"></ion-icon>
{{ 'add' | translate }}
</button>
</ion-item-options>
<ion-item-options side="left">
<button ion-button color="success">
<ion-icon name="stats"></ion-icon>
{{ 'stats' | translate }}
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
Don't understand why product.title or product.image, or even product.ean are not displayed ?
Try to modify type of the public products property, but... nothing appears, as if the product variable was not an object or an empty object.
I'm new to ionic 2 and really don't understand what it's wrong.
I am in the process of upgrading from Ionic 2 beta to rc3. I have the following components that were working, but there must be something I need to do to make it fit with rc3.
When the user clicks an icon, it invokes the following function inorder to show a popover.
presentPopover(event: Event): void {
this.popover = this.popoverController.create(SearchJobsPopOverPage, {
ev: event
});
this.popover.present();
}
SearchJobsPopOverPage
import { Component } from '#angular/core';
import { NavController, ViewController, NavParams, Events } from 'ionic-angular';
import { MapPage } from '../map/map';
import { CategoryPage } from '../category/category';
import { JobModel } from '../model/jobModel';
import { ReviewPage } from '../review/review';
import { RatingModel } from '../model/ratingModel';
import { PersonModel } from '../model/personModel';
import { DateTimePage } from '../datetime/datetime';
#Component({
//selector: 'searchjobspopover',
template: `
<ion-content padding id="search-popover">
<ion-list>
<ion-row>
<ion-col>
<div style="text-align:center">
<div id="pinButton"><button ion-button class="search-popover-button" (click)="presentFilterMap()" color="danger"><ion-icon class="search-popover-icon" name="pin"></ion-icon></button></div>
<p>Location</p>
</div>
</ion-col>
<ion-col>
<div style="text-align:center">
<div id="pinButton"><button ion-button class="search-popover-button" (click)="presentFilterCategories()" primary><ion-icon class="search-popover-icon" name="happy"></ion-icon></button></div>
<p>Sectors</p>
</div>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<div style="text-align:center">
<div id="pinButton"><button ion-button class="search-popover-button search-button-star" (click)="presentFilterRating()"><ion-icon class="search-popover-icon" name="star"></ion-icon></button></div>
<p>Rating</p>
</div>
</ion-col>
<ion-col>
<div style="text-align:center">
<div id="pinButton"><button ion-button class="search-popover-button" (click)="presentFilterTime()" color="secondary"><ion-icon class="search-popover-icon" name="time"></ion-icon></button></div>
<p>Last Online</p>
</div>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<div style="text-align:center">
<div id="pinButton"><button ion-button class="search-popover-button" (click)="clearFilters()" color="light"><ion-icon class="search-popover-icon" name="trash"></ion-icon></button></div>
<p>Clear Filters</p>
</div>
</ion-col>
</ion-row>
</ion-list>
</ion-content>
`
})
export class SearchJobsPopOverPage {
public nav: NavController = null;
public jobModel: JobModel = null;
public events: Events = null;
public ratingModel: RatingModel = null;
public personModelLoggedIn: PersonModel = null;
public lastOnline: number = null;
constructor(public navParams: NavParams, nav: NavController, public viewCtrl: ViewController, events: Events) {
this.events = events;
this.nav = nav;
this.jobModel = navParams.get('jobModel');
this.lastOnline = navParams.get('lastOnline');
this.personModelLoggedIn = navParams.get('personModelLoggedIn');
if (!this.jobModel) {
this.jobModel = new JobModel();
}
this.ratingModel = navParams.get('ratingModel');
}
presentFilterMap(event: Event) {
//this.viewCtrl.dismiss().then(() => {
this.nav.push(MapPage, {
jobModel: this.jobModel,
ratingModel: this.ratingModel,
fromSearch: true
});
//});
}
presentFilterCategories(event: Event) {
this.viewCtrl.dismiss().then(() => {
this.nav.push(CategoryPage, {
jobModel: this.jobModel,
ratingModel: this.ratingModel,
fromSearch: true
});
});
}
presentFilterRating(event: Event) {
//this.viewCtrl.dismiss().then(() => {
this.nav.push(ReviewPage, {
jobModel: this.jobModel,
ratingModel: this.ratingModel,
personModelLoggedIn: this.personModelLoggedIn,
fromFilter: true
});
//});
}
presentFilterTime(event: Event) {
this.viewCtrl.dismiss().then(() => {
this.nav.push(DateTimePage, {
lastOnline: this.lastOnline,
fromSearch: true
});
});
}
clearFilters() {
if (this.jobModel) {
this.jobModel.locations = [];
this.jobModel.categories = [];
this.jobModel.subCategories = [];
this.lastOnline = null;
}
if (this.ratingModel) {
this.ratingModel.rating = -1;
}
let data = {
jobModel: this.jobModel,
ratingModel: this.ratingModel,
fromClearFilters: true
};
this.nav.popToRoot().then(() => {
this.events.publish('popupFilter:update', data);
});
}
}
Error
The popover is not displayed, but the following error is displayed in the browser console:
PopoverCmp ionViewPreLoad error: No component factory found for SearchJobsPopOverPage
Any help appreciated.
Sill me, I forgot to define the page in app.module.ts as is required post rc0.
I am trying to make use of the Ionic DateTime component. I have the following code, but nothing displays (just the label and button). If anyone can advise, I would appreciate it.
datetime.html
<ion-header>
<ion-navbar>
<ion-title>Last Online</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<center>
<ion-spinner icon="android" *ngIf="loading"></ion-spinner>
</center>
<ion-row>
<ion-col>
<center>
<ion-label>Last Online</ion-label>
<ion-datetime displayFormat="h:mm A" pickerFormat="h mm A" [(ngModel)]="event.timeStarts"></ion-datetime>
</center>
</ion-col>
</ion-row>
<ion-buttons>
<button (click)="done()" block round class="form-button-text">{{'Done'}}</button>
</ion-buttons>
</ion-content>
datetime.ts
import { Component } from '#angular/core';
import { NavController, NavParams, Events } from 'ionic-angular';
import { JobModel } from '../model/jobModel';
#Component({
templateUrl: 'build/pages/datetime/datetime.html'
})
export class DateTimePage {
private loading: boolean = true;
private jobModel: JobModel = null;
private event: Events = null;
constructor(private nav: NavController, private navParams: NavParams, event: Events) {
this.event = event;
this.jobModel = navParams.get('jobModel');
if (!this.jobModel) {
this.jobModel = new JobModel();
}
this.loading = false;
}
private done(): void {
alert('todo');
}
}
The following works, it's a different date picker.
http://ionicframework.com/docs/v2/components/#datetime
I might sound old, but i am new for ionic 2. i wrote a code in ionic 2 for checkbox and it is working well but how do i retrieve the count of the checked boxes
Below is the code i have.
checklist.ts code
import { Component } from '#angular/core';
import { NavController, NavParams, Alert } from 'ionic-angular';
#Component({
templateUrl: 'build/pages/checklist/checklist.html',
})
export class ChecklistPage {
checklist:any;
constructor(public nav: NavController, public navParams: NavParams) {
this.checklist = this.navParams.get('checklist');
}
addItem(): void {
let prompt = Alert.create({
title: 'Add Item',
message: 'Enter the name of the task for this checklist below:',
inputs:[
{
name: 'name'
}
],
buttons:[
{
text: 'Cancel'
},
{
text: 'Save',
handler: data => {
this.checklist.addItem(data.name);
}
}
]
});
this.nav.present(prompt);
}
toggleItem(item): void {
this.checklist.toggleItem(item);
}
removeItem(item): void {
this.checklist.removeItem(item);
}
renameItem(item): void {
let prompt = Alert.create({
title: 'Rename Item',
message: 'Enter the new name of the task for this checklist below:',
inputs: [
{
name: 'name'
}
],
buttons: [
{
text: 'Cancel'
},
{
text: 'Save',
handler: data => {
this.checklist.renameItem(item,data.name);
}
}
]
});
this.nav.present(prompt);
}
checkallItems(): void {
this.checklist.items.forEach((item) => {
if(!item.checked){
this.checklist.toggleItem(item);
}
});
}
uncheckItems(): void {
this.checklist.items.forEach((item) => {
if(item.checked){
this.checklist.toggleItem(item);
}
});
}
}
checklist.html
<ion-header>
<ion-navbar secondary>
<ion-title>{{checklist.title}} #(checkeditem)/{{checklist.items.length}}</ion-title>
<ion-buttons end>
<button (click)="checkallItems()">
<ion-icon name="checkbox"></ion-icon>
</button>
<button (click)="uncheckItems()">
<ion-icon name="refresh-circle"></ion-icon>
</button>
<button (click)="addItem()">
<ion-icon name="add-circle"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list no-lines>
<ion-item-sliding *ngFor="let item of checklist.items" class="home-sliding-item">
<ion-item>
<ion-label>{{item.title}}</ion-label>
<ion-checkbox [checked]="item.checked" (click)="toggleItem(item)" class="checklist-item"></ion-checkbox>
</ion-item>
<ion-item-options>
<button light (click)="renameItem(item)">
<ion-icon name="clipboard"></ion-icon>Edit
</button>
<button danger (click)="removeItem(item)">
<ion-icon name="trash"></ion-icon>Delete
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
You can add a method in your ChecklistPage like this:
public getItemsCount(): number {
let count= 0;
for(let i=0; i<this.checklist.items.length; i++) {
if(this.checklist.items[i].checked){
count++;
}
}
return count;
}
Or following the checkallItems() method:
public getItemsCount(): number {
let count= 0;
this.checklist.items.forEach((item) => {
if(item.checked){
count++;
}
});
return count;
}
And then in your view:
<ion-title>{{checklist.title}} #{{getItemsCount()}}/{{checklist.items.length}}</ion-title>