【问题标题】:MVC, AngularJS, Sortable, keep sort by latest created nodeMVC,AngularJS,Sortable,按最新创建的节点排序
【发布时间】:2015-05-27 16:08:26
【问题描述】:

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

我通过 Angular 将每次调用都拉到数据库中。这是我的 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);

    }]);

})();

【问题讨论】:

    标签: c# angularjs model-view-controller


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我去看看。
    • 好的,我已经读完了。如果我理解正确,那么我需要数据库中的 dateTime 变量才能按日期对节点进行排序?有没有办法在不添加日期时间的情况下使用 orderBy?
    • 我想通了——我在数据库中添加了一个 int,您可以在其中添加订单和使用的 orderBy。谢谢。
    猜你喜欢
    • 2014-09-09
    • 2013-02-18
    • 2017-05-26
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多