AngularJS implementing double-click Sorting

Source: Internet
Author: User



Key code: HTML


<th class = "col-md-3"> <a href="" ng-click="desc(‘2‘,la=!la)"> Unit price </a> </ th>
  <th class = "col-md-3"> <a href="" ng-click="desc(‘3‘,la=!la)"> sales amount </a> </ th>
  <th class = "col-md-1"> <a href="" ng-click="desc(‘4‘,la=!la)"> Number of sales </a> </ th>


Where La=!la is used to determine whether the current click is True or False



JS Code


// The default is the totalnum field in descending order

$ scope.foodsale = ret.sort (function (x, y)
                {
                    return y.totalnum-x.totalnum; // This means according to the totalnum field in ret [receives the returned array] in descending order, otherwise return x.totalnum-y.totalnum; ascending order
})


 $ scope.desc = function (fla, bol) {
            if (fla == "4") {
                if (bol == false) {
                 
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                    {
                        return y.totalnum-x.totalnum;
                    })
                } else {
                    console.log ("bbb")
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                    {
                        return x.totalnum-y.totalnum;
                    })
                }
            } else if (fla == "3") {// totalmoney
                if (bol == false) {
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                    {
                        return y.totalmoney-x.totalmoney;
                    })
                } else {
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                        {
                            return x.totalmoney-y.totalmoney;
                        }
                    )
                }
            } else if (fla == "2") {// price
                if (bol == false) {
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                    {
                        return y.price-x.price;
                    })
                } else {
                    $ scope.foodsale = $ scope.foodsale.sort (function (x, y)
                        {
                            return x.price-y.price;
                        }
                    )
                }
            } 

The above code can realize double-click sorting



AngularJS implementing double-click Sorting


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.