1

I'm trying to search for the fname, mname and lname together. Right now, i can only search by fname, mname and lname but not together.

Example: I'm trying to search for "Jacob jj9eqwif Nguyen" but it doesnt work. But try to search for Jacob and it works. Try to search for jj9eqif and it works. Try to search for Nguyen and it works.

Pls see this link

CLICK HERE

     search(event) {
        const val = event.target.value.toLowerCase();

        if (!val) {
          this.data = this.tempData;
        }

        const temp = this.tempData.filter(row => {
          return Object.keys(row).some(property => {
            if (property === 'received_by') {
              return row[property].fname
                .toString()
                .toLowerCase()
                .indexOf(val) !== -1
                ? row[property].fname
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1
                : row[property].mname
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1
                ? row[property].mname
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1
                : row[property].lname
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1
                ? row[property].lname
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1
                : (row[property].fname + row[property].lname + row[property].mname)
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1;
            }
            if (property === 'warehouse') {
              return (
                row[property].name
                  .toString()
                  .toLowerCase()
                  .indexOf(val) !== -1
              );
            } else {
              return row[property] === null
                ? null
                : row[property]
                    .toString()
                    .toLowerCase()
                    .indexOf(val) !== -1;
            }
          });
        });

        this.data = temp;
      }
1
  • can you elaborate search for the fname, mname and lname together with some example search text? Commented Oct 1, 2019 at 4:24

3 Answers 3

1

There is typo in your code you are searching for fname,mname,lname but you connect your string fname+lname+mname but lname should be on last position so change

(row[property].fname + row[property].lname + row[property].mname)
                .toString()
                .toLowerCase()
                .indexOf(val) !== -1;

to

(row[property].fname + row[property].mname + row[property].lname)
                .toString()
                .toLowerCase()
                .indexOf(val) !== -1;

Also if you want to search condition could be more simpler if you don't use trinory oprator here exmp

row[property].fname.toString()
            .toLowerCase().includes(val)
           || row[property].mname.toString()
                .toLowerCase().includes(val)|| row[property].lname
                .toString().toLowerCase().includes(val) || (row[property].fname + row[property].mname + row[property].lname)
                .toString()
                .toLowerCase()
                .includes(val);

As @Sangram Nandkhile pointed out Consider the space between fname, mname and lname.Your condition should be

(row[property].fname +' '+ row[property].mname +' ' row[property].lname)

Demo

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

4 Comments

Consider the space between fname, mname and lname. If you search Jacob jj9eqwif Nguyen, it will not return anything. Better to use (row[property].fname +' '+ row[property].mname +' ' row[property].lname)
@SangramNandkhile There's no need to do a searching in fname/mname/lname individually. If you're combining the string and then searching in that string, that condition alone will work for individual cases too.
@jitender. One more thing, if you just type "jj9eqwif Nguyen", it should also appear
@Joseph add one more or condition demo stackblitz.com/edit/filter-multiple-efvzhw
1

Let's start with your code,

You used Object.key(row) inside filter function then you only check for 2 fixed properties; recieved_by and warehouse, so you get object keys and loop through them which consumes unnecessary process.

Also, you used multiple ternary operator; which will be confusing, but all you need to use || (or) operator.

That's my solution:

const temp = this.tempData.filter(row => {
  return (
    row.warehouse.name.toLowerCase().indexOf(val) !== -1 ||
    (
      row.received_by.fname + ' ' +
      row.received_by.mname + ' ' +
      row.received_by.lname
    ).toLowerCase().indexOf(val) !== -1
  );
});

But it inside search function, it should work properly.

Check that in DEMO.

7 Comments

Keep in mind that "includes" on string doesn't work in IE, indexOf is always a good option
Okay, just replace .includes(val) with indexOf(val) !== -1
one more thing, your code doesn't work for the case asked in the question. It will still not return if you type "Jacob jj9eqwif Nguyen". You're only checking the fields individually and not together.
Okay, i added it
no need for individual cases of fname/mname/lname. Combining the strings and searching in them cover these cases automatically.
|
0

Concatenate the strings from which you want to search and look for the searched value in the concatenated string.

let temp = this.tempData.filter(data => {
    let combinedData = (data.received_by.fname + " " + data.received_by.mname + " " + data.received_by.lname + " " + data.warehouse.name).toLowerCase();
    return combinedData.indexOf(val) >= 0;
}

If there are more columns and you want to search across all of them, you could use JSON.stringify to convert the entire data into string and search in that string.

let temp = this.tempData.filter(data => {
    let combinedData = JSON.stringify(data).toLowerCase();
    return combinedData.indexOf(val) >= 0;
});

I have modified your fork here - https://stackblitz.com/edit/filter-multiple-wujuvz

3 Comments

One more thing, if you just type "jj9eqwif Nguyen", it should also appear
Doesn't make sense to add warehouse to the combined string; so if i search for name 'Jacob jj9eqwif Nguyen GG' it will return value, but there's no rows with that name!
@Joseph I think the statement in the question to search for combined result is vague and doesn't give an exact idea on what you want to achieve, instead it should say that any combination of fname/mname/lname should work. Also what about ordering of fname/mname/lname during search, for e.g. what about "Nguyen jj9eqwif", should that work too? Please clarify the scenarios in details.

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.