AngularJS sorting rows by table header

后端 未结 9 1898
旧巷少年郎
旧巷少年郎 2020-12-04 07:08

I have four table headers:

$scope.headers = [\"Header1\", \"Header2\", \"Header3\", \"Header4\"];

And I want to be able to sort my table by

相关标签:
9条回答
  • 2020-12-04 07:59

    I'm just getting my feet wet with angular, but I found this great tutorial.
    Here's a working plunk I put together with credit to Scott Allen and the above tutorial. Click search to display the sortable table.

    For each column header you need to make it clickable - ng-click on a link will work. This will set the sortName of the column to sort.

    <th>
         <a href="#" ng-click="sortName='name'; sortReverse = !sortReverse">
              <span ng-show="sortName == 'name' && sortReverse" class="glyphicon glyphicon-triangle-bottom"></span>
              <span ng-show="sortName == 'name' && !sortReverse" class="glyphicon glyphicon-triangle-top"></span>
               Name
         </a>
    </th>
    

    Then, in the table body you can pipe in that sortName in the orderBy filter orderBy:sortName:sortReverse

    <tr ng-repeat="repo in repos | orderBy:sortName:sortReverse | filter:searchRepos">
         <td>{{repo.name}}</td>
         <td class="tag tag-primary">{{repo.stargazers_count | number}}</td>
         <td>{{repo.language}}</td>
    </tr>
    
    0 讨论(0)
  • 2020-12-04 08:00

    I had found the easiest way to solve this question. If efficient you can use

    HTML code: import angular.min.js and the angular.route.js library

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>like/dislike</title>
    </head>
    <body ng-app="myapp" ng-controller="likedislikecntrl" bgcolor="#9acd32">
    <script src="./modules/angular.min.js"></script>
    <script src="./modules/angular-route.js"></script>
    <script src="./likedislikecntrl.js"></script>
    </select></h1></p>
    <table border="5" align="center">
    <thead>
    <th>professorname <select ng-model="sort1" style="background-color: 
    chartreuse">
    <option value="+name" >asc</option>
    <option value="-name" >desc</option>
    </select></th>
    <th >Subject <select ng-model="sort1">
    <option value="+subject" >asc</option>
    <option value="-subject" >desc</option></select></th>
    <th >Gender <select ng-model="sort1">
    <option value="+gender">asc</option>
    <option value="-gender">desc</option></select></th>
    <th >Likes <select ng-model="sort1">
    <option value="+likes" >asc</option>
    <option value="-likes" >desc</option></select></th>
    <th >Dislikes <select ng-model="sort1">
    <option value="+dislikes" >asc</option>
    <option value="-dislikes">desc</option></select></th>
    <th rowspan="2">Like/Dislike</th>
    </thead>
    <tbody>
    <tr ng-repeat="sir in sirs | orderBy:sort1|orderBy:sort|limitTo:row" >
    <td >{{sir.name}}</td>
    <td>{{sir.subject|uppercase}}</td>
    <td>{{sir.gender|lowercase}}</td>
    <td>{{sir.likes}}</td>
    <td>{{sir.dislikes}}</td>
    <td><button ng-click="ldfi1(sir)" style="background-color:chartreuse" 
    >Like</button></td>
    <td><button ng-click="ldfd1(sir)" style="background-
    color:chartreuse">Dislike</button></td>
    </tr>
    </tbody>
    </table>
    </body> 
    </html>
    JavaScript Code::likedislikecntrl.js
    
    var app=angular.module("myapp",["ngRoute"]);
    
    app.controller("likedislikecntrl",function ($scope) {
    var sirs=[
        {name:"Srinivas",subject:"dmdw",gender:"male",likes:0,dislikes:0},
        {name:"Sharif",subject:"dms",gender:"male",likes:0,dislikes:0},
        {name:"Chaitanya",subject:"daa",gender:"male",likes:0,dislikes:0},
        {name:"Pranav",subject:"wt",gender:"male",likes:0,dislikes:0},
        {name:"Anil Chowdary",subject:"ds",gender:"male",likes:0,dislikes:0},
        {name:"Rajesh",subject:"mp",gender:"male",likes:0,dislikes:0},
        {name:"Deepak",subject:"dld",gender:"male",likes:0,dislikes:0},
        {name:"JP",subject:"mp",gender:"male",likes:0,dislikes:0},
        {name:"NagaDeepthi",subject:"oose",gender:"female",likes:0,dislikes:0},
        {name:"Swathi",subject:"ca",gender:"female",likes:0,dislikes:0},
        {name:"Madavilatha",subject:"cn",gender:"female",likes:0,dislikes:0}
    
    
    
    ]
    $scope.sirs=sirs;
    $scope.ldfi1=function (sir) {
        sir.likes++
    
    }
    $scope.ldfd1=function (sir) {
       sir.dislikes++
    
    }
    $scope.row=8;
    
    })
    
    0 讨论(0)
  • 2020-12-04 08:03

    Here is an example that sorts by the header. This table is dynamic and changes with the JSON size.

    I was able to build a dynamic table off of some other people's examples and documentation. http://jsfiddle.net/lastlink/v7pszemn/1/

    <tr>
        <th class="{{header}}" ng-repeat="(header, value) in items[0]" ng-click="changeSorting(header)">
        {{header}}
      <i ng-class="selectedCls2(header)"></i>
    </tr>
    
    <tbody>
        <tr ng-repeat="row in pagedItems[currentPage] | orderBy:sort.sortingOrder:sort.reverse">
            <td ng-repeat="cell in row">
                  {{cell}}
           </td>
        </tr>
    

    Although the columns are out of order, on my .NET project they are in order.

    0 讨论(0)
提交回复
热议问题