简体   繁体   English

拖放 angular 顺序不变

[英]drag and drop angular order not changing

i create form builder based on this https://stackblitz.com/edit/angular-dynamic-survey-creation-golkhg , and i use this function for droped我基于此https://stackblitz.com/edit/angular-dynamic-survey-creation-golkhg创建表单构建器,我使用此 function 进行丢弃

onDrop(event: CdkDragDrop<string[]>) {
    moveItemInArray(this.surveyForm.get('surveyQuestions')['controls'], event.previousIndex, event.currentIndex);
}

the questionTitle must changing, but it was not questionTitle必须改变,但事实并非如此

在此处输入图像描述

but in the html it's changing但在 html 中它正在改变

在此处输入图像描述

the order of the array is changing in html, but in my json is not changing, can someone help me?数组的顺序在 html 中发生了变化,但在我的 json 中没有变化,有人可以帮我吗?

EDIT编辑

After a while i don't touch this code again, i found some error,please see this before and after images, here's before过了一会儿我不再碰这个代码,我发现了一些错误,请看之前和之后的图像,这里是之前

前

"quizQuestions": [
    {
      "questionType": "File Upload",
      "display_order": 1,
      "questionGroup": {
        "questionTitle": "",
        "quizAnswer": ""
      }
    },
    {
      "questionType": "Tanggal",
      "display_order": 2,
      "questionGroup": {
        "questionTitle": "",
        "quizAnswer": ""
      }
    }
  ],

after i dragged the first form, it looks like this在我拖动第一个表单后,它看起来像这样

后

"quizQuestions": [
    {
      "questionType": "Tanggal",
      "display_order": 2,
      "questionGroup": {
        "questionTitle": "",
        "quizAnswer": ""
      }
    },
    {
      "questionType": "File Upload",
      "display_order": 1,
      "questionGroup": {
        "questionTitle": "",
        "quizAnswer": ""
      }
    }
  ],

what's is wrong actually?实际上有什么问题? i've tried both of the best solution you guys provide but it's still like this, please help me out.我已经尝试了你们提供的两种最佳解决方案,但仍然是这样,请帮帮我。 Note: i change surverysQuestions to quizQuestions注意:我将 surverysQuestions 更改为 quizQuestions

You need to add display_order property in your formGroup and you can see order change in that property.您需要在 formGroup 中添加 display_order 属性,您可以在该属性中看到顺序更改。

And in onDrop method change the display order of the other questions.并在 onDrop 方法中更改其他问题的显示顺序。 something like this:像这样的东西:

onDrop(event: CdkDragDrop<string[]>) {
   moveItemInArray(this.surveyForm.get('surveyQuestions')['controls'], event.previousIndex, event.currentIndex);
   this.questionFormArray.controls[event.currentIndex]['controls']['display_priority']
            .setValue(event.currentIndex + 1);
   this.questionFormArray.controls.forEach((category, index) => {
            (category as FormGroup).controls['display_priority'].setValue(index + 1);
   });
}

try to add changeDetectionStrategy to your component尝试将 changeDetectionStrategy 添加到您的组件

https://angular.io/api/core/ChangeDetectionStrategy https://angular.io/api/core/ChangeDetectionStrategy

Method example:方法示例:

onDrop(event: CdkDragDrop<string[]>) {
   moveItemInArray(this.surveyForm.get('surveyQuestions')['controls'], event.previousIndex, event.currentIndex);
  this.surveyForm.get('surveyQuestions').updateValueAndValidity({ onlySelf: false });
}

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

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