AngularJS orderBy反转数组而不是命令它

时间:2017-01-18 15:10:16

标签: javascript html angularjs angularjs-orderby

当我点击“订购”按钮'表格不是订购,它只是反转所有td。比如,' A',' C',' B'成为''' C'," A"。我真的不知道为什么会这样。

HTML

                        <!-- Top section -->
    <div class="row">
      <div class="col-lg-12"><img src="/img/AngularJS.svg" class="img-responsive"></div>
      <p class="text-center">поиск и сортировка данных</p>
    </div>

                        <!--Input and buttons-->
    <div class="container-fluid" >
        <div class="row-fluid">
            <input class="col-md-12 col-xs-12" type="text" placeholder="Search people by name..." ng-model='data.text' ng-pattern='data.regexp'>

            <div class="buttons">
                <button ng-click='data.sortType = "data.students.name"; sortReverse = !sortReverse'class="btn btn-sort">Sort by name</button>

                <button ng-click='data.sortType = "data.students.age"; sortReverse = !sortReverse' class="btn btn-sort">Sort by age</button>

                <button ng-click='data.reset()' class="btn btn-danger">Reset</button>
            </div>
        </div>


                        <!--Main content-->
        <div class="main-table col-sm-8  col-md-7 col-md-offset-1 col-lg-7 col-lg-offset-1">
            <table class="table table-hover">
                <thead>
                  <tr>
                    <th class="text-center">Image</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Phone</th>
                  </tr>
                </thead>

                <tbody ng-repeat='student in data.students | filter:data.text | orderBy : sortType:sortReverse'>

                  <tr ng-click='data.sideBar(student.img, student.name, student.age, student.phone)'>

                    <td><img ng-src='{{student.img}}'></td>
                    <td>{{student.name}}</td>
                    <td>{{student.age}}</td>
                    <td>{{student.phone}}</td>
                  </tr>
                </tbody>
            </table>
        </div>
    </div>

JS

(function(){
    var app = angular.module('app', []);


    app.controller('DataController', function() {
        this.students = arr;

        this.regexp = /^[A-z]+$/;

        this.sideBarImg = '/img/dog.svg';
        this.sideBarName = 'Chad Snyder';
        this.sideBarAge = 22;
        this.sideBarPhone = '8 (629) 653-9041';

        this.sortType = 'student.age';
        this.sortReverse = false;

        this.reset = function() {
            this.text = '';
        };

        this.sideBar = function(img, name, age, phone){
            this.sideBarImg = img;
            this.sideBarName = name;
            this.sideBarAge = age;
            this.sideBarPhone = phone;

        };


    });

    app.filter('favAnimal', function() {
        return function(animalName) {

            animalName = animalName.substring(0, animalName.indexOf('.'));
            var tmp = animalName.split('/');
            var result = tmp[tmp.length - 1];

            return result;
        }
    })


    arr = [
        {img: '/img/dog.svg', name: 'Chad Snyder', age: 22, phone: '8 (629) 653-9041'},
        {img: '/img/raccoon.svg', name: 'Milton Warner', age: 19, phone: '8 (366) 958-8850'},
        {img: '/img/fox.svg', name: 'Viola Hale', age: 35, phone: '8 (687) 456-4873'},
        {img: '/img/owl.svg', name: 'Tyler Herrera', age: 27, phone: '8 (537) 867-1647'},
        {img: '/img/sheep.svg', name: 'Gabriel Howell', age: 45, phone: '8 (332) 288-8294'},
        {img: '/img/cat.svg', name: 'Adelaide Jacobs', age: 32, phone: '8 (609) 383-7022'},
        {img: '/img/koala.svg', name: 'James Diaz', age: 21, phone: '8 (262) 812-4095'},
        {img: '/img/lion.svg', name: 'Vincent Sparks', age: 21, phone: '8 (475) 497-4172'},
        {img: '/img/owl.svg', name: 'Jackson Peterson', age: 23, phone: '8 (838) 992-3015'},
        {img: '/img/penguin.svg', name: 'Theresa Schwartz', age: 31, phone: '8 (215) 881-3278'},
        {img: '/img/pig.svg', name: 'Charles Carter', age: 39, phone: '8 (719) 862-9379'},
        {img: '/img/dog.svg', name: 'Curtis Briggs', age: 70, phone: '8 (255) 719-5400'},
        {img: '/img/raccoon.svg', name: 'Mollie French', age: 57, phone: '8 (668) 359-4293'},
        {img: '/img/koala.svg', name: 'Maurice Watson', age: 36, phone: '8 (368) 345-7784'},
        {img: '/img/lion.svg', name: 'Derrick Hill', age: 42, phone: '8 (413) 435-6719'}
        ];
}());

1 个答案:

答案 0 :(得分:0)

您的排序类型必须是属性名称。

java.lang.ClassCastException: poc.staticmethod.PocStaticTest$BResponse cannot be cast to poc.staticmethod.PocStaticTest$AResponse
    at poc.staticmethod.PocStaticTest$ClassUnderTest.execute(PocStaticTest.java:44)
    at poc.staticmethod.PocStaticTest.myTest(PocStaticTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:316)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:300)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:288)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:208)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:121)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:123)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)  

看一下这个see image我必须引入$ scope来运行应用程序。