1

I have one checkbox filter that does work. but when I try to make another set of filters it doesn't allow it. the second filter just doesn't do anything. I'm using a predicate in both filters as there isn't a ready to use filter for checkbox.

This is my Component :

export class ContainersPageComponent implements OnInit {

  tableColumn = ['AITOR','SOG_MCOLH','GOBH_MCOLH','AORKH_MCOLH','MCOLH_N7','MCOLH_AAAA', 'TAOR_QTSR_EBRI','QOD_MCHSN','STTOS_RASHI_4_1',
'LQOCH_SHM_MQOTSR_EBRI','LQOCH_SHM_LOEZI_QTSR','LQOCH_QOD_LQOCH'];

  p :number = 1;
  dataSource : any = new MatTableDataSource;
  public show:boolean = false;
  public tableHide:any = 'Show';
  searchText: string = "";
  autoFilter : any;
  clientType : string;
  storageType: any;
  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;


  filterCheckboxes: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);
  filterParamasBoxes : BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);

  constructor(private marinService:MarinServiceService,private route: ActivatedRoute) { }

  ngOnInit() {
    this.marinService.getAllContainers().subscribe((result) => {
     //Data
      this.dataSource = new MatTableDataSource(result);
      //Paginator
      this.dataSource.paginator = this.paginator;
      //AutoFilter Form 1st page
      this.clientType = this.route.snapshot.queryParamMap.get('clientType');
      this.storageType= this.route.snapshot.queryParamMap.get('storageTypes');
      console.log('The Client name is : '+this.clientType+'  '+'The storage Facility is : '+this.storageType);
      //CheckBox Filter
      this.dataSource.filterPredicate = (data: Container, filter: any) => {
        return filter.split(',').every((item: any) => data.SOG_MCOLH.indexOf(item) !== -1);
      };
      this.filterCheckboxes.subscribe((newFilterValue: any[]) => {
        this.dataSource.filter = newFilterValue.join(',');
      });
    });
    this.dataSource.filterPredicate = (data : Container,filterParam : any) => {
      return filterParam.split(',').every((paramItem: any) => data.LQOCH_SHM_LOEZI_QTSR.indexOf(paramItem) !== -1);
    };
    this.filterParamasBoxes.subscribe((paramFilterValue: any[]) => {
      this.dataSource.filterParam = paramFilterValue.join(',');
    })
  }



  toggle(){
    this.show = !this.show;

    if(this.show) {
      this.tableHide = "Hide";
    } else {
      this.tableHide = "Show";
    }
  }

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

  public getRowsValue(flag) {
    if (flag === null) {
      return this.dataSource.length;
    } else {
      return this.dataSource.filter(i => (i.state == flag)).length;
    }
  }

  addFilter(change: MatCheckboxChange) {
    if (this.filterCheckboxes.value.some((a: any) => a === change.source.value)) {
      this.filterCheckboxes.next(this.filterCheckboxes.value.filter((a: any) => a !== change.source.value));
    } else {
      this.filterCheckboxes.next(this.filterCheckboxes.value.concat(change.source.value));
    }
  }
  paramFilter(changeParam: MatCheckboxChange){
    if (this.filterParamasBoxes.value.some((a: any) => a === changeParam.source.value)) {
      this.filterParamasBoxes.next(this.filterParamasBoxes.value.filter((a: any) => a !== changeParam.source.value));
    } else {
      this.filterParamasBoxes.next(this.filterParamasBoxes.value.concat(changeParam.source.value));
    }
}

This is my HTML :

