簡體   English   中英

如何在用戶搜索之前對用戶隱藏所有數據

[英]How to hide all the data from the user until user search for it

我創建了一個自定義過濾器搜索框,它工作正常,但我不想提前顯示所有數據,而是想在用戶搜索之前隱藏它。 請建議我應該如何實現那件事。

在這里我分享我的代碼:

  1. searchFilter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
import { Pincodes } from '../classes/pincodes';

@Pipe({
  name: 'searchfilter'
})
export class SearchfilterPipe implements PipeTransform {

  transform(Pincodes: Pincodes[], searchValue: string): Pincodes[] {
    if (!Pincodes || !searchValue) {
      return Pincodes
    }
    return Pincodes.filter(pincode =>
      pincode.taluka.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.village.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.district.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.pincode.toString().toLowerCase().includes(searchValue.toLowerCase())
    )
  }

}

  1. 注冊頁面.component.ts
import { Component, OnInit } from '@angular/core';
import { Pincodes } from '../classes/pincodes';
import { MetaDataService } from '../services/meta-data.service';
import { FormBuilder, FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { startWith } from 'rxjs-compat/operator/startWith';
import { FormGroup } from '@angular/forms';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
  selector: 'app-register-page',
  templateUrl: './register-page.component.html',
  styleUrls: ['./register-page.component.css']
})
export class RegisterPageComponent implements OnInit {
  observeData: Pincodes[]
  modifiedText: any;
  PinSelected: any = {}
  searchValue: string

  constructor(private _metaDataAPI: MetaDataService) {

  }

  ngOnInit() {
    this._metaDataAPI.getData().subscribe(data => {
      this.observeData = data;
    });

  }

  onPincodeSelected(val: Pincodes) {
    console.log("value" + val);
    this.customFunction(val)

  }
  customFunction(val: Pincodes) {
    // this.modifiedText = "The Selected Pin :  " + val.pincode +
    //  " and selected District is " + val.village
    this.modifiedText = "Selected Pin : " + val.pincode + "\n" +
      "District : " + val.district + "\n" +
      "Taluka : " + val.taluka


    console.log("Modified Text: " + this.modifiedText);
    console.log("Pincode Selected: " + this.PinSelected);
    console.log("observeData Selected: " + this.observeData);

  }


}

  1. 注冊頁面.component.html
<div class="wrapper">
  <div class="main_content">
    <div class="header">
      <strong><b>Tell us more about you</b></strong>
    </div>
    <div class="info">
      <h3>Basic Details</h3>


    <div class="form-group row col-md-4 mb-3">
      <label for="search" class="col-sm-2 col-form-label">Pincode*</label>
      <div class="col-sm-6">
        <input
          type="text"
          [(ngModel)]="searchValue"
          class="form-control"
          id="search"
        />
      </div>
    </div>
    <!-- Table started -->
    <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col">Pincode</th>
          <th scope="col">Village</th>
          <th scope="col">Taluka</th>
          <th scope="col">District</th>
          <th scope="col">State</th>
          <th scope="col">Tick</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let pin of observeData | searchfilter: searchValue">
          <td scope="col">{{ pin.pincode }}</td>
          <td scope="col">{{ pin.village }}</td>
          <td scope="col">{{ pin.taluka }}</td>
          <td scope="col">{{ pin.district }}</td>
          <td scope="col">{{ pin.state }}</td>
          <td scope="col">
            <mat-checkbox class="example-margin"></mat-checkbox>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

在此處輸入圖像描述

如您所見,它提前顯示了所有數據,我想隱藏這些數據。

將您的 PIPE 更改為:-

import { Pipe, PipeTransform } from '@angular/core';
import { Pincodes } from '../classes/pincodes';

@Pipe({
  name: 'searchfilter'
})
export class SearchfilterPipe implements PipeTransform {

  transform(Pincodes: Pincodes[], searchValue: string): Pincodes[] {
    if (!Pincodes || !searchValue) {
      return [];
    }
    return Pincodes.filter(pincode =>
      pincode.taluka.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.village.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.district.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.pincode.toString().toLowerCase().includes(searchValue.toLowerCase())
    )
  }

}

如果搜索值或密碼不存在,我們基本上會返回空白數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM