search

Home  >  Q&A  >  body text

jquery - angularjs中如何根据表格数据中的值修改样式。

<p ng-controller='myCtrl'>
    <table>
    <tr ng-class='setStyle(item.grade)' ng-repeat='item in p'>
        <td>{{ item.name }}</td>
        <td>{{ item.sex }}</td>
        <td>{{ item.grade }}</td>
    </tr>
    </table>
</p> 
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.p=[
            {'name':'rose', 'sex':'girl', 'grade':67},
            {'name':'rose', 'sex':'girl', 'grade':130},
            {'name':'jack', 'sex':'boy', 'grade':40},
            {'name':'mike', 'sex':'boy', 'grade':34},
            {'name':'tom', 'sex':'boy', 'grade':80},
            {'name':'tom', 'sex':'boy', 'grade':100},
            {'name':'john', 'sex':'boy', 'grade':120},
            {'name':'john', 'sex':'boy', 'grade':170},
            {'name':'john', 'sex':'boy', 'grade':270},
            {'name':'john', 'sex':'boy', 'grade':310}
        ];
        $scope.setStyle = function(args){
            if(args>0 && args<=50){
                return 'colorOneF'
            }
            else if(args>50 && args<=100){
                return 'colorTwoF'
            }
            else if(args>100 && args<=150){
                return 'colorThreeF'
            }
            else if(args>150 && args<=200){
                return 'colorFourF'
            }
            else if(args>200 && args<=300){
                return 'colorFiveF'
            }
            else{
                return 'colorSixF'
            }
        }
    });
</script>
这是我目前实现的方法,希望大家能给出更多的方法 
为情所困为情所困2744 days ago510

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-15 17:08:47

    Use the ng-class directive to add different classes to td according to the value of grade. ng-class

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 17:08:47

    When using ng-class, do you need to write jq code in the controller? ng is data-driven and uses less DOM. If you must use DOM to operate, instructions are your paradise...

    reply
    0
  • Cancelreply