简体   繁体   中英

Detecting an empty field of an input

I create a search bar. When the user presses the letter 'A' I want to display the users whose first name starts with the letter 'A'. This works, but my problem is when the search bar is empty. When it is empty, my program displays all the users, and I want it to show none.

Here is my program to obtain the value entered by the user :

searchUser(): void {
  let str;
  let element = document.getElementById('chat-window-input');

  if(element !== null) {
    str = (element as HTMLInputElement).value;
  }
  else {
    str = null;
  }
}

Do you know how to detect an empty field ? I tested this but it does not change anything :

if(str == null) {
    this.searchArrayThreads = [];
  }

Hi you can try like this,

 if(str == null || (!str) || str=='') {
  this.searchArrayThreads = [];
}

In your question it seems you have one array of data. so keep a tempDataSet

this.tempDataSet = []; then while filtering you can directly assign values.

this.str = '';
searchUser(): void {
  let element = document.querySelector('#chat-window-input');
  if(element && element.value) {
    return this.str = element.value;
  }
}

I think this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM