简体   繁体   English

在数组中插入元素取决于其索引位置,而不替换或更改angularjs中的其他元素

[英]Insert element in array depends on its index position without replace or change other element in angularjs

I have dynamic select box using ng-repeat. 我有使用ng-repeat的动态选择框。 Then I passed the index value for every select box by ng-change. 然后我通过ng-change传递了每个选择框的索引值。

This is my html: 这是我的html:

    <thead>
      <th ng-repeat="l in labels"><div style="width:200px;"></div>{{l.labelname_en}}
      </th>
   </thead>

   <tbody>
      <td ng-repeat="e in excelvalues">
          <select name="selectExcel" class="form-control"  ng-model="selectedExcel" ng-options="excel for excel in excelvalues" ng-change="change(selectedExcel,$index)">
          </select>
      </td>
  </tbody>

This is my js: 这是我的js:

$scope.change = function(excel,index){

            var data = {
                index:index,
                risk_disc_en:excel
            };
           $scope.arr.splice(index,1,data);
}

This is the screen: 这是屏幕:

在此处输入图片说明

In this, I have used the index value for making it as unique. 在此,我使用索引值使其成为唯一。 If I selected the 1st select box the array will be like 如果我选择了第一个选择框,数组将像

[{"index":0,"risk_disc_en":"Risk Description"}]

After that, if I selected the 3rd select box the array be like 在那之后,如果我选择了第三个选择框,数组将像

在此处输入图片说明

[{"index":0,"risk_disc_en":"Risk Description"},{"index":2,"risk_disc_en":"Impact"}]

Note: The index of the 1st element is 0 because I selected the 1st box. 注意:第一个元素的索引为0,因为我选择了第一个框。 Then the index of the 2nd element is 2 because I selected the 3rd box instead of the 2nd box. 然后,第二个元素的索引为2,因为我选择了第三个框而不是第二个框。

After that, I selected the 2nd box the array value in the console like 之后,我在第二个框中选择了控制台中的数组值

在此处输入图片说明

[{"index":0,"risk_disc_en":"Risk Description"},{"index":1,"risk_disc_en":"Probability"}]

The element in the array position 1 {"index":2,"risk_disc_en":"Impact"} is replaced by {"index":1,"risk_disc_en":"Probability"} 数组位置1 {"index":2,"risk_disc_en":"Impact"}的元素替换为{"index":1,"risk_disc_en":"Probability"}

but I want the output array like 但我想要输出数组像

[{"index":0,"risk_disc_en":"RiskDescription"},`{"index":1,"risk_disc_en":"Probability"},{"index":2,"risk_disc_en":"Impact"}]`

If the index value does not exist before, the element should insert at the correct position. 如果索引值之前不存在,则元素应插入正确的位置。 If it exists before it will update or replace the corresponding element having the same index value. 如果存在,它将更新或替换具有相同索引值的相应元素。

For example : 例如 :

case 1: arr={0,2} and I try to insert 1 it should be like arr={0,1,2} . 情况1: arr={0,2} ,我尝试插入1,它应该像arr={0,1,2} In my case it replace the element and looks like arr={0,1} 就我而言,它替换了元素,看起来像arr={0,1}

case 2: arr={0,2} and I try to insert 2 again it should replace and array be like arr={0,2} 情况2: arr={0,2} ,我尝试再次插入2,它应该替换并排列为arr={0,2}

I think it will be better to search for the item first. 我认为最好先搜索该项目。 If you got the item, you don't need to do anything. 如果您收到了该物品,则无需执行任何操作。 But if you didn't, you can push the item on the specified index: 但是,如果没有,则可以将项目推送到指定的索引上:

 $scope.change = function(excel, index) { var data = { index: index, risk_disc_en: excel }; var index = $scope.arr.findIndex(item => item.index === index); if (index === -1) { $scope.arr.splice(index, 0, data); } else { $scope.arr.splice(index, 1, data); } } 

You can use an Object as map to keep track of selected options and sort the object values to get the sorted array. 您可以使用Object作为映射来跟踪所选选项,并对对象值进行排序以获得排序后的数组。

 $scope = {}; $scope.map = {}; $scope.change = function(excel, index) { var data = { index: index, risk_disc_en: excel }; $scope.map[index] = data; $scope.arr = Object.values($scope.map).sort(function(a,b){ return a.value - b.value; }) } $scope.change("item2", 2); $scope.change("item3", 3); $scope.change("item0", 0); console.log($scope.arr); 

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

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