AngularJs #04 How To Use The orderBy Flter To Sorting Data With Two Methods ( Tutorial )

in #utopian-io7 years ago (edited)


Image Source

What Will I Learn?

I will learn how to sorting data in angularJS with the filter ' orderBy ' .

  • How to use the filter orderBy to sorting data .
  • What's the parameters of sorting first method .
  • What's the parameters of sorting second method .

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 the sorting data in AngularJS we use the orderBy filter , how ?
{{ orderBy_expression | orderBy : expression : reverse }} this is the binding expression of this filter so you choose the orderBy expression that you would to order , then the pipe sign ' | ' to create the filter , after the the name of filter as our case here orderBy , the expression that you want to do and it's ascending or descending order ..
Example : ng-repear = " employee in employees | orderBy : 'salary ' : false
So I have used the order by expression which is the ' employee in employees ' inside the ng-repeat directive , also the filter ' orderBy ' the parameter is the column salary and the reverse value is false , so we have two value of reverse parameter is boolean it will be true or false no other ..

  • To sort data in ascending order , set reverse to false .
  • To sort data in descending order , set reverse to true .
    There is other way to sort data you can use the ' + 'sign to sort data in ascending order or ' - ' sign to sort them in descending order ..
    Example : ng-repeat = " employee in employees | orderBy : '+salary' "
    As you see I have added the plus sign to the column to be in ascending order .
    Now I will to give you an example how to use the orderBy , firstly I want to create an object of employees has name , date of birth , gender and salary from the angularJS
    capture-20180206-144636.png
    And now I will import these informations by the binding expression in HTML after the relation of module and controller by the ng-repeat directive in a table ..
    capture-20180206-144733.png
    This is the result of the table , this is by default order as I created in AngularJS file
    capture-20180206-144759.png
    I want to sort data in ascending order , I will use the filter of orderBy with the column that I want and the reverse value ..
    capture-20180206-145709.png
    I have sort this data by name in ascending order , the reverse has the value false , by default if we take it empty it will have the value false , this is the result :
    capture-20180206-145839.png
    As you see the order has changed Ben stay the first but there is a change of places between Sara and Mark , I will now modify the reverse value to true .
    capture-20180206-150301.png
    Good , the value of reverse has changed to true before we see the result I think it will be in descending order
    capture-20180206-150322.png
    Great ! So the value of reverse if it's true we're in descending order , else in ascending order , by default is ascending .
    capture-20180206-150841.png
    This is the second method to sort by the sign ' + ' as I used here it will be in ascending order
    capture-20180206-150914.png
    Very nice the data sorted with ascending order , I will now use the ' - ' minus sign to sort data with descending order ..
    capture-20180206-151405.png
    As you see I used the minus sign and this is the result in descending order
    capture-20180206-151352.png
    Now I want to be dynamically I want to choose the column then , I choose the type of order and from my choice the data will sort
    capture-20180206-153248.png
    Here I have created a select option inside it I have added the ng-model to be dynamic with the angular and to use it in the HTML page , then the value if the column is ascending order I put just the name else if it's descending order I put the minus sign ' - ' .
    capture-20180206-153321.png
    I must link it with the $scope to be angular propriety and I gave it the " name " column by default .
    capture-20180206-153417.png
    In the first parameter I put the value of model which is from the angular it's dynamically the value of options that the user selected , this is the result :
    capture-20180206-153647.png
    This is the result sorting by name it's by default as I did in the angularJS page
    capture-20180206-153703.png
    This is the descending order of name column as I have created in options
    capture-20180206-153802.png
    Here I have selected the salary with ascending order
    capture-20180206-154113.png
    And this is the salary in descending order with the minus sign .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

  • In addition to being good at the teacher, he must look good at the same time. The pictures and the writings are almost side by side. They have to be at the bottom of the bottom or top. You can also use Markdown tools for a better look. I recommend you to be more attentive to professional content. Please keep in mind that if you make the edits I mentioned in this post, you are happy with your next posts.

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

Thank you , I will do it next post sure , thank you sir

Hey @sedatyildiz, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

@aymenz, Approve is not my ability, but I can upvote you.

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.16
TRX 0.15
JST 0.028
BTC 58182.09
ETH 2307.96
USDT 1.00
SBD 2.48