MVC,AngularJS,可排序,按最新创建的节点排序

时间:2015-03-24 11:12:19

标签: c# angularjs model-view-controller

我有一个解决方案,我可以在节点列表中创建一个节点。然后将这些节点保存在我的数据库中。如果我在同一会话中创建三个节点然后更新站点,它会随机对节点进行排序。是否可以按最新创建的节点进行排序,因此当我更新页面时,第一个创建的节点位于顶部,最后一个创建的节点位于底部?

我通过角度调用每次调用数据库。这是我的js文件:

(function () {
var domainName = 'Match';

angular.module('FightCardModule').controller("FightCardController",
    ['$scope', '$modal', '$http', '$routeParams', 'guidGenerator', 'eventBus', domainName + "Service", 'MatchModelFactory',
    function ($scope, $modal, $http, $routeParams, guidGenerator, eventBus, domainService, modelFactory) {

        $scope.eventId = $routeParams.eventId;
        $scope.id = $routeParams.id;


        $scope.refreshList = function (eventId) {
            var serviceUrl = "/api/Match" + (eventId ? "/" + eventId : "");

            $http({
                method: 'GET',
                url: serviceUrl
            }).success(function (data, status, headers, config) {
                $scope.itemsList.nodes = buildHierarchy(data);
            }).error(function (data, status, headers, config) {

            });
        }

        $scope.itemsList = {
            nodes: [],
        };

        $scope.sortableOptions = {
            containment: '#sortable-container',
            accept: function (sourceItemHandleScope, destSortableScope) {
                return sourceItemHandleScope.itemScope.sortableScope.$id === destSortableScope.$id;
            }
        };

        $scope.addNote = function (data) {
            newNode = createNewNode();
            newNode.EventId = $scope.eventId;
            $scope.itemsList.nodes.push(newNode);

            var serviceUrl = "/api/" + "Match";

            var cleanModel = {};
            cleanModel.Id = newNode.Id;
            cleanModel.EventId = newNode.EventId;
            cleanModel.ParentMatchId = newNode.ParentMatchId;
            cleanModel.Player1= newNode.Player1;

            $http({
                method: 'POST',
                url: serviceUrl,
                data: cleanModel
            }).success(function (data, status, headers, config) {
            });
        }

        $scope.alertDelete = function (data) {
            $modal.open({
                templateUrl: 'openAlertBox.html',
                scope: $scope,
                controller: function ($scope) {
                    $scope.ok = function () {

                        var indexToRemove;
                        var Nodes;

                        Nodes = $scope.itemsList.nodes;

                        for (var i = 0; i < Nodes.length; i++) {
                            if (Nodes[i].Id == data.Id) {
                                indexToRemove = i;
                                break;
                            }
                        }
                        Nodes.splice(indexToRemove, 1);
                        eventBus.publish(domainName + ".delete.submit", { model: data });
                        $scope.$dismiss();

                    }
                    $scope.cancel = function () {
                        $scope.$dismiss()
                    }
                }
            })
        }

        function createNewNode(parentNode) {
            var newNodeModel = new modelFactory.DomainModelConstructor();
            if (parentNode) {
                newNodeModel.ParentMatchId = parentNode.Id;
                newNodeModel.EventId = parentNode.EventId;
            }

            var newNode = convertModelToViewModel(newNodeModel);
            newNode.parent = parentNode;
            return newNode;
        }

        function convertModelToViewModel(nodeModel) {
            nodeModel.nodes = [];
            return nodeModel;
        }

        function buildHierarchy(matches) {

            var roots = [], children = {};
            for (var i = 0, len = matches.length; i < len; ++i) {
                var item = convertModelToViewModel(matches[i]);
                if (item.EventId == $scope.eventId){
                roots.push(item);
            }
            }

            return roots;
        }

        $scope.editMatchUsingModal = function (data) {
            $modal.open({
                templateUrl: 'openMatchBox.html',
                scope: $scope,
                controller: function ($scope) {
                    $scope.player1= data.Player1;
                    $scope.ok = function (player1) {
                        $scope.edit(data, player1);

                        $scope.$dismiss();
                    }
                    $scope.cancel = function () {
                        $scope.$dismiss();
                    }
                }
            })
        }

        $scope.edit = function (data, player1) {
            var newNode = {};
            newNode.Player1= player1;

            data.Player1= player1;

            eventBus.publish(domainName + ".edit.submit", { model: newNode });

        };

        $scope.refreshList($scope.eventId);
        $scope.refreshList($scope.id);

    }]);

})();

1 个答案:

答案 0 :(得分:1)

请查看following link,了解如何使用orderBy对列表进行排序。

顺便说一句 您不必更新您的页面..尝试使用$ watch