具有多个参数和随机值的角度自定义排序

时间:2015-10-30 19:28:26

标签: javascript angularjs sorting

我知道以前曾多次询问过这种情况的各种变化,但是我遇到了麻烦。我有一个小角度的应用程序,我在比较两个角色(这是D& D倡议卷)。我希望在以下代码中对ng-repeat进行排序:

var dmTools = angular.module('dmTools', []);
dmTools.controller('initTracker', function($scope, $http) {

  var charInit = function() {
    this.name = "";
    this.mod = 0;
    this.init = 0;
  }

  $scope.characters = [

  ];

  $scope.character = new charInit();

  $scope.addChar = function() {
    $scope.characters.push($scope.character);
    $scope.character = new charInit();
  }

  $scope.deleteChar = function(character) {
    $scope.characters.splice($scope.characters.indexOf(character), 1);
  }
  $scope.charSort = function(a, b) {
    //first we go by initiative roll
    if (a.init < b.init) {
      return -1;
    }
    if (a.init > b.init) {
      return 1;
    }
    //if initiative rolls are the same, go by initiative mod
    if (a.mod < b.mod) {
      return -1;
    }
    if (a.mod > b.mod) {
      return 1;
    }
    //if both are the same, roll off until one gets a higher roll than the other.
    var aRoll = 0;
    var bRoll = 0;
    while (aRoll == bRoll) {
      aRoll = Math.floor(Math.random() * 20) + 1;
      bRoll = Math.floor(Math.random() * 20) + 1;
      if (aRoll < bRoll) {
        return -1;
      }
      if (aRoll > bRoll) {
        return 1;
      }
    }
    //while this should not be possible to reach, we'll put it in for safeties sake.
    return 0;
  };
  $scope.rollInit = function() {
    for (x in $scope.characters) {
      $scope.characters[x].init = Math.floor(Math.random() * 20) + 1 + $scope.characters[x].mod;
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="app" id="initTracker" data-ng-controller="initTracker">
  <h3>Initiative Tracker</h3>
  <div class="form-group">
    <label>Name
      <input type="text" data-ng-model="character.name" class="form-control" />
    </label>
  </div>
  <div class="form-group">
    <label>Mod
      <input type="number" data-ng-model="character.mod" class="form-control" />
    </label>
  </div>
  <div>
    <input class="btn btn-primary" style="margin:5px;" data-ng-click="addChar()" type="button" value="Add Character" />
    <input class="btn btn-success" style="margin:5px;" data-ng-click="rollInit()" type="button" value="Roll Initiative" />
  </div>

  <table class="table table-striped">
    <tr>
      <th>Name</th>
      <th>Modifier</th>
      <th>Initiative</th>
      <th>Delete</th>
    </tr>
    <tr data-ng-repeat="character in characters | orderBy: charSort">
      <td data-ng-bind="character.name"></td>
      <td data-ng-bind="character.mod"></td>
      <td data-ng-bind="character.init"></td>
      <td>
        <input type="button" class="btn btn-danger" value="Delete" data-ng-click="deleteChar(character)" />
      </td>
    </tr>
  </table>
</div>

我的charSort函数是一种工作类型,可以很容易地传递给array.sort(函数)类型的调用,但angular只传递1个字符对象到函数。如何在角度模板中获得基于此种类的正确自定义比较?

2 个答案:

答案 0 :(得分:1)

您是否需要在data-ng-repeat中对数组进行排序?您可以在rollInit()中对数组进行排序。

如果有任何内容通过插入或删除更改了数组,则可以在数组上使用$ watchCollection,并在添加或删除内容时重新排序。

然后你可以在Javascript中进行任何你喜欢的排序,而data-ng-repeat将始终以正确的顺序看到数据。

答案 1 :(得分:0)

如果您有一个对象的三个属性,您可以按属性A,属性B和属性C的优先级对它们进行排序,如下所示

<tr data-ng-repeat="character in characters | orderBy:['A','B','C']">