Filtering and sorting based on Angularjs

Source: Internet
Author: User


The previous understanding of the use of ANGULARJS, here simply write a small program, the implementation of query filtering and sorting functions.

In this program you can learn:

1 Angularjs Filter

2 How to use Ng-repeat

3 Use of the controller

4 Binding of data

Programming analysis


First, if you first query the filter, you will use the filter in Angularjs.



Use the pipe command immediately after the expression | , you can achieve a filtering effect by following this notation:


{{persons | filter:query}}


By using filter for filtering, query is the string that is entered when the query is filtered.



Similarly, the ability to sort is accomplished by using the order by:


{{persons | filter:query | orderby:order}}


The above query and sort involves two variables, query and order. Here you can use Ng-model to implement data binding:


 Search:<input ng-model="query"> Sort by:<select ng-model="order">
                <option value="name">name</option>
                <option value="age">age</option>
            </select>





Angularjs is a DOM-based framework language, so there is no need to implement any listener and event triggers, and when the input box where the query is located changes, it triggers a two-way refresh of the input box and the expression below!



The implementation of ANGULARJS accelerates the presentation of models and views, compared to other frameworks, which are based on strings added to the DOM via DOM node innerHTML. and reduce a lot of unnecessary listener AH trigger Ah, such as the writing of code, really achieve a similar effect of spring ~






The presentation of the data can be achieved through ng-repeat . When the Web page resolves to ng-repeat , a copy of the tag is cloned for each element in the array for compilation parsing.





 <ul class="persons">
                <li ng-repeat="person in persons | filter:query | orderBy:order"> {{person.name}}
                    
                    {{person.age}} </li>
            </ul>





The rest of the work is to initialize the perons array in script:


<div ng-controller="ctl"> ... </div>
        <script type="text/javascript"> function ctl($scope){
                $scope.persons = [
                    {"name":"xingoo","age":25},
                    {"name":"zhangsan","age":18},
                    {"name":"lisi","age":20},
                    {"name":"wangwu","age":30}
                ];
                $scope.order = "age";
            } </script>   
Code and results


Finally, put all the code on it:


<!doctype html>
<html ng-app>
    <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>

    </head>
    <body>
        <div ng-controller="ctl">
            Search:<input ng-model="query">
            Sort by:<select ng-model="order">
                <option value="name">name</option>
                <option value="age">age</option>
            </select>

            <ul class="persons">
                <li ng-repeat="person in persons | filter:query | orderBy:order">
                    {{person.name}}
                    
                    {{person.age}}
                </li>
            </ul>
        </div>
        <script type="text/javascript">
            function ctl($scope){
                $scope.persons = [
                    {"name":"xingoo","age":25},
                    {"name":"zhangsan","age":18},
                    {"name":"lisi","age":20},
                    {"name":"wangwu","age":30}
                ];
                $scope.order = "age";
            }
        </script>
    </body>
</html>


View Code
Results:
By default, age is used for sorting:
Select to sort by name
When inputting characters again, some options will be filtered out through query automatically
Filtering and sorting based on angularjs


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.