How to retrieve data from a multi level object in Ionic 3? - arrays

This page works likes the following. The user selects a product (C2, Coca Cola, etc.) then the prices from the different supermarket are supposed to be displayed. This is the object I created.
productPrices = [
{
name: "C2 Green Tea",
prices: [
{
smPrice: 19.15,
tsPrice: 19.25,
rbPrice: 19.10
}
],
stock: 50
},
{
name: "Coca Cola",
prices: [
{
smPrice: 21.50,
tsPrice: 21.45,
rbPrice: 24.50
}
],
stock: 50
},
{
name: "Nature's Spring",
prices: [
{
smPrice: 12.50,
tsPrice: 11.50,
rbPrice: 13.00
}
],
stock: 50
},
{
name: "Red Horse Beer",
prices: [
{
smPrice: 31.50,
tsPrice: 29.50,
rbPrice: 30.90
}
],
stock: 50
},
];
getProductPrice(product: string){
return this.productPrices.filter(item => item.name == product);
}
By using the getProducPrice() method, I'm able to get the product selected and its properties. But I can't get the value of the prices. What I have on the template page is this.
<div *ngFor="let itemPrice of productPrices">
<div *ngFor="let marketPrice of itemPrice.prices">
<form (ngSubmit)="onFormSubmit(userForm)" #userForm="ngForm" >
<ion-list>
<ion-list-header>
Supermarkets
</ion-list-header>
<ion-item>
<ion-thumbnail item-start>
<img src="assets/grocery/market-logo/ts.jpg">
</ion-thumbnail>
<p>{{marketPrice.tsPrice}}</p>
<button ion-button clear item-end (click)="onBuy()" >Buy</button>
</ion-item>
<ion-item>
<ion-thumbnail item-start>
<img src="assets/grocery/market-logo/sm.png">
</ion-thumbnail>
<p>{{marketPrice.smPrice}}</p>
<button ion-button clear item-end (click)="onBuy()"> Buy</button>
</ion-item>
<ion-item>
<ion-thumbnail item-start>
<img src="assets/grocery/market-logo/rb.jpg">
</ion-thumbnail>
₱<input name="price" value="{{marketPrice.rbPrice}}">
<p>{{marketPrice.rbPrice}}</p>
<button ion-button clear item-end (click)="onBuy()">Buy</button>
</ion-item>
<ion-item>
<ion-label>Enter Quantity</ion-label>
<ion-input type="number" min="0"></ion-input>
</ion-item>
</ion-list>
</form>
</div>
</div>
The problem with that is that if I use a form to submit, it'll submit all the values including the prices of the other supermarkets.
Is there a better way for me to loop from the object I received and access the prices values of each product? I'm not using a DB nor is this file saved in a json file. I just want to know if there's a better way to get values at the typescript file by using some methods. Thank you.

try this you will property of selected product
<div *ngFor="let item of productPrices">
<button (click) ="getProductPrice(item)">get</button>
</div>
In your ts file
getProductPrice(product: any){
console.log(product);
}

Related

Print filtered array(object) ionic4/angular

