1

I'm currently trying to filter a table of users using pipes. I get it to work for 1 selection, but I'm trying to expand it to multiple selection. Keep in mind this is for only one column of the table. As of right now I have a column of users such as: Adam, Brenda, Dan, Harry. etc.. Keep in mind this column might multiple of the same name which I want to return as well. My current pipe implementation is as follows:

import * as _ from 'lodash';
import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
name: 'dataFilter'
})
export class DataFilterPipe implements PipeTransform {

transform(array: any[], query: string): any {
if (query) {
  return _.filter(array, row=>row.user.indexOf(query) > -1);
}
return array;
}
}

And my html is simply:

<table [mfData]="tableData | dataFilter : filterQuery" #mf="mfDataTable">

Where the filterQuery is just a string. Basically what I want to do is pass in an array of names to that filterQuery so it returns the values based on that. So lets say the rows of the user column had:

  • Adam, Adam, Brenda, Dan, Harry, Harry, Harry

and I passed in filterQuery ['Adam' , 'Harry'] I would get back:

  • Adam, Adam, Harry, Harry, Harry

Any help on modifying the code to help achieve this would be appreciated! I tried doing some loops in the transform function with the query: string[] but no luck yet.

Thanks!

1

1 Answer 1

1

I got to work via this pipe:

export class DataFilterPipe implements PipeTransform {
transform(array[], query:string[]):any[] {
if (typeof array === 'object') {
  var resultArray = [];
  if (query.length === 0) {
    resultArray = array;
  }
  else {
    resultArray = (array.filter(function (a) {
      return ~this.indexOf(a.user);
    }, query));
  }
  return resultArray;
}
else {
  return null;
  }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

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.