在ionic 2中单击时如何禁用动态列表中存在的按钮
how to disable a button which is present in a dyanamic list when clicked in ionic 2
<ion-card *ngFor="let product of products" no-padding>
<ion-item>
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="false">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<button [disabled]=disable color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
</ion-card>
这是我的
var disable=false;
addlist(name, id, price, qty, image) {
//it is disabling each button present in the list
this.disable=true;
}
在您的组件中制作 products
的 loop
并根据您的要求添加 disable
属性 condition
:
for (let product of products) {
product.disabled = false;
if(product.somefield == 'somevalue'){
product.disabled = true;
}
}
中html取button disabled
属性为,[disabled]="product.disabled"
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
问题很明显,您对数组中的每个项目使用相同的 disable
属性,您需要为数组中的每个项目使用特定的 disable
.
我看到了两种方法:
- 您可以遍历
products
并添加 disable
属性。如果它来自服务器并且您可以并且想要,您可以在那里添加 属性 这样您就不会在前端浪费一点时间。
- 您可以创建一个新的
disable
数组并使用您的 *ngFor 索引来访问每个项目的正确禁用。
第一个选项是这样的:
在您的 .ts 文件中,您将拥有:
// Let's say you're fetching your products in a lifecyclehook like ionViewDidLoad
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// let's use map to iterate through the products
this.products.map(item => {
// this'll create a property in every item of your array.
item.disabled = false;
})
})
}
addlist(name, id, price, qty, image, product) {
// you'll need to pass the entire object to your method, and then set the property to true.
product.disabled = true;
}
我本可以直接在响应上使用 map 或展开运算符,但让我们以最简单的为目标。
在你的html中:
<!-- All your card code. Change the disabled of the button and the select as bellow -->
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, product)">ADD</button>
<!-- Just pass the entire product in the end of the method -->
第二个选项:
在您的 .ts 中,您将创建一个新数组,并为产品中的每个项目推送一个新的布尔值:
export class YourPage {
// the new boolean array
public disabled: Array<boolean> = [];
// The method where you get the products, same as the first option
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// use a for to iterate through
for (let i = 0; i < this.products.length; i++){
this.disabled.push(false);
}
})
}
addlist(name, id, price, qty, image, index) {
// now you'll pass the index from the ngfor
// you'll access the corresponding disable from the array and set to true
this.disabled[index] = true;
}
}
然后在你的 html:
<!-- declare the index in your ngFor -->
<ion-card *ngFor="let product of products; let i = index" no-padding>
<ion-item>
<!-- change the disable to reflect the correct array position -->
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="disabled[i]">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<!-- Add the index in the end of your click method -->
<button [disabled]="disabled[i]" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, i)">ADD</button>
</ion-card>
希望对您有所帮助 :D
<ion-card *ngFor="let product of products" no-padding>
<ion-item>
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="false">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<button [disabled]=disable color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
</ion-card>
这是我的
var disable=false;
addlist(name, id, price, qty, image) {
//it is disabling each button present in the list
this.disable=true;
}
在您的组件中制作 products
的 loop
并根据您的要求添加 disable
属性 condition
:
for (let product of products) {
product.disabled = false;
if(product.somefield == 'somevalue'){
product.disabled = true;
}
}
中html取button disabled
属性为,[disabled]="product.disabled"
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
问题很明显,您对数组中的每个项目使用相同的 disable
属性,您需要为数组中的每个项目使用特定的 disable
.
我看到了两种方法:
- 您可以遍历
products
并添加disable
属性。如果它来自服务器并且您可以并且想要,您可以在那里添加 属性 这样您就不会在前端浪费一点时间。 - 您可以创建一个新的
disable
数组并使用您的 *ngFor 索引来访问每个项目的正确禁用。
第一个选项是这样的:
在您的 .ts 文件中,您将拥有:
// Let's say you're fetching your products in a lifecyclehook like ionViewDidLoad
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// let's use map to iterate through the products
this.products.map(item => {
// this'll create a property in every item of your array.
item.disabled = false;
})
})
}
addlist(name, id, price, qty, image, product) {
// you'll need to pass the entire object to your method, and then set the property to true.
product.disabled = true;
}
我本可以直接在响应上使用 map 或展开运算符,但让我们以最简单的为目标。
在你的html中:
<!-- All your card code. Change the disabled of the button and the select as bellow -->
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, product)">ADD</button>
<!-- Just pass the entire product in the end of the method -->
第二个选项:
在您的 .ts 中,您将创建一个新数组,并为产品中的每个项目推送一个新的布尔值:
export class YourPage {
// the new boolean array
public disabled: Array<boolean> = [];
// The method where you get the products, same as the first option
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// use a for to iterate through
for (let i = 0; i < this.products.length; i++){
this.disabled.push(false);
}
})
}
addlist(name, id, price, qty, image, index) {
// now you'll pass the index from the ngfor
// you'll access the corresponding disable from the array and set to true
this.disabled[index] = true;
}
}
然后在你的 html:
<!-- declare the index in your ngFor -->
<ion-card *ngFor="let product of products; let i = index" no-padding>
<ion-item>
<!-- change the disable to reflect the correct array position -->
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="disabled[i]">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<!-- Add the index in the end of your click method -->
<button [disabled]="disabled[i]" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, i)">ADD</button>
</ion-card>
希望对您有所帮助 :D