I didn't understand how to pass data from .ts page to .html page in ionic angular.
Array:
this.s1mstatus = [
{
"code" : "01",
"descr" : "Text1."
},
{
"code" : "02",
"descr" : "Text2."
},
{
"code" : "03",
"descr" : "Text3."
}
]
HTML page
<ion-content fullscreen>
<ion-grid>
<ion-row>
<ion-col>
<ion-item [ngClass]="roundedInput" class="roundedInput">
<ion-input type="text" placeholder="Enter M-Status Code" [(ngModel)]="s1mstatus_get" maxlength="2"></ion-input>
<ion-button type="submit" (click)="mstatussend()">Submit</ion-button>
</ion-item>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-item class="roundedInput">
<ion-input type="text" placeholder="Enter M-Data Code" [(ngModel)]="s1mdata_get"></ion-input>
<ion-button type="submit" (click)="mstatussend()">Submit</ion-button>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
<ion-list *ngFor="let s1ms of s1filtered">
<ion-card class="card-border">
<ion-card-header>
<ion-card-subtitle>M-STATUS: {{s1ms.code}}</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<p>ERROR MESSAGE:</p>
<p class="ion-black" [innerHTML]="s1ms.descr"></p>
</ion-card-content>
</ion-card>
</ion-list>
</ion-content>
FUNCTION TO CHECK IF EMPTY AND "DO SOMETHING" IF ISN'T:
mstatussend() {
if(this.s1mstatus_get=="" || this.s1mstatus_get==undefined){
this.roundedInput = 'invalid';
}
else if(this.s1mstatus_get=="" || this.s1mstatus_get==undefined){
this.roundedInput = 'invalid';
}
else {
this.roundedInput = 'valid';
var s1filtered = this.s1mstatus.filter(element => element.code == this.s1mstatus_get);
console.log(s1filtered);
}
};
As You can see by the screenshot the console.log command works but I didn't understand how to print those values on the HTML page, maybe it's something easy but is driving me mad.
When you declare variable in your method, then this variable cannot be seen for HTML template:
var s1filtered = this.s1mstatus.filter(element => element.code == this.s1mstatus_get);
So you need to create a variable that can be seen for HTML template:
TypeScript:
s1filtered: any; // The variable that can be seen for HTML template
mstatussend() {
if(this.s1mstatus_get=="" || this.s1mstatus_get==undefined){
this.roundedInput = 'invalid';
}
else if(this.s1mstatus_get=="" || this.s1mstatus_get==undefined){
this.roundedInput = 'invalid';
}
else {
this.roundedInput = 'valid';
this.s1filtered = this.s1mstatus.filter(element => element.code == this.s1mstatus_get);
console.log(this.s1filtered);
}
};
HTML:
<ng-container *ngIf="s1filtered && s1filtered.length > 0">
<ion-list *ngFor="let s1ms of s1filtered">
<!-- The other code is omitted for the brevity -->
</ion-list>
<ng-container>

how to bind product price on product variation in ionic 3 and woocommerce

My list.html file this file contains the list of products I am fetching using Woocommerce API.Its all working good but I am not able to bind the variation price with each variation.Because in woocommerce each variation has its own id and price is given in that array.I am so confused how to get the price on its particular variation.
<ion-slide> <!--This slide is for fruits-->
<ion-card *ngFor="let product of products" no-padding>
<ion-item>
<ion-thumbnail item-start>
<img-loader *ngIf="product.images" src="{{product.images[1].src}}"
useImg>
</img-loader>
</ion-thumbnail>
<h4 top text-wrap>{{product.name}}</h4>
<p>Rs.{{product.price*product.quantity}}</p>
<ion-icon icon-only class="dec" name="remove-circle"
(click)="decrement(product)">
</ion-icon>
{{product.quantity}}
<ion-icon icon-only class="inc" name="add-circle"
(click)="increment(product)">
</ion-icon>
</ion-item>
<ion-item *ngFor="let attr of product.attributes">
<ion-select [(ngModel)]="qty" placeholder="QTY">
<ion-option *ngFor="let opt of attr.options" [value]="opt">
{{ opt }}
</ion-option>
</ion-select>
</ion-item>
<button color="light" class="addbtn" ion-button clear >add</button>
</ion-card>
</ion-slide>
this is my list.ts file I am using getAynsc() to get the data.
this.WooCommerce.getAsync('products?
category=34&per_page=10').then((result) => {
console.log(JSON.parse(result.toJSON().body));
this.products = JSON.parse(result.toJSON().body);
console.log(this.products);
this.productsvv.forEach(product => { product.quantity = 1; });
this.products.forEach(product => { product.quantity = 1; });
return JSON.parse(result.toJSON().body);
}, (err) => {
console.log(err)
})

How to show datas under the value in angular2/ionic2

