The angular filter is used in the

Source: Internet
Author: User
Tags array sort

The first parameter can have three types, namely: Function,string,array: The first type: function, if function, The function then iterates through the array to be sorted and compares the returned result as a comparator parameter in the angular library function. Second: If it is a string, if the array to be sorted is an object, it will be sorted according to the corresponding property value of each object in the array to be sorted. If the string has a "+", "-" symbol in front of it, then the + is the ascending sort, which means descending sort. If the string is empty, it is sorted by the element itself. The third: If it is an array, then it is a multi-attribute sort method for the second case. For example, if the parameter is [a, b], then the value of the A attribute will be sorted first, and if the A property value is the same, then the value of the property will be sorted according to. How to order the simple elements array by the way of ordering, we need to use the first parameter of the order as the function of this type: the definition function is: $scope. Sortsimpletype (OP) {return op;} The code in the page is: string array sort <div> {{[' Test ', ' Bob ', ' Market ', ' type ', ' Barrow '] | orderby:sortsimpletype}}</div> Sorted by: ["Barrow", "Bob", "Market", "Test", "type"] numeric array sort: <div> {[1,3434,2,1,12121,6,56,3] | orderby:sortsimplet Ype}}</div> sorting results are: [1,1,2,3,6,56,3434,12121]of course, the parameter type is a function-by-order filter and can also be used to sort the array of elements as objects. Simply return the property value of the object in the function. For example, <div> {{' name ': ' Nick ', ' age ': '},{' name ': ' Bob ', ' age ': ' + '}] | orderby:sortobjecttype}}</div>$scope. Sortobjecttype = function (obj) {return obj[' name ']}The result is: [{"Name": "Bob", "Age": "},{", "name": "Nick", "Age": "34"}]for this kind of need to sort by the object property, you can take the argument type as string, so as to take full advantage of the angular sorting characteristics, for the above example can be changed to:<div> {{' name ': ' Nick ', ' age ': ' The '},{' name ': ' Bob ', ' age ': ' All '}] ' | By: ' Name '}}</div>//Sort results as:[{"Name": "Bob", "Age": "},{"-"name": "Nick", "Age": "34"}].
 for multi-attribute sorting, a third strategy is used, with the parameter array type, such as: <div> {[{' name ': ' Nick ', ' age ': 34},{' name ': ' Nick ', ' age ': ' 23},{' name ': ' Bob ', ' age ': +}]] | orderby:[' name ', ' age ']}}</div>//Sort result: [{' Name ': ' Bob ', ' age ': 23},{' name ': ' Nick ', ' age ': 23},{' name ': ' Nick ', ' age ': 34}] The introduction of the use-by-the-end is over. So the order in angular inside is how to achieve the sort of, need to delve into the angular code to make analysis.

By filter in angular

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.