깜놀하는 해므찌로

Angular typescript 검색 기능 예시 ts / Date 날짜 비교 본문

IT

Angular typescript 검색 기능 예시 ts / Date 날짜 비교

agnusdei1207 2023. 4. 16. 03:08
반응형
SMALL
 searchCondition(): void {
    this.items = this.orderService.getItem();
    // 탭
    if (this.currentTab !== '전체') {
      this.items = this.items.filter(
        (_item) => _item.state === this.currentTab
      );
    }
    // 카테고리
    if (this.currentBranch !== '지점') {
      this.items = this.items.filter(
        (_item) => _item.branchName === this.currentBranch
      );
    }
    // 날짜
    if (this.startAt && this.endAt) {
      this.items = this.items.filter((_item) => {
        return (
          dayjs(_item.createdAt).isAfter(dayjs(this.startAt)) &&
          dayjs(_item.createdAt).isBefore(dayjs(this.endAt))
        );
      });
    } else if (this.startAt && !this.endAt) {
      this.items = this.items.filter((_item) => {
        return dayjs(_item.createdAt).isAfter(dayjs(this.startAt));
      });
    } else if (!this.startAt && this.endAt) {
      this.items = this.items.filter((_item) => {
        return dayjs(_item.createdAt).isBefore(dayjs(this.endAt));
      });
    }
    // 검색어
    if (this.searchKeyword) {
      this.items = this.items.filter((_item) => {
        return (
          _item.ordererName.includes(this.searchKeyword) ||
          _item.product.includes(this.searchKeyword)
        );
      });
    }
    this.setPage();
  }

1. filter 함수 사용시 {} 중괄호를 표기하여 안에 로직을 작성할 경우 반드시 boolean 값을 return 하라고 명시해야 합니다.

반응형
LIST

'IT' 카테고리의 다른 글

전화번호 정규식 자동 '-' hyphen 처리  (0) 2023.04.17
dayJs  (0) 2023.04.17
C언어 전역변수 활용 예시  (0) 2023.04.16
Ionic & component 데이터 이동 예시 on Angular  (0) 2023.04.15
LocalStorage 활용 예시 / Angular  (0) 2023.04.14