home.ts
items = [];
constructor(public navCtrl: NavController) {
this.initializeItems();}
initializeItems() {
this.items = [
{
'title': 'Fiat',
"models" : [
{"model":"500L","value":1,"specs":"hatchback"},
{"model":"Linea","value":2,"specs":"sedan"}
]
}]
brand.html
<ion-list>
<ion-item>
<ion-label>Models</ion-label>
<ion-select id="secim" [(ngModel)]="values">
<ion-option *ngFor="let m of item.models" value="{{m.value}}">{{m.model}}</ion-option>
</ion-select>
</ion-item>
</ion-list>
<div class="" *ngIf="values==1">
<h1 *ngFor="let m of item.models">{{m.model}}:{{m.specs}}</h1>
</div>
<div class="" *ngIf="values==2" >
<h1 *ngFor="let m of item.models">{{m.model}}:{{m.specs}}</h1>
</div>
It's appear like this
500L:hatchback
Linea:sedan
but I want to do like this
(if select value=1)
500L:hatchback
(if select value=2)
Linea:sedan
How could I do? Thank you
Instead of what you are doing now, why not store the selected model to a variable, of which you can then show model and specs, so for example have a variable selectedModel:
<ion-item>
<ion-label>Models</ion-label>
<ion-select id="secim" [(ngModel)]="selectedModel">
<ion-option *ngFor="let m of item.models" [value]="m">{{m.model}}</ion-option>
</ion-select>
</ion-item>
And then just have one div to show the other properties:
<div *ngIf="selectedModel">{{selectedModel.model}}:{{selectedModel.specs}}</div>
DEMO: http://plnkr.co/edit/1xwhtwB5YjKHDbRzusHP?p=preview

radio button selection remove after click on other group of radio box

I am facing problem in two radio group box in ionic when i click on one group of radio the selected answer was saved after clicking second radio group first selected radio was removed
<div ng-app="ionicApp" ng-controller="HomeCtrl" style="padding:25px">
{{question | json }}
<div ng-repeat="q in question" >
<div class="list">
<div class="item item-divider">
{{q.question}}
</div>
<ion-radio ng-repeat="option in q.options" ng-value="option" ng-model="q.answer" >
{{ option }}
</ion-radio>
<br>
</div>
</div>
angular.module('ionicApp', ['ionic'])
.controller('HomeCtrl', function($scope) {
$scope.question =
[
{
"is_radio": true,
"question": "Roughly how much of your profile was spent on digital activities in your last job?",
"options": ["Less than 10%","Less than 25%","Less than 50%","More than 50%"],
"id": "130",
"answer": ""
},
{
"is_radio": true,
"question": "Have you spent more time with B2B or B2C?",
"options": ["B2C","Both","Less than 10%"],
"id": "130",
"answer": ""
}
]
})
Here is preview | jsfiddle
Please change following line of code
<ion-radio ng-repeat="option in q.options" ng-value="option" ng-model="q.answer">
With this
<ion-radio ng-repeat="option in q.options" ng-value="option" ng-model="q.answer" name="radio_{{$parent.$index}}" >
This will solve your issue. Problem was due to same name given to 2 radio button group. So I had changed that and given dynamically.
angular.module('ionicApp', ['ionic'])
.controller('HomeCtrl', function($scope) {
$scope.question =
[
{
"is_radio": true,
"question": "Roughly how much of your profile was spent on digital activities in your last job?",
"options": ["Less than 10%","Less than 25%","Less than 50%","More than 50%"],
"id": "129",
"answer": ""
},
{
"is_radio": true,
"question": "Have you spent more time with B2B or B2C?",
"options": ["B2C","Both","Less than 10%"],
"id": "130",
"answer": ""
}
]
})
<link href="http://code.ionicframework.com/1.0.0-beta.14/css/ionic.css" rel="stylesheet"/>
<script src="http://code.ionicframework.com/1.0.0-beta.14/js/ionic.bundle.js"></script>
<div ng-app="ionicApp" ng-controller="HomeCtrl" style="padding:25px">
{{question | json }}
<div ng-repeat="q in question" >
<div class="list">
<div class="item item-divider">
{{q.question}}
</div>
<ion-radio name="{{q.id}}" ng-repeat="option in q.options" ng-value="option" ng-model="q.answer" >
{{ option }}
</ion-radio>
<br>
</div>
</div>
</div>

How to make filter to be viewed in html

On my home page I added a button named 'city'. On clicking it opens a popup listing the city names. On clicking the city 'paris' the data with city name must be displayed.
homepage
<ion-header-bar class="bar bar-subheader">
<button class="button button-flat button-positive"ng-click="citypopup();" style="padding-right: 63px;
padding-left: 18px;">
<l >City </l>
<l class="icon ion-chevron-down"></l>
</button>
</ion-header-bar>
<ion-content>
<div class='card ' ng-repeat="item in list | filter:test.searchCity ">
<div class="list " >
<div class='item' style="padding-top:0px;" > {{item.id}}
<l class="item-icon-right" style="padding-left:30%"> {{item.date}} </l>
</div>
<div class='item' style="padding-top:0px;" >{{item.status }}
<l class="item-icon-right" style="text-align:right;">{{item.QCstatus}}</l>
<i class="icon ion-chevron-right"></i>
</div>
<b class='item '> {{item.Factory}} </b>
<l class='item '>{{item.city }}</l>
</div>
popup page
<ion-list>
<ul class="list" ng-model="test.searchCity" ng-repeat="ci in cityList | orderBy:'city' " >
<li class="item" ng-click="test.searchCity">{{ci.city}} </li>
</ul>
</ion-list>
javascript
$scope.test = {
searchCity: null,
};
$scope.cityList = [
{
"city": "Chennai"
}, {
"city": "Banglore"
}, {
"city": "Delhi"
}
];
$scope.list = [
{
id: "#001",
date:"DEC 04 2016",
status: "Successful",
QCstatus: "Approve",
Factory: "ARUN ICE CREAM",
city: "paris"
},
{
id: "#002",
date: "JAN 11 2016",
status: "Successful",
QCstatus: "Approve",
Factory: "Floxtronics",
city: "Delhi"
},
{
id: "#003",
date: "Feb 14 2016",
status: "Bug fixing",
QCstatus: "Approve",
Factory: "Aachi",
city: "Chennai"
}
];
$scope.$watch('test.searchCity', function () {
$scope.test.searchCity = null;
console.log('test.searchCity')
});
$scope.citypopup = function() {
var myPopup = $ionicPopup.show({
scope: $scope,
templateUrl: 'templates/cityTemplate.html',
buttons: [
{ text: 'Cancel',
type: 'button-positive' },
]
});
}
What I need is when I click 'city' button, my city popup appears and when I click on a city nothing happens! Could someone help me?
No need to create another popup page, you can open popup content in citypopup() function
$scope.citypopup = function() {
$scope.popup = $ionicPopup.show({
template:"<ion-list><ul class='list' ng-model='test.searchCity' ng-repeat='ci in cityList | orderBy:city ' ><li class='item' ng-click='searchCity(ci.city)'>{{ci.city}} </li> </ul></ion-list>" ,
title: 'City',
scope: $scope
});
}
$scope.searchCity = function(city){
$scope.test.searchCity = city;
$scope.popup.close();
}
You never assign test.searchCity.
In you popup page code you use ng-click="test.searchCity", where you probably should use ng-click="test.searchCity = ci.city".
Unrelated to your problem (but a matter of correct use of patterns): No need for ngModel in a ul element (it's only for form elements), and ng-repeat makes more sense when used on lis rather than uls.
In conclusion:
<ion-list>
<ul class="list">
<li class="item" ng-repeat="ci in cityList | orderBy:'city' " ng-click="test.searchCity = ci.city">{{ci.city}} </li>
</ul>
</ion-list>

Resources