AngularJs #07 How To filter by multiple properties in Angular JS ( Tutorial )

in #utopian-io8 years ago (edited)


Image Source

What Will I Learn?

I will learn how search by multiple proprieties in two different search boxes and in one search box.

  • How to search in two different search boxes by multiple proprieties .
  • How to search by one search box with all proprieties .
  • How to search by one search box with multiple proprieties ( function ) .

Requirements

  • Basic Knowledge of HTML and CSS language .
  • Basic Knowledge of JavaScript language .
  • Has an editor text support HTML and JavaScript .
  • Must has the angular files from the official site .

Difficulty

  • Basic

Tutorial Contents

In this tutorial we will discuss how to filter by mulitple proprieties , this is continuation to part 6 so please read the previous part to be ready , I will give you an idea what we will create two search boxes , search name by this box you can search by only the name and the city search box to search only the cities , also a check box named exact match the search will be an exact match , so what is the exact match search ?
For example if you take the search box name when we type something for example ' B ' the name has this letter will display , but if we click on exact match checkbox this column will not display because we haven't a column has just the letter ' b ' as name as a complete name but when we type the full name " ben " for example the column will display again , let's see how to do it
capture-20180216-120238.png
This is the same example that we created in the previous parts a table has 4 columns , the name , gender , salary and the city , I have used the ng-repeat " employee in employees to loop the columns " and this is the table in Angular JS
capture-20180216-120519.png
I have created an array named " employees " has 4 objects each one has 4 elements " Name , Gender , Salary and City " and I attached it with the $scope object , this is the result :
capture-20180216-121154.png
Now I will add a search box text by name and city to search by them by the ng-model attribute to be dynamically
capture-20180216-121505.png
I added a input to search , the type of this input is text to accept all sentences , also the attribute ng-model to be in the filter and to be dynamically , and placeholder to be more clear this is the result when I type the letter B
capture-20180216-121613.png
Now what I will do is to change the propriety to search by city , just I will change the propriety of the model by name , we try it ..
capture-20180216-122855.png
Here I just change the propriety of the ng-model attribute to searchText.city so I will search by city and this is the result :
capture-20180216-123025.png
As you see if I type the letter B and I want to search by name nothing will display because the propriety is the city , but if I type the letter P which is the first letter of Paris this is the result
capture-20180216-123052.png
Now I will create a multiple search by the name and the city and one of the exact match choice , let's see it ..
capture-20180216-123709.png
This is the two search boxes the first by name has the propriety of ng-model the name and the second has the city as propriety this is the result
capture-20180216-123552.png
I searched by the name by letter B it's Ben and the city by L it means London and the first column will appear
capture-20180216-123619.png
To prove to you that the search by city and name works successfully This is the result the table is empty as you see .
Now let's implement the " Exact Match " for that we need a check box it's an input with type="checkbox"
capture-20180216-132521.png
This is the check box input with a ng-model named exactMatch to link it with the filter of search it's a boolean value if we check it , it will be true else it will be false and I will attach it by the $scope , and we use this ng-model propriety in the filter now I will to apply it we use the " : " sign , and this is the result :
capture-20180216-133316.png
So this is the result I type the letter b in the search box of the name and the letter L in the search box of the city it's just a search by any letter in the table
capture-20180216-133407.png
Now if I click on the check box it gone because it's not a exact match but look now
capture-20180216-135922.png
If I complete typing the name and the city exactly like it is in the table this is the result , the first row display .
Now let's look to another example , the example is we have two search boxes each one search about specific propriety but it we have just one search box it will search in all columns all what you do is :
capture-20180216-140348.png
I remove the check box exact match and the second input of city , so just one input can search for all values in the table
capture-20180216-140437.png
Here just I type the first letter of Sara and this is the result , two values " Paris " and " Sara "
capture-20180216-140456.png
And I type the salary and this is the result it gave us the column of this salary , all what I want it's to give you the way to search by multi columns
The very important thing that I want to do is to search by multiple proprieties in one search box by city and name , let's see how to do it ..
capture-20180216-141612.png
I change the placeholder to search for city and name , also the filter I type search it's a variable I will link it with a function to search for the item that you type on the search box
capture-20180216-143915.png
Now I attached a function called search it's going to be a function with parameter ' item ' which is the element to search , Now if you look to the model propriety of the input search it's " searchText " so I check ' if ( the search text that I was typing is undefined ) the function will check for any element in the search box if you want to display just return a true else return a false , else if it's not undefined I will take the element by the item.name and convert it to lowerCase to facilitate the search operation and search into it by the indexOf function , int the parameter of this function I take the value of search box by the model " searchText " convert it to lowerCase and look at the result it's not equal to "-1" or no , if it not equals to -1 I will return true to display this element , this search can be by the name or " || " by this sign it means or , search by city the same code , else return false .
capture-20180216-144002.png
capture-20180216-144231.png
I type " Sar " name and the column of Sara appear also the London city and this is the result .
capture-20180216-144309.png
This is the result when you type undefined value .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for sharing

Welcome , thank you too for your reading

@aymenz, Upvote for supporting you.

Thank you sir

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you sir

Hey @aymenz I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.074
BTC 63547.44
ETH 1672.18
USDT 1.00
SBD 0.41