search

Home  >  Q&A  >  body text

angular.js - Click on the serial number in the angular table to sort in ascending or descending order. How to implement it.

<p ng-app="app" ng-controller="appController">
    <table>
        <thead>
        <tr>
            <th>序号</th>
            <th>内容</th>
            <th>姓名</th>
            <th>地点</th>
            <th>时间</th>
            <th ng-click="sort('id')">序号</th>
        </tr>
        </thead>
        <tbody>
            <tr ng-repeat="x in obj | orderBy: 'id'">
                <td ng-if="$index + 1 === 1" style="color: red">{{$index + 1}}</td>
                <td ng-if="$index + 1 === 2" style="color: green">{{$index + 1}}</td>
                <td ng-if="$index + 1 === 3" style="color: yellow">{{$index + 1}}</td>
                <td ng-if="$index + 1 > 3">{{$index + 1}}</td>
                <td>{{x.Name}}</td>
                <td>{{x.City}}</td>
                <td>{{x.Country}}</td>
                <td>{{x.Country + x.City}}</td>
                <td>{{x.id}}</td>
            </tr>
        </tbody>
    </table>
</p>
//js文件
<script>
    var app = angular.module('app', []);
    app.controller('appController', function($scope, $http) {
            //数据
            $scope.obj = [
                {
                    "Name": "Alfreds Futterkiste",
                    "City": "Berlin",
                    "Country": "Germany",
                    "id": 4
                },
                {
                    "Name": "Ana Trujillo Emparedados y helados",
                    "City": "México D.F.",
                    "Country": "Mexico",
                    "id": 3
                },
                {
                    "Name": "Comércio Mineiro",
                    "City": "São Paulo",
                    "Country": "Brazil",
                    "id": 2
                }, {
                    "Name": "Ana Trujillo Emparedados y helados",
                    "City": "México D.F.",
                    "Country": "Mexico",
                    "id": 5
                }
            ];
    });
</script>

How to sort by clicking on the serial number in ascending and descending order? I am a newbie, please give me the detailed code. Thank you all.

PHP中文网PHP中文网2810 days ago763

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-15 17:05:36

    htmlChange to:

    <p ng-app="app" ng-controller="appController">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>内容</th>
                <th>姓名</th>
                <th>地点</th>
                <th>时间</th>
                <th ng-click="toggleSort()">序号</th>
            </tr>
            </thead>
            <tbody>
                <tr ng-repeat="x in obj">
                    <td ng-if="$index + 1 === 1" style="color: red">{{$index + 1}}</td>
                    <td ng-if="$index + 1 === 2" style="color: green">{{$index + 1}}</td>
                    <td ng-if="$index + 1 === 3" style="color: yellow">{{$index + 1}}</td>
                    <td ng-if="$index + 1 > 3">{{$index + 1}}</td>
                    <td>{{x.Name}}</td>
                    <td>{{x.City}}</td>
                    <td>{{x.Country}}</td>
                    <td>{{x.Country + x.City}}</td>
                    <td>{{x.id}}</td>
                </tr>
            </tbody>
        </table>
    </p>

    js

    //js文件
    <script>
        var app = angular.module('app', []);
        app.controller('appController', function($scope, $http) {
        
                $scope.sortIsAsc = true;
                
                //数据
                $scope.obj = [
                    {
                        "Name": "Alfreds Futterkiste",
                        "City": "Berlin",
                        "Country": "Germany",
                        "id": 4
                    },
                    {
                        "Name": "Ana Trujillo Emparedados y helados",
                        "City": "México D.F.",
                        "Country": "Mexico",
                        "id": 3
                    },
                    {
                        "Name": "Comércio Mineiro",
                        "City": "São Paulo",
                        "Country": "Brazil",
                        "id": 2
                    }, {
                        "Name": "Ana Trujillo Emparedados y helados",
                        "City": "México D.F.",
                        "Country": "Mexico",
                        "id": 5
                    }
                ];
                
                $scope.toggleSort = function(){
                    $scope.sortIsAsc = !$scope.sortIsAsc;
                    $scope.obj.sort(function(a, b){
                        if($scope.sortIsAsc){
                            if (a.id < b.id) {
                                return -1;
                            }
                            if (a.id === b.id) {
                                return 0;
                            }
                            return 1;
                        }
                        if (a.id > b.id) {
                            return -1;
                        }
                        if (a.id === b.id) {
                            return 0;
                        }
                        return 1;
                    });
                };
        });
    </script>

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:05:36

    You can use the filter orderBy:'id':desc to control whether the sorting is ascending or descending order.
    The following code is easier to understand if you read it in the order of 3-2-1.
    1. Click once to change desc

    <th ng-click="desc=!desc">序号</th>
    

    2. Set desc initialization value = 0 in appController

    $scope.desc = 0;

    3. Add sorting conditions to the filter

    <tr ng-repeat="x in obj | orderBy: 'id':desc">

    reply
    0
  • Cancelreply