How to use Angular7 (angular material) drag drop between two components












8















As recently angular introduced drag and drop in angular material
https://material.angular.io/cdk/drag-drop/overview .



All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.










share|improve this question





























    8















    As recently angular introduced drag and drop in angular material
    https://material.angular.io/cdk/drag-drop/overview .



    All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.










    share|improve this question



























      8












      8








      8


      2






      As recently angular introduced drag and drop in angular material
      https://material.angular.io/cdk/drag-drop/overview .



      All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.










      share|improve this question
















      As recently angular introduced drag and drop in angular material
      https://material.angular.io/cdk/drag-drop/overview .



      All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.







      angular drag-and-drop angular-material angular7 angular-cdk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 13:47







      Jomy Joseph

















      asked Nov 21 '18 at 13:31









      Jomy JosephJomy Joseph

      435




      435
























          2 Answers
          2






          active

          oldest

          votes


















          16














          You may use properties id and cdkDropListConnectedTo to link both lists:



          Component 1:



          <div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
          <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
          </div>


          Component 2:



          <div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
          <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
          </div>


          If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"



          After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:



          drop(event: CdkDragDrop<string>) {
          if (event.container.id === event.previousContainer.id) {
          // move inside same list
          moveItemInArray(this.list, event.previousIndex, event.currentIndex);
          } else {
          // move between lists
          }
          }


          For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.






          share|improve this answer


























          • Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

            – Jomy Joseph
            Nov 22 '18 at 10:02













          • @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

            – GCSDC
            Nov 22 '18 at 15:12











          • Sure. You are rock. I missed single quote for each id.

            – Jomy Joseph
            Nov 22 '18 at 15:58











          • in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

            – Dirk
            Jan 15 at 14:04













          • @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

            – GCSDC
            Jan 15 at 20:39



















          0














          Not sure if the above solution still works with angular 7.2.9 and angular material/cdk 7.3.5



          It did not work for me and thus after some hard time - I managed to make it work using cdkDropListGroup directive. All cdkDropList within cdkDropListGroup will be available to drop items. You no longer need to connect Drop Lists with cdkDropListConnectedTo property



          <div cdkDropListGroup>
          <component1></component1>
          <component2></component2>
          </div>





          share|improve this answer








          New contributor




          Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            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',
            autoActivateHeartbeat: false,
            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%2f53413175%2fhow-to-use-angular7-angular-material-drag-drop-between-two-components%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









            16














            You may use properties id and cdkDropListConnectedTo to link both lists:



            Component 1:



            <div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            Component 2:



            <div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"



            After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:



            drop(event: CdkDragDrop<string>) {
            if (event.container.id === event.previousContainer.id) {
            // move inside same list
            moveItemInArray(this.list, event.previousIndex, event.currentIndex);
            } else {
            // move between lists
            }
            }


            For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.






            share|improve this answer


























            • Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

              – Jomy Joseph
              Nov 22 '18 at 10:02













            • @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

              – GCSDC
              Nov 22 '18 at 15:12











            • Sure. You are rock. I missed single quote for each id.

              – Jomy Joseph
              Nov 22 '18 at 15:58











            • in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

              – Dirk
              Jan 15 at 14:04













            • @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

              – GCSDC
              Jan 15 at 20:39
















            16














            You may use properties id and cdkDropListConnectedTo to link both lists:



            Component 1:



            <div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            Component 2:



            <div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"



            After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:



            drop(event: CdkDragDrop<string>) {
            if (event.container.id === event.previousContainer.id) {
            // move inside same list
            moveItemInArray(this.list, event.previousIndex, event.currentIndex);
            } else {
            // move between lists
            }
            }


            For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.






            share|improve this answer


























            • Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

              – Jomy Joseph
              Nov 22 '18 at 10:02













            • @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

              – GCSDC
              Nov 22 '18 at 15:12











            • Sure. You are rock. I missed single quote for each id.

              – Jomy Joseph
              Nov 22 '18 at 15:58











            • in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

              – Dirk
              Jan 15 at 14:04













            • @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

              – GCSDC
              Jan 15 at 20:39














            16












            16








            16







            You may use properties id and cdkDropListConnectedTo to link both lists:



            Component 1:



            <div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            Component 2:



            <div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"



            After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:



            drop(event: CdkDragDrop<string>) {
            if (event.container.id === event.previousContainer.id) {
            // move inside same list
            moveItemInArray(this.list, event.previousIndex, event.currentIndex);
            } else {
            // move between lists
            }
            }


            For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.






            share|improve this answer















            You may use properties id and cdkDropListConnectedTo to link both lists:



            Component 1:



            <div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            Component 2:



            <div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
            <div *ngFor="let item of list" cdkDrag>{{ item }}</div>
            </div>


            If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"



            After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:



            drop(event: CdkDragDrop<string>) {
            if (event.container.id === event.previousContainer.id) {
            // move inside same list
            moveItemInArray(this.list, event.previousIndex, event.currentIndex);
            } else {
            // move between lists
            }
            }


            For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 15:46

























            answered Nov 22 '18 at 1:15









            GCSDCGCSDC

            735619




            735619













            • Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

              – Jomy Joseph
              Nov 22 '18 at 10:02













            • @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

              – GCSDC
              Nov 22 '18 at 15:12











            • Sure. You are rock. I missed single quote for each id.

              – Jomy Joseph
              Nov 22 '18 at 15:58











            • in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

              – Dirk
              Jan 15 at 14:04













            • @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

              – GCSDC
              Jan 15 at 20:39



















            • Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

              – Jomy Joseph
              Nov 22 '18 at 10:02













            • @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

              – GCSDC
              Nov 22 '18 at 15:12











            • Sure. You are rock. I missed single quote for each id.

              – Jomy Joseph
              Nov 22 '18 at 15:58











            • in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

              – Dirk
              Jan 15 at 14:04













            • @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

              – GCSDC
              Jan 15 at 20:39

















            Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

            – Jomy Joseph
            Nov 22 '18 at 10:02







            Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that

            – Jomy Joseph
            Nov 22 '18 at 10:02















            @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

            – GCSDC
            Nov 22 '18 at 15:12





            @JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.

            – GCSDC
            Nov 22 '18 at 15:12













            Sure. You are rock. I missed single quote for each id.

            – Jomy Joseph
            Nov 22 '18 at 15:58





            Sure. You are rock. I missed single quote for each id.

            – Jomy Joseph
            Nov 22 '18 at 15:58













            in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

            – Dirk
            Jan 15 at 14:04







            in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list. [cdkDropListConnectedTo]="[list-1]" or [cdkDropListConnectedTo]="['list-1']"

            – Dirk
            Jan 15 at 14:04















            @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

            – GCSDC
            Jan 15 at 20:39





            @Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before: cdkDropListConnectedTo="list-1"

            – GCSDC
            Jan 15 at 20:39













            0














            Not sure if the above solution still works with angular 7.2.9 and angular material/cdk 7.3.5



            It did not work for me and thus after some hard time - I managed to make it work using cdkDropListGroup directive. All cdkDropList within cdkDropListGroup will be available to drop items. You no longer need to connect Drop Lists with cdkDropListConnectedTo property



            <div cdkDropListGroup>
            <component1></component1>
            <component2></component2>
            </div>





            share|improve this answer








            New contributor




            Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              0














              Not sure if the above solution still works with angular 7.2.9 and angular material/cdk 7.3.5



              It did not work for me and thus after some hard time - I managed to make it work using cdkDropListGroup directive. All cdkDropList within cdkDropListGroup will be available to drop items. You no longer need to connect Drop Lists with cdkDropListConnectedTo property



              <div cdkDropListGroup>
              <component1></component1>
              <component2></component2>
              </div>





              share|improve this answer








              New contributor




              Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                0












                0








                0







                Not sure if the above solution still works with angular 7.2.9 and angular material/cdk 7.3.5



                It did not work for me and thus after some hard time - I managed to make it work using cdkDropListGroup directive. All cdkDropList within cdkDropListGroup will be available to drop items. You no longer need to connect Drop Lists with cdkDropListConnectedTo property



                <div cdkDropListGroup>
                <component1></component1>
                <component2></component2>
                </div>





                share|improve this answer








                New contributor




                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                Not sure if the above solution still works with angular 7.2.9 and angular material/cdk 7.3.5



                It did not work for me and thus after some hard time - I managed to make it work using cdkDropListGroup directive. All cdkDropList within cdkDropListGroup will be available to drop items. You no longer need to connect Drop Lists with cdkDropListConnectedTo property



                <div cdkDropListGroup>
                <component1></component1>
                <component2></component2>
                </div>






                share|improve this answer








                New contributor




                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Mar 20 at 12:33









                AltyAlty

                1




                1




                New contributor




                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Alty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413175%2fhow-to-use-angular7-angular-material-drag-drop-between-two-components%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)