0

レコードをグループ化する単純なページを作成し、ボタンを追加して一部のレコードを削除しようとしています。

問題は、削除された同じ名前のレコードが間違ったグループ化されたリストから削除されることです。また、リストにグループ化されたレコードがない場合は消え、代わりに常にそこにあります。

フィドル: http://jsfiddle.net/Tropicalista/qyb6N/15/

// create a deferred object to be resolved later
var teamsDeferred = $q.defer();

// return a promise. The promise says, "I promise that I'll give you your
// data as soon as I have it (which is when I am resolved)".
$scope.teams = teamsDeferred.promise;

// create a list of unique teams
var uniqueTeams = unique($scope.players, 'team');

// resolve the deferred object with the unique teams
// this will trigger an update on the view
teamsDeferred.resolve(uniqueTeams);

// function that takes an array of objects
// and returns an array of unique valued in the object
// array for a given key.
// this really belongs in a service, not the global window scope
function unique(data, key) {
    var result = [];
    for (var i = 0; i < data.length; i++) {
        var value = data[i][key];
        if (result.indexOf(value) == -1) {
            result.push(value);
        }
    }
    console.log(result)
    console.log(Math.ceil(result.length / 10))
    $scope.noOfPages = Math.ceil(result.length / 10);
    return result;
}

$scope.currentPage = 1;
$scope.pageSize = 5;
$scope.maxSize = 2;

$scope.deleteItem = function(item){
    //code to delete here
    var index=$scope.players.indexOf(item)
    $scope.players.splice(index,1);
};  
4

4 に答える 4

2

これは、SpykeBytes からのヒントで拡張されたもののサンプルです。

      <div ng-repeat="location in journey.locations">
                <div id="location_div_{{ $index }}">
                    <label class="journey-label">Location name</label>
                    <input class="journey-input" id="location_{{ $index }}" type="text" ng-model="location.location_name" />
                    <button ng-show="editable" tabindex="-1" class="journey-button remove" ng-click="removeItem(journey.locations, $index)">
                        Remove location
                    </button>

次に、コントローラーで、個々のアイテムを削除するアクションを設定します

$scope.removeItem = function(itemArray, index) {
  return itemArray.splice(index, 1);
};
于 2013-09-13T13:49:30.047 に答える
1

何もリストされていないときにグループを非表示にするには、フィルタリングされたリストを取得し、それを使用ng-showして表示を操作する必要があります。これは少しトリッキーです:

<div ng-show="currentList.length>0" ng-repeat="team in teams| startFrom:(currentPage - 1)*pageSize  | limitTo:pageSize | filter:searchInput"> <b>{{team}}</b>
    <li ng-repeat="player in (currentList = (players | filter: {team: team}))">{{player.name}}
        <button class="btn btn-small" type="button" ng-click="deleteItem(player)">Delete</button>
    </li>
</div>

ただし、間違ったグループから削除することについてあなたが言った問題は見られません。再現方法を教えていただけないでしょうか。

于 2013-09-13T13:56:28.663 に答える
0

オブジェクトが ng-repeat 内にある場合は、代わりにオブジェクトのインデックスを渡すことができます。

于 2013-09-13T13:31:29.170 に答える