【问题标题】:How to get the ordered index after changing values in an array in angular?如何在角度更改数组中的值后获取有序索引?
【发布时间】:2018-06-20 08:33:35
【问题描述】:

我有一个数组中的项目列表,

Design
Analysis
Testing
Development

谁的索引,

0,1,2,3

这里我使用了用于拖放列表元素的角拖拉。通过使用 Dragula,我正在更改数组的位置,例如,

Analysis
Design
Development
Testing

顺序变了,但索引位置还是一样,

1,0,3,2

这里的索引位置不是按顺序排列的,尽管它看起来像旧的索引值,但它看起来像旧的索引值。 而无论数组内发生什么变化,我都需要具有相同的索引位置,这意味着即使顺序发生变化,索引位置也应始终从 0,1,2,... so on 开始。

我用过的html组件,

    <div class="card-content">
        <div dropDirective (dropEvent)="addDropItem($event)" class="droppable" [dropHighlight]="'highlight'">
            <div [dragula]="'second-bag'">
                <app-generic-drop-box *ngFor="let item of itemsDropped; index as i" (editBtn)='editBtn($event)' (deleteBtn)='deleteBtn($event)' [genericBox]='item' class="arrow_box dropItem temp" (click)='indexOfItem(i)'> </app-generic-drop-box>
            </div>
        </div>
    </div>

在ts文件中,

        import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ViewContainerRef, OnDestroy } from '@angular/core';
        import { Router, ActivatedRoute } from '@angular/router';
        import { ScenarioMapService } from '../scenario-map.service';

        import { DynamicFormModel } from '../dynamic-form-config/dynamic-form-model';
        import { Textbox } from '../dynamic-form-config/textbox';
        import { CONFIG } from '../../app.config';

        @Component({
          selector: 'app-drop-area',
          templateUrl: './drop-area.component.html',
          styleUrls: ['./drop-area.component.scss']
        })
        export class DropAreaComponent implements OnInit, OnDestroy {

        indexOfItem(i) {
        console.log(i); 
        }

    }

这里console.log(i) 显示索引位置,如果用户单击列表,如果我更改数组的位置,我需要在更改后获取有序索引,如0,1,2,3... .. 请帮助我实现结果。

【问题讨论】:

    标签: javascript angular typescript dragula ng2-dragula


    【解决方案1】:

    要重置 AngularJS 数组中的索引,您可以完全过滤它。

    这样索引会重新设置。

    resetArr = orgArr.filter(function(){return true;});

    数组索引1,0,3,2 将再次变为0,1,2,3,但数据仍然存在。

    作为参考:Need to reset just indexes of JavaScript array Stackoverflow Thread...

    【讨论】:

    • 这里我使用的是 angular 2,如果你能帮助我现有的代码,它会更加明显..
    猜你喜欢
    • 2017-12-18
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多