Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic3 enable/disable button programmatically when ion list is full/empty

I have a searchbar for which I check for input events and then filter firebase data.
This is the html code:

<ion-searchbar (ionInput)="getItems($event)" placeholder="Add tag ... "></ion-searchbar>
<ion-list>
<ion-item *ngFor="let tag of tagList">
<h2> {{ tag.name }} </h2>
</ion-item>
</ion-list>

and this is the ts code:

getItems(searchbar) {
 // Reset items back to all of the items
this.initializeItems();

 // set q to the value of the searchbar
var q = searchbar.srcElement.value;

// if the value is an empty string don't filter the items
if (!q) {
return;
}

this.tagList = this.tagList.filter((v) => {
if(v.name && q) {
  if (v.name.toLowerCase().indexOf(q.toLowerCase()) > -1) {
    return true;
  }
  return false;
}
 });

}

This code works well, but now I would like to enable a button whenever the list loaded on the view is empty, while keeping it disabled when there's at least one element loaded. The code for the button is this one:

  <button ion-button [disabled]="!isEnabled">Add tag</button>

I change the value of isEnabled to true or false in the getItems() method, this way:

if (this.tagList.length==0){
console.log('No elements ')
this.isEnabled=true;
  } else {
    console.log('Elements ')
    this.isEnabled=false;
  }

but the button remains disabled (when first entering the page, isEnabled is marked as false by default).
The logs are shown correctly when I write something in the searchbar, that is whenever I enter a letter the console outputs "elements" or "no elements" depending if the list has elements or not, but the button remains disabled.
How do I solve that? Am I missing something?

EDIT: here's the code where I set isEnabled :

getItems(searchbar) {
  // Reset items back to all of the items
  this.initializeItems();

  // set q to the value of the searchbar
  var q = searchbar.srcElement.value;
  this.textSearch = q;

  // if the value is an empty string don't filter the items
  if (!q) {
    return;
  }

  this.tagList = this.tagList.filter((v) => {
  if(v.name && q) {
     if (v.name.toLowerCase().indexOf(q.toLowerCase()) > -1) {
     return true;
  }
  return false;
   }
 });

 if (this.tagList.length==0){
   this.isEnabled = true;
 } else{
   this.isEnabled = false;
 }
}
like image 866
Usr Avatar asked Jun 21 '17 11:06

Usr


People also ask

How do I disable the button if the input box is empty and enable when field is filled?

How do you disable button until all fields are entered? Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do you turn off ionic ion button?

Using the :disabled CSS selector to target a disabled ion-button will not work as it only works on activable/focusable elements like a button or an input , instead, you have to use [disabled] (I don't think relying on the . button-disabled class set by Ionic is a good practice).

How do I turn off PrimeNG button?

It is of the boolean datatype, the default value is true. Angular PrimeNG Focus Trap Disabled Button properties: disabled: It specifies the button to be disabled if the value is set to true. The default value is false.


1 Answers

As we talked about in the comments, please edit your post to include the entire method where you set the isEnabled property so we can figure out what could be going on, but in the meantime, replace

<button ion-button [disabled]="!isenabled">Add tag</button>

by

<button ion-button [disabled]="tagList && tagList.length > 0">Add tag</button>

or what would be equivalent by using the elvis operator:

<button ion-button [disabled]="tagList?.length > 0">Add tag</button>
like image 104
sebaferreras Avatar answered Oct 12 '22 05:10

sebaferreras