8

I have multiple button im trying to disable the button after getting the response.Im using ngClass to disable the button after the getting the response

<tr *ngFor="let item of data">

  <td>{{item.username}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.email}}</td>
                    <td>{{item.phone}}</td>
                   <td><button  type="button" [ngClass]="{disabled : item.username === name }" (click)="changestatus(item.username)" class="btn btn-success btn-xs" >Change Status</button></td>

</tr>

And my changestatus function

public name : string ;

 changestatus(username : string){

         this.httpService.changeuserstatus({uname : username }).subscribe(data => {

           this.name = (data.data == 1) ? username : "no";

         })

  }

im assigning the username value to the name variable and if both matches i will disable the button

The problem is if i click the 1st button after the getting response it is disabled but when i click the 2nd button the after getting response 1st button is enabled and the 2nd button becomes disabled.But the thing is need to disable both buttons. Help me thanks in advance

2
  • Possible duplicate of: stackoverflow.com/questions/38618463/… Commented Jul 10, 2017 at 11:58
  • Try this: [ngClass]="{disabled : item.temp || (item.username === name && item.temp = true) }" Commented Jul 10, 2017 at 12:02

3 Answers 3

8

@maciej-caputa is correct regarding the use of [disabled] rather than a class, but your error is actually due to your application logic.

Your function changestatus() updates the global variable this.name. This will affect all rows, since their disabled state is conditional on 'item.username === name'

Try the following - I'm assuming that item is of a type called User (to which you will need to add a new property isDisabled:

Model:

export class User {
  username: string;
  name: string;
  email: string;
  phone: string;
  isDisabled: boolean;
}

component.html:

<tr *ngFor="let item of data">
    <td>{{item.username}}</td>
    <td>{{item.name}}</td>
    <td>{{item.email}}</td>
    <td>{{item.phone}}</td>
    <td><button  type="button" [disabled]="item.isDisabled" (click)="changestatus(item)" class="btn btn-success btn-xs" >Change Status</button></td>
</tr>

component.ts:

    changestatus(user: User){
        this.httpService.changeuserstatus({uname : user.username }).subscribe(data => {
            user.isDisabled = (data.data == 1);
        });
    }

Very simple plunkr demonstrating a working version: https://plnkr.co/edit/quLBCMoFtragJ32OBTvp

Sign up to request clarification or add additional context in comments.

2 Comments

i have multiple buttons and if click the 1st button it should be disable and i click the 2nd button it should be disabled without enabling the 1st button and your answer enables the 1st button on clicking the 2nd button
@ArunKumaresh There were some typos before - The plunkr demonstrates a working version using this approach
6

To really have disabled button you need to use disabled attribute on button element otherwise you will only change styles but still be able to click it. In angular2 you can bind to an attribute as follows.

<button [disabled]='item.username === name'></button>

4 Comments

this what i have also tried but gives the same result what i mentioned.The problem is every time the name variable gets the new value
You can move item.username === name to a public method on a component and then call <button [disabled]='isDisabled()'></button> it's a good practise and might help you double check the logic. If you need to style it then refer to it by button:disabled (:disabled pseudo class).
note i have i multiple buttons what you saying will work when there is single buttons
Add a class to it, e.g. <button class='original-button' [disabled]='item.username === name'></button> and then in css .original-button:disabled.
0

html:

<button type="button"
        (click)="changestatus(item.username)"
        [disabled]='isDisabled(item)' 
        class="btn btn-success btn-xs">Change Status</button>

TS :

isDisabled(item) : boolean {
 return item && item.username === name;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.