简体   繁体   English

Angular JS将空对象推送到数组

[英]Angular JS Push Empty Object To Array

I've been working a project that allows a user to manage Option Types and Options. 我一直在开发一个允许用户管理选项类型和选项的项目。 Basically user can add a new Option Type, let's say they name it Color and then they add the options - Black, Red, Purple, etc. When the collection first loads up the existing records, an empty option should be added at the end 基本上用户可以添加一个新的选项类型,假设他们将其命名为Color,然后他们添加选项 - 黑色,红色,紫色等。当集合首次加载现有记录时,最后应添加一个空选项

When a user starts typing in the text field, I want to add a new empty option , thereby always giving the user a new field to work with. 当用户开始在文本字段中键入内容时,我想添加一个新的空选项,从而始终为用户提供一个新字段。

I have this almost working, but can't figure how to properly add new empty option to a new Option Type or to existing option types. 我有这个几乎工作,但无法计算如何正确添加新的空选项到新的选项类型或现有的选项类型。 The push method keeps crashing Plunkr. push方法不断让Plunkr崩溃。 Any input is appreciated, short sample review of the plunkr is below 感谢任何输入,plunkr的简短样品审查如下

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

$scope.optionTypeId = 0;
$scope.productOptionId = 0;

$scope.productEditorModel = {
  "ProductOptions": [0],
  "OptionTypes": [0]
};

$scope.productEditorModel.optionTypeName = '';

$scope.addEmptyOption = function (optionTypeId) {

var emptyOption = { ProductOptionId: 3, ProductId: '1066', OptionTypeId: 1, OptionValue: '', Price: '', IsStocked: true };
console.log(emptyOption);
//$scope.productEditorModel.ProductOptions.push(emptyOption);
};

$scope.loadData = function () {

$scope.productEditorModel.OptionTypes = [{ OptionTypeId: 1, OptionName: 'Color' },{ OptionTypeId: 2, OptionName: 'Size' },];
  $scope.productEditorModel.ProductOptions = [{ ProductOptionId: 1, ProductId: '1066', OptionTypeId: 2, OptionValue: 'Medium', Price: '', IsStocked: true, },{ ProductOptionId: 2, ProductId: '1066', OptionTypeId: 1, OptionValue: 'Black', Price: '', IsStocked: true }];

angular.forEach($scope.productEditorModel.ProductOptions, function (item) {
      //console.log(item.OptionTypeId);
      $scope.addEmptyOption(item.OptionTypeId);
});
};

$scope.loadData();

$scope.removeOption = function (option) {
        var index =   $scope.productEditorModel.ProductOptions.indexOf(option);
        $scope.productEditorModel.ProductOptions.splice(index, 1);
};

$scope.filterEmptyElements = function (optionTypeId) {
$scope.emptyElements = $.grep($scope.productEditorModel.ProductOptions, function (k) { return k.OptionValue === "" || angular.isUndefined(k.OptionValue) && k.OptionTypeId == optionTypeId });
};

$scope.update = function (option, index) {
  var optionTypeId = option.OptionTypeId;
  $scope.filterEmptyElements(optionTypeId);

  if (!angular.isUndefined(option.OptionValue) && $scope.emptyElements.length == 1 && option.OptionValue.length > 0) {
      $scope.addOption(optionTypeId);
  } else if (angular.isUndefined(option.OptionValue)) {
      $scope.removeOption(option);
  }
}; 

$scope.addOptionType = function () {
  var optionTypeId = --$scope.optionTypeId;
  var optionName = $scope.productEditorModel.optionTypeName;
  var newOptionType = { OptionTypeId: optionTypeId, OptionName: optionName    };

  $scope.productEditorModel.OptionTypes.push(newOptionType);
  $scope.addEmptyOption(optionTypeId);
};

$scope.editOptionType = function (optionType) {
  $scope.editing = true;
};

$scope.saveOptionType = function (optionType) {
  $scope.editing = false;
};

$scope.trackOptionTypesCount = function () {
if ($scope.productEditorModel.OptionTypes.length == 3) {
    $scope.isMaxOptionTypes = true;
} else {
    $scope.isMaxOptionTypes = false;
}
};

$scope.removeOptionType = function (optionType) {
  var index = $scope.productEditorModel.OptionTypes.indexOf(optionType);
  $scope.productEditorModel.OptionTypes.splice(index, 1);
  $scope.trackOptionTypesCount();
};
});

See the plunker below: http://plnkr.co/edit/YHLtSwQWVb2swhNVTQzU?p=info 请参阅下面的插件: http ://plnkr.co/edit/YHLtSwQWVb2swhNVTQzU?p = info

The error you get that $ is not defined is because you haven't included jQuery. $ is not defined得到的错误是因为你没有包含jQuery。 You don't need jQuery for this though, array.map should be able to perform the same functionality. 你不需要jQuery, array.map应该能够执行相同的功能。

$scope.emptyElements = $scope.productEditorModel.ProductOptions.map(function (k) { 
   return k.OptionValue === "" || angular.isUndefined(k.OptionValue) && k.OptionTypeId == optionTypeId 
});

And it crashes because inside $scope.loadData you have 它崩溃了,因为你在$scope.loadData里面

angular.forEach($scope.productEditorModel.ProductOptions, function (item) {
    $scope.addEmptyOption(item.OptionTypeId);
});

and then inside $scope.addEmptyOption you try to 然后你尝试在$scope.addEmptyOption里面

$scope.productEditorModel.ProductOptions.push(emptyOption);

So the foreach will loop for each item in $scope.productEditorModel.ProductOptions , which you keep adding options to so....? 所以foreach将为$scope.productEditorModel.ProductOptions每个项循环,你继续添加选项.... Infinite loop. 无限循环。

Non-crashing version: http://plnkr.co/edit/5Sc2sWfhKBs9kLCk83f1?p=preview 非崩溃版本: http//plnkr.co/edit/5Sc2sWfhKBs9kLCk83f1?p = preview

What you really should do though is look over your data structure. 你真正应该做的是查看你的数据结构。 Make the ProductOptions a sub-object of OptionTypes and just rename it Options. 使ProductOptions成为OptionTypes的子对象,并将其重命名为Options。 Remove ALL code about creating id's here in your GUI, that should be handled by the backend. 删除有关在GUI中创建id的所有代码,应该由后端处理。 Instead in the GUI there should be a Sortorder property on the Options (which also of course gets stored by the backend). 相反,在GUI中,Options上应该有一个Sortorder属性(当然也可以由后端存储)。 Then when you store, the ones without an id get inserted, the ones with an id get updated. 然后,当您存储时,插入没有id的那些,具有id的那些更新。 Much easier to handle everything that way. 更容易处理所有这些方式。

I'd also break out optionTypes and options to their own services/providers. 我还要将optionTypes和选项分解为他们自己的服务/提供者。 Much easier to keep track of what needs to be done. 更容易跟踪需要完成的工作。 And each just basically contains add, remove and maybe a find/getJSON or something. 每个只是基本上包含添加,删除,也许是一个find / getJSON或其他东西。

Here's a restructured version. 这是一个重组版本。 Much easier to keep track of what belongs where. 更容易跟踪属于哪里。 And it has more features than the original with less code. 它具有比原始功能更多的功能,代码更少。 http://plnkr.co/edit/BHcu6vAfcpEYQpZKHc5G?p=preview http://plnkr.co/edit/BHcu6vAfcpEYQpZKHc5G?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM