angular cdk drag drop with (reactive) forms











up vote
1
down vote

favorite












I have this example : https://stackblitz.com/edit/angular-asevei?file=app%2Fcdk-drag-drop-sorting-example.html



everything works, but while dragging the selectbox 'resets' to the first value in the list.



is there any way to fix this ? it's only visual, but quite jarring for the user. I have tried using the cdkDragPreview option, but couldn't get it to work.



Component:



import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import {FormBuilder, FormGroup, FormArray} from '@angular/forms';

@Component({
selector: 'cdk-drag-drop-sorting-example',
templateUrl: 'cdk-drag-drop-sorting-example.html',
styleUrls: ['cdk-drag-drop-sorting-example.css'],
})
export class CdkDragDropSortingExample {

myForm: FormGroup;

constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
items: fb.array([
fb.group({
name: fb.control('1'),
note: fb.control('quux')
}),
fb.group({
name: fb.control('2'),
note: fb.control('bar')
}),
fb.group({
name: fb.control('3'),
note: fb.control('baz')
})

])
})
}
drop(event: CdkDragDrop<string>) {
moveItemInArray(this.myForm.get('items').controls, event.previousIndex, event.currentIndex);
moveItemInArray(this.myForm.get('items').value, event.previousIndex, event.currentIndex);
}
}


Template:



<form [formGroup]="myForm">
<input formControlName="title" />
<div cdkDropList id="foo" class="example-list" (cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag>
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>
</div>
{{ myForm.value | json}}
</form>








share
























  • how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
    – Andre Elrico
    Nov 17 at 21:48















up vote
1
down vote

favorite












I have this example : https://stackblitz.com/edit/angular-asevei?file=app%2Fcdk-drag-drop-sorting-example.html



everything works, but while dragging the selectbox 'resets' to the first value in the list.



is there any way to fix this ? it's only visual, but quite jarring for the user. I have tried using the cdkDragPreview option, but couldn't get it to work.



Component:



import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import {FormBuilder, FormGroup, FormArray} from '@angular/forms';

@Component({
selector: 'cdk-drag-drop-sorting-example',
templateUrl: 'cdk-drag-drop-sorting-example.html',
styleUrls: ['cdk-drag-drop-sorting-example.css'],
})
export class CdkDragDropSortingExample {

myForm: FormGroup;

constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
items: fb.array([
fb.group({
name: fb.control('1'),
note: fb.control('quux')
}),
fb.group({
name: fb.control('2'),
note: fb.control('bar')
}),
fb.group({
name: fb.control('3'),
note: fb.control('baz')
})

])
})
}
drop(event: CdkDragDrop<string>) {
moveItemInArray(this.myForm.get('items').controls, event.previousIndex, event.currentIndex);
moveItemInArray(this.myForm.get('items').value, event.previousIndex, event.currentIndex);
}
}


Template:



<form [formGroup]="myForm">
<input formControlName="title" />
<div cdkDropList id="foo" class="example-list" (cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag>
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>
</div>
{{ myForm.value | json}}
</form>








share
























  • how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
    – Andre Elrico
    Nov 17 at 21:48













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this example : https://stackblitz.com/edit/angular-asevei?file=app%2Fcdk-drag-drop-sorting-example.html



everything works, but while dragging the selectbox 'resets' to the first value in the list.



is there any way to fix this ? it's only visual, but quite jarring for the user. I have tried using the cdkDragPreview option, but couldn't get it to work.



Component:



import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import {FormBuilder, FormGroup, FormArray} from '@angular/forms';

@Component({
selector: 'cdk-drag-drop-sorting-example',
templateUrl: 'cdk-drag-drop-sorting-example.html',
styleUrls: ['cdk-drag-drop-sorting-example.css'],
})
export class CdkDragDropSortingExample {

myForm: FormGroup;

constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
items: fb.array([
fb.group({
name: fb.control('1'),
note: fb.control('quux')
}),
fb.group({
name: fb.control('2'),
note: fb.control('bar')
}),
fb.group({
name: fb.control('3'),
note: fb.control('baz')
})

])
})
}
drop(event: CdkDragDrop<string>) {
moveItemInArray(this.myForm.get('items').controls, event.previousIndex, event.currentIndex);
moveItemInArray(this.myForm.get('items').value, event.previousIndex, event.currentIndex);
}
}


