【问题标题】:remove item from json click over again从json中删除项目再次单击
【发布时间】:2015-05-21 12:41:27
【问题描述】:

我曾经问过如何更准确地从 json 中删除一个项目:Angular remove item from json

它运行良好.. 现在我遇到了同样的问题,但代码有点不同。总之,我有一个清单。当我单击一个项目时,它会将其属性添加到 json 中。我希望如果我再次单击此项目,它会从 json 中删除其属性。因此,我单击 Item1 并添加,再次单击 Item1 并删除。

这是我尝试过的

myApp.controller("mycontroller", ["$scope", "$http",
    function($scope, $http){
        $scope.getItems = {
    "data": [
        {
            "label": "first",
            "objects": [
                {
                    "name": "firstObj",
                    "attributes": [
                        {
                            "attrname": "Item1",
                            "attrValue": "",
                            "attrType":"text"
                        },
                        {
                            "attrname": "Item2",
                            "attrValue": "",
                            "attrType":"text"
                        }
                    ]
                }
            ],
            "key": "bolla"
        }
    ]

};
    $scope.filterSelected = $scope.getItems.data[0].objects;

        $scope.myNewArray = {
            objects: [

            ]
        }

        $scope.createjson = function(attribute, items) {
            var obj = {};
            obj.name = attribute;
            obj.attributes = [];
            obj.attributes.push(items);
            return obj;
        }

        $scope.checkIfAttributeExists = function(attribute) {        
            for(var i=0; i<$scope.myNewArray.objects.length; i++) {                
                if($scope.myNewArray.objects[i]["name"] == attribute) {
                    return i;
                }
            }
            return -1;
        }

        $scope.pushItems = function pushItems(attribute, items) {
            var index = $scope.checkIfAttributeExists(attribute);
            if(index == -1) {
                var obj = $scope.createjson(attribute, items);
                $scope.myNewArray.objects.push(obj);
            } else if(index !== -1) {

                $scope.myNewArray.objects.splice(index, 1);

            }else {
                $scope.myNewArray.objects[index].attributes.push(items);
            }
        }

        $scope.showNewJson = function() {
            return $scope.myNewArray;
        }
}]);

似乎问题出在索引中。在这里我创建了一个 jsFiddle:

更新链接: https://jsfiddle.net/vuqcopm7/53/

其实这就是代码的作用:

{"objects":[{"name":"firstObj","attributes":[{"attrname":"Item1","attrValue":"","attrType":"text"}]},{"name":"firstObj","attributes":[{"attrname":"Item1","attrValue":"","attrType":"text"}]},{"name":"firstObj","attributes":[{"attrname":"Item2","attrValue":"","attrType":"text"}]}]}

如您所见,属性“Item1”有两次,因为我在它上面单击了两次。但是,如果我单击两次,Item1 就会消失!所以正确的json是:

{"objects":[{"name":"firstObj","attributes":[{"attrname":"Item2","attrValue":"","attrType":"text"}]}]}

【问题讨论】:

  • 对您的代码的一点建议:您的函数定义是 $scope.checkIfAttributeExists = function(attribute) { ...,而实际调用是 "var index = $scope.checkIfAttributeExists(items); "太混乱了!
  • 我更新了fiddle的链接
  • 不要将属性值传递给函数,而是使用整个对象。您的代码远比需要的复杂。还要了解json 是什么。您不是在创建或使用 json,而是在使用 javascript 对象和数组。 json 只是一种类似于 javascript 对象和数组的字符串数据传输格式
  • 该数组是我将发送到服务器的内容。但我必须发送我需要的内容。因此,选择的每个项目都有 1 个属性。不多。明白了吗?
  • 尝试查看我对任务的小编辑

标签: javascript jquery json angularjs


【解决方案1】:

您可以大大简化您的代码,如下所示:

HTML

 <ul ng-repeat="item in att.attributes">
      <li>
           <a ng-click="pushItems(item)">{{item.attrname}}</a>
      </li>
 </ul>

JS

$scope.pushItems = function pushItems(item) { // note only single argument now
    // index the whole item object
    var itemIndex = $scope.myNewArray.objects.indexOf(item);
    if (itemIndex == -1) {
        // if index doesn't exist ...add to array
        $scope.myNewArray.objects.push(item);
    } else {
        // otherwise remove from array
        $scope.myNewArray.objects.splice(itemIndex, 1);
    }
}

通过使用对象本身,您无需查看对象的属性来检查它是否存在,而是检查实际对象的索引

DEMO

【讨论】:

【解决方案2】:
var index = $scope.checkIfAttributeExists(attribute);

你传递了错误的参数。

【讨论】:

  • 如果我通过 var index = $scope.checkIfAttributeExists(attribute);如果我再次单击它会删除该项目,真的..但即使我单击第二个项目它也会删除该项目。
  • 如果您单击不同的项目(Item1、Item2、Item3 ...),它们会添加到 json。但只能是其中之一。因此,如果您再次单击 Item1,您将获得如下列表: (Item2, Item3..) 没有第一个。
  • 我想要我点击的那个项目的单个属性。
  • 有什么帮助吗?现在明白我的问题了吗?
猜你喜欢
  • 1970-01-01
  • 2017-09-10
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多