Saya mencoba memahami filterPredicate dari MatTableDataSource, dan ketika saya pikir saya sudah dekat, saya kehilangan beberapa logika.

Saya ingin memfilter melalui sumber data dan jika nilai array kosong atau "", maka seharusnya tidak memfilter untuk setiap nilai yang didefinisikan sebagai "". Dengan kata lain, filter dengan apa yang diketahuinya dan bukan apa yang tidak diketahuinya.

Saya mencoba menetapkan nilai ke nol jika panjang array sama dengan 0. Tetapi itu pun tidak berhasil.

Naskah Ketik

this.registeredUserService.GetAllAdverts().subscribe(val => {
          this.dataSource = new MatTableDataSource<Card>(val);
          this.dataSource.paginator = this.paginator;
          this.dataSource.filterPredicate = (myObject: IFilter, filterString: any) => {
            let filterObj: IFilter = JSON.parse(filterString);
            if (!filterObj.provinceName.includes(myObject.provinceName) ||
              !filterObj.vehicleMake.includes(myObject.vehicleMake) ||
              !filterObj.vehicleModel.includes(myObject.vehicleModel) ||
              !filterObj.vehicleYear.includes(myObject.vehicleYear) ||
              !filterObj.vehicleColor.includes(myObject.vehicleColor))
            {
              return false;
            }
            else {
              return true;
            }
          }

    filter()//whenever triggered, it should do the filtering
    {
        this.myFilter.provinceName = this.search.value.provinceSelector;
        this.myFilter.vehicleMake = this.search.value.makeSelector;
        this.myFilter.vehicleModel = this.search.value.modelSelector;
        this.myFilter.vehicleColor = this.search.value.colorSelector;
        this.myFilter.vehicleYear = this.search.value.yearSelector;

        if (this.myFilter.provinceName.length == 0 &&
          this.myFilter.vehicleMake.length == 0 &&
          this.myFilter.vehicleModel.length == 0 &&
          this.myFilter.vehicleColor.length == 0 &&
          this.myFilter.vehicleYear.length == 0) {
          this.dataSource.filter = '';
        }

        else {
          this.dataSource.filter = JSON.stringify(this.myFilter);
        }
      }

    myFilter: IFilter = {
        provinceName: [],
        vehicleMake: [],
        vehicleModel: [],
        vehicleColor: [],
        vehicleYear: []
      }

    interface IFilter{
      provinceName:any[],
      vehicleMake:any[],
      vehicleModel:any[],
      vehicleColor:any[],
      vehicleYear:any[]
    }

Apa yang harus dilakukan: Filter berdasarkan permintaan saya

Fungsinya: Hanya memfilter segera setelah semua nilai terisi.

0
MrTheBarintokyo 23 Oktober 2019, 18:44

1 menjawab

Jawaban Terbaik

Anda hanya perlu memeriksa atribut filter sebelumnya jika ada dan panjangnya lebih besar 0 dan kapan mencarinya di objek Anda.

this.registeredUserService.GetAllAdverts().subscribe(val => {
  this.dataSource = new MatTableDataSource<Card>(val);
  this.dataSource.paginator = this.paginator;
  this.dataSource.filterPredicate = (myObject: IFilter, filterString: any) => {
    let filterObj: IFilter = JSON.parse(filterString);
    if (
      (filterObj.provinceName && filterObj.provinceName.length > 0 && !filterObj.provinceName.includes(myObject.provinceName)) ||
      (filterObj.vehicleMake && filterObj.vehicleMake.length > 0 && !filterObj.vehicleMake.includes(myObject.vehicleMake)) ||
      (filterObj.vehicleModel && filterObj.vehicleModel.length > 0 && !filterObj.vehicleModel.includes(myObject.vehicleModel)) ||
      (filterObj.vehicleYear && filterObj.vehicleYear.length > 0 && !filterObj.vehicleYear.includes(myObject.vehicleYear)) ||
      (filterObj.vehicleColor && filterObj.vehicleColor.length > 0 && !filterObj.vehicleColor.includes(myObject.vehicleColor))
     ) {
       return false;
     } else {
       return true;
     }
   }
});
1
JuNe 24 Oktober 2019, 05:36