Template:



<form [formGroup]="myForm">
<input formControlName="title" />
<div cdkDropList id="foo" class="example-list" (cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag>
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>
</div>
{{ myForm.value | json}}
</form>








share















I have this example : https://stackblitz.com/edit/angular-asevei?file=app%2Fcdk-drag-drop-sorting-example.html



everything works, but while dragging the selectbox 'resets' to the first value in the list.



is there any way to fix this ? it's only visual, but quite jarring for the user. I have tried using the cdkDragPreview option, but couldn't get it to work.



Component:



import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import {FormBuilder, FormGroup, FormArray} from '@angular/forms';

@Component({
selector: 'cdk-drag-drop-sorting-example',
templateUrl: 'cdk-drag-drop-sorting-example.html',
styleUrls: ['cdk-drag-drop-sorting-example.css'],
})
export class CdkDragDropSortingExample {

myForm: FormGroup;

constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
items: fb.array([
fb.group({
name: fb.control('1'),
note: fb.control('quux')
}),
fb.group({
name: fb.control('2'),
note: fb.control('bar')
}),
fb.group({
name: fb.control('3'),
note: fb.control('baz')
})

])
})
}
drop(event: CdkDragDrop<string>) {
moveItemInArray(this.myForm.get('items').controls, event.previousIndex, event.currentIndex);
moveItemInArray(this.myForm.get('items').value, event.previousIndex, event.currentIndex);
}
}


Template:



<form [formGroup]="myForm">
<input formControlName="title" />
<div cdkDropList id="foo" class="example-list" (cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag>
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>
</div>
{{ myForm.value | json}}
</form>






angular angular-reactive-forms angular-cdk





share














share












share



share








edited Nov 8 at 15:33









Alexander Staroselsky

11k31639




11k31639










asked Nov 8 at 15:12









tech-no-logical

83




83












  • how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
    – Andre Elrico
    Nov 17 at 21:48


















  • how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
    – Andre Elrico
    Nov 17 at 21:48
















how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
– Andre Elrico
Nov 17 at 21:48




how did your drag n drop form project evolve. I'm working on a similar project. Would you like to chat ?
– Andre Elrico
Nov 17 at 21:48












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










I have revised @Amir Fawzy's answer by adding a variable in getting the current active note which shows upon dragging the selected box.



TS:



activeNote: string;
enter(i) {
this.activeNote = this.myForm.get('items')['controls'][i].get('note').value;
}


HTML:



<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
<div [id]="i" class="example-box" *ngFor="let item of myForm.get('items').controls; let i=index;" cdkDrag #elem="cdkDrag" (mouseenter)="enter(i)">
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option [hidden]="elem.moved">{{activeNote}}</option>
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>
</div>


Here's a duplicate on Stackblitz..





share





















  • both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
    – tech-no-logical
    Nov 9 at 10:29












  • *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
    – KimCindyOliveros
    Nov 9 at 23:36


















up vote
0
down vote













your code is fine nothing wrong with it but this issue if called it so, caused by drag-drop cdk as this's the default behavior as i know but you can work around to enhance that behavior to make it looks good to the user.



try this



<div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag #elem="cdkDrag">
<span cdkDragHandle>drag</span>
<div [formGroup]="item">
<input type="text" formControlName="name">
<select formControlName="note">
<option [hidden]="elem.moved">dragging...</option>
<option>foo</option>
<option>bar</option>
<option>baz</option>
<option>quux</option>
</select>
</div>
</div>


what happened here to things #elem="cdkDrag" added to drag element as local reference
and <option [hidden]="elem.moved">dragging...</option> what gonna happen here is when the user drag the element dragging select option will show in select input when user end the dragging it will hide again and select input value will set again with some css it will looks good.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210612%2fangular-cdk-drag-drop-with-reactive-forms%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I have revised @Amir Fawzy's answer by adding a variable in getting the current active note which shows upon dragging the selected box.



    TS:



    activeNote: string;
    enter(i) {
    this.activeNote = this.myForm.get('items')['controls'][i].get('note').value;
    }


    HTML:



    <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
    <div [id]="i" class="example-box" *ngFor="let item of myForm.get('items').controls; let i=index;" cdkDrag #elem="cdkDrag" (mouseenter)="enter(i)">
    <span cdkDragHandle>drag</span>
    <div [formGroup]="item">
    <input type="text" formControlName="name">
    <select formControlName="note">
    <option [hidden]="elem.moved">{{activeNote}}</option>
    <option>foo</option>
    <option>bar</option>
    <option>baz</option>
    <option>quux</option>
    </select>
    </div>
    </div>
    </div>


    Here's a duplicate on Stackblitz..





    share





















    • both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
      – tech-no-logical
      Nov 9 at 10:29












    • *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
      – KimCindyOliveros
      Nov 9 at 23:36















    up vote
    0
    down vote



    accepted










    I have revised @Amir Fawzy's answer by adding a variable in getting the current active note which shows upon dragging the selected box.



    TS:



    activeNote: string;
    enter(i) {
    this.activeNote = this.myForm.get('items')['controls'][i].get('note').value;
    }


    HTML:



    <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
    <div [id]="i" class="example-box" *ngFor="let item of myForm.get('items').controls; let i=index;" cdkDrag #elem="cdkDrag" (mouseenter)="enter(i)">
    <span cdkDragHandle>drag</span>
    <div [formGroup]="item">
    <input type="text" formControlName="name">
    <select formControlName="note">
    <option [hidden]="elem.moved">{{activeNote}}</option>
    <option>foo</option>
    <option>bar</option>
    <option>baz</option>
    <option>quux</option>
    </select>
    </div>
    </div>
    </div>


    Here's a duplicate on Stackblitz..





    share





















    • both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
      – tech-no-logical
      Nov 9 at 10:29












    • *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
      – KimCindyOliveros
      Nov 9 at 23:36













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    I have revised @Amir Fawzy's answer by adding a variable in getting the current active note which shows upon dragging the selected box.



    TS:



    activeNote: string;
    enter(i) {
    this.activeNote = this.myForm.get('items')['controls'][i].get('note').value;
    }


    HTML:



    <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
    <div [id]="i" class="example-box" *ngFor="let item of myForm.get('items').controls; let i=index;" cdkDrag #elem="cdkDrag" (mouseenter)="enter(i)">
    <span cdkDragHandle>drag</span>
    <div [formGroup]="item">
    <input type="text" formControlName="name">
    <select formControlName="note">
    <option [hidden]="elem.moved">{{activeNote}}</option>
    <option>foo</option>
    <option>bar</option>
    <option>baz</option>
    <option>quux</option>
    </select>
    </div>
    </div>
    </div>


    Here's a duplicate on Stackblitz..





    share












    I have revised @Amir Fawzy's answer by adding a variable in getting the current active note which shows upon dragging the selected box.



    TS:



    activeNote: string;
    enter(i) {
    this.activeNote = this.myForm.get('items')['controls'][i].get('note').value;
    }


    HTML:



    <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
    <div [id]="i" class="example-box" *ngFor="let item of myForm.get('items').controls; let i=index;" cdkDrag #elem="cdkDrag" (mouseenter)="enter(i)">
    <span cdkDragHandle>drag</span>
    <div [formGroup]="item">
    <input type="text" formControlName="name">
    <select formControlName="note">
    <option [hidden]="elem.moved">{{activeNote}}</option>
    <option>foo</option>
    <option>bar</option>
    <option>baz</option>
    <option>quux</option>
    </select>
    </div>
    </div>
    </div>


    Here's a duplicate on Stackblitz..






    share











    share


    share










    answered Nov 9 at 0:19









    KimCindyOliveros

    2379




    2379












    • both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
      – tech-no-logical
      Nov 9 at 10:29












    • *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
      – KimCindyOliveros
      Nov 9 at 23:36


















    • both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
      – tech-no-logical
      Nov 9 at 10:29












    • *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
      – KimCindyOliveros
      Nov 9 at 23:36
















    both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
    – tech-no-logical
    Nov 9 at 10:29






    both yours and @Amir Fawzy's answers led me to a slighly different solution shown here : stackblitz.com/edit/angular-asevei-wwblpi?file=app/… using the activeNote option with the *cdkDragPreview directive and the (cdkDragStarted) event. thanks !
    – tech-no-logical
    Nov 9 at 10:29














    *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
    – KimCindyOliveros
    Nov 9 at 23:36




    *cdkDragPreview is another great option. We're just focused on the display at the selectbox. Anyways, glad I could help :)
    – KimCindyOliveros
    Nov 9 at 23:36












    up vote
    0
    down vote













    your code is fine nothing wrong with it but this issue if called it so, caused by drag-drop cdk as this's the default behavior as i know but you can work around to enhance that behavior to make it looks good to the user.



    try this



    <div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag #elem="cdkDrag">
    <span cdkDragHandle>drag</span>
    <div [formGroup]="item">
    <input type="text" formControlName="name">
    <select formControlName="note">
    <option [hidden]="elem.moved">dragging...</option>
    <option>foo</option>
    <option>bar</option>
    <option>baz</option>
    <option>quux</option>
    </select>
    </div>
    </div>


    what happened here to things #elem="cdkDrag" added to drag element as local reference
    and <option [hidden]="elem.moved">dragging...</option> what gonna happen here is when the user drag the element dragging select option will show in select input when user end the dragging it will hide again and select input value will set again with some css it will looks good.






    share|improve this answer

























      up vote
      0
      down vote













      your code is fine nothing wrong with it but this issue if called it so, caused by drag-drop cdk as this's the default behavior as i know but you can work around to enhance that behavior to make it looks good to the user.



      try this



      <div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag #elem="cdkDrag">
      <span cdkDragHandle>drag</span>
      <div [formGroup]="item">
      <input type="text" formControlName="name">
      <select formControlName="note">
      <option [hidden]="elem.moved">dragging...</option>
      <option>foo</option>
      <option>bar</option>
      <option>baz</option>
      <option>quux</option>
      </select>
      </div>
      </div>


      what happened here to things #elem="cdkDrag" added to drag element as local reference
      and <option [hidden]="elem.moved">dragging...</option> what gonna happen here is when the user drag the element dragging select option will show in select input when user end the dragging it will hide again and select input value will set again with some css it will looks good.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        your code is fine nothing wrong with it but this issue if called it so, caused by drag-drop cdk as this's the default behavior as i know but you can work around to enhance that behavior to make it looks good to the user.



        try this



        <div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag #elem="cdkDrag">
        <span cdkDragHandle>drag</span>
        <div [formGroup]="item">
        <input type="text" formControlName="name">
        <select formControlName="note">
        <option [hidden]="elem.moved">dragging...</option>
        <option>foo</option>
        <option>bar</option>
        <option>baz</option>
        <option>quux</option>
        </select>
        </div>
        </div>


        what happened here to things #elem="cdkDrag" added to drag element as local reference
        and <option [hidden]="elem.moved">dragging...</option> what gonna happen here is when the user drag the element dragging select option will show in select input when user end the dragging it will hide again and select input value will set again with some css it will looks good.






        share|improve this answer












        your code is fine nothing wrong with it but this issue if called it so, caused by drag-drop cdk as this's the default behavior as i know but you can work around to enhance that behavior to make it looks good to the user.



        try this



        <div class="example-box" *ngFor="let item of myForm.get('items').controls" cdkDrag #elem="cdkDrag">
        <span cdkDragHandle>drag</span>
        <div [formGroup]="item">
        <input type="text" formControlName="name">
        <select formControlName="note">
        <option [hidden]="elem.moved">dragging...</option>
        <option>foo</option>
        <option>bar</option>
        <option>baz</option>
        <option>quux</option>
        </select>
        </div>
        </div>


        what happened here to things #elem="cdkDrag" added to drag element as local reference
        and <option [hidden]="elem.moved">dragging...</option> what gonna happen here is when the user drag the element dragging select option will show in select input when user end the dragging it will hide again and select input value will set again with some css it will looks good.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 16:26









        Amir Fawzy

        1758




        1758






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210612%2fangular-cdk-drag-drop-with-reactive-forms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Landwehr

            Reims

            Schenkenzell