<mat-checkbox  class="CheckBoxClass" value="MRR" (change)="paramFilter($event)">MRR</mat-checkbox>

  <div>
    <mat-table [dataSource]="dataSource"  [hidden]="!show" matSort >
      <!-- Location  -->
      <ng-container matColumnDef="AITOR">
        <mat-header-cell *matHeaderCellDef> Location </mat-header-cell>
        <mat-cell *matCellDef="let container"> {{container.AITOR}} </mat-cell>
      </ng-container>
          <!-- Type  -->
          <ng-container matColumnDef="SOG_MCOLH">
            <mat-header-cell *matHeaderCellDef mat-sort-header > Container Type </mat-header-cell>
            <mat-cell *matCellDef="let container"> {{container.SOG_MCOLH}} </mat-cell>
          </ng-container>
              <!-- Height  -->
              <ng-container matColumnDef="GOBH_MCOLH">
                <mat-header-cell *matHeaderCellDef> Container Height </mat-header-cell>
                <mat-cell *matCellDef="let container"> {{container.GOBH_MCOLH}} </mat-cell>
              </ng-container>    
              <!-- Length  -->
              <ng-container matColumnDef="AORKH_MCOLH">
              <mat-header-cell *matHeaderCellDef> Container Lenght </mat-header-cell>
              <mat-cell *matCellDef="let container"> {{container.AORKH_MCOLH}} </mat-cell>
              </ng-container> 
               <!-- Number  -->
               <ng-container matColumnDef="MCOLH_N7">
                <mat-header-cell *matHeaderCellDef> Container Number </mat-header-cell>
                <mat-cell *matCellDef="let container"> {{container.MCOLH_N7}} </mat-cell>
                </ng-container> 
                    <!-- Name AAAA  -->
                    <ng-container matColumnDef="MCOLH_AAAA">
                      <mat-header-cell *matHeaderCellDef > Container Name </mat-header-cell>
                      <mat-cell *matCellDef="let container"> {{container.MCOLH_AAAA}} </mat-cell>
                      </ng-container>  
                     <!-- QTSR Hebrow  -->
                     <ng-container matColumnDef="TAOR_QTSR_EBRI">
                     <mat-header-cell *matHeaderCellDef> Storage Site </mat-header-cell>
                     <mat-cell *matCellDef="let container"> {{container.TAOR_QTSR_EBRI}} </mat-cell>
                     </ng-container>   
                                  <!-- Storage Code  -->
                                  <ng-container matColumnDef="QOD_MCHSN">
                                    <mat-header-cell *matHeaderCellDef> Storage Code </mat-header-cell>
                                    <mat-cell *matCellDef="let container"> {{container.QOD_MCHSN}} </mat-cell>
                                    </ng-container>
                                 <!-- Status  -->
                                  <ng-container matColumnDef="STTOS_RASHI_4_1">
                                    <mat-header-cell *matHeaderCellDef> Status </mat-header-cell>
                                    <mat-cell *matCellDef="let container"> {{container.STTOS_RASHI_4_1}} </mat-cell>
                                    </ng-container>      
                                  <!-- MQOTSR Hebrow  -->
                                  <ng-container matColumnDef="LQOCH_SHM_MQOTSR_EBRI">
                                    <mat-header-cell *matHeaderCellDef> Customer MQOTSR </mat-header-cell>
                                    <mat-cell *matCellDef="let container"> {{container.LQOCH_SHM_MQOTSR_EBRI}} </mat-cell>
                                    </ng-container>
                                  <!-- MQOTSR Hebrow  -->
                                  <ng-container matColumnDef="LQOCH_SHM_LOEZI_QTSR">
                                    <mat-header-cell *matHeaderCellDef> ESN </mat-header-cell>
                                    <mat-cell *matCellDef="let container"> {{container.LQOCH_SHM_LOEZI_QTSR}} </mat-cell>
                                    </ng-container>
                                  <!-- MQOTSR Hebrow  -->
                                  <ng-container matColumnDef="LQOCH_QOD_LQOCH">
                                    <mat-header-cell *matHeaderCellDef> Code Customer </mat-header-cell>
                                    <mat-cell *matCellDef="let container"> {{container.LQOCH_QOD_LQOCH}} </mat-cell>
                                    </ng-container>  

Added stackblitz link - https://stackblitz.com/edit/marin-project-stackblitz Do note that yo wont see any data as its an internal API service.

I have tried changing values and nothing seems to work. I have tried moving the filter inside the NgOnInit

4
  • Could you please add stackblitz example ? Commented Jul 7, 2019 at 9:04
  • do you have only checkboxes or will there be a possibility of having a search filter as well? Commented Jul 7, 2019 at 9:08
  • @AmitBaranes i dont have a stackblitz example for this as im using an Internal API so you wont be able reproduce this (you cant access this API on an outside network). Commented Jul 7, 2019 at 9:16
  • @nash11 i don't want to use a search filter for this. i want to use the check box filter i have but for some reason it works with one option but not with the other. Commented Jul 7, 2019 at 9:17

1 Answer 1

1

There's some logical errors in your code. Where you are trying to add the filter to the filterPredicate(), you are instead rewriting the initially defined filterPredicate(). so your code must only be filtering through LQOCH_SHM_LOEZI_QTSR and not through SOG_MCOLH.

You can use ngModel or formControlName to bind to the value of the checkbox and call the applyFilter() function on change of the checkbox. I'm passing the filter as an object since it's easier to work with. Although, mat-table only accepts it as a string so you will need to use JSON.stringify().

I have attached a working example on StackBlitz.

Sign up to request clarification or add additional context in comments.

7 Comments

I have tried using your StackBlitz example and it didn't work for me. it just deleted the entire table when i pressed on the checkbox
That's weird. It's working fine for me. Do you see any error in the console?
No it just turns from 1400 results to 0
added the stackblitz link
Just to be clear. when you said my stackblitz example didn't work for you, did you mean you tried to implement the same thing in yours and it didn't work or that the stackblitz example I put up isn't working? Also Please provide some dummy data for the table in your stackblitz example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.