Angular cdkDropList drag element restriction












1















I am currently using Angular 2 and a drag and drop module from https://material.angular.io/cdk/drag-drop/overview. I have made the drag and drop features work. I have two different types of class objects that i desire to be limited to their own types of drag and drop lists.



This could very likely be solved with grouping the lists but since I am using recursion other issues came up...



Currently I am having every lists inside the same group, meaning that anything can be dragged and dropped in every list (cdkDropListGroup, is positioned in a component before the recursion part is performed).



I am trying to make the lists restricted to only accept either Element or Attribute (but not both), but I have no idea of how to do this...



I have the following:



Classes:



export class Attribute {
name: string;
type: string;

}

export class Element {
id: number;
name: string;
elements: Element
attributes: Attribute;

}


HTML:



<div > 
Elements
<div
cdkDropList
[cdkDropListData]="elements"
class="example-list"
(cdkDropListDropped)="drop($event)"
[cdkDropListEnterPredicate]="isElement">
<div type="button" text-align="right" class="btn btnNotInline" (click)="addNewElement()">
<img src="assets/img/IconPlus.png" class="elementListIcon"></div>
<div *ngFor="let element of elements" class="example-box" cdkDrag>
<mat-list>
<mat-list-item>
<mat-form-field appearance="standard dense" class="example-container">
<input matInput placeholder="{{element.name}}">
</mat-form-field>
</mat-list-item>
<mat-list-item>
<div
cdkDropList
[cdkDropListData]="attributes"
class="cdk-drag-list-attributes"
(cdkDropListDropped)="drop($event)"
[cdkDropListEnterPredicate]="isAttribute">
<div type="button" text-align="right" class="btn btnNotInline" (click)="addNewAttribute()">
<img src="assets/img/IconPlusPurple.png" class="elementListIcon"></div>
<div *ngFor="let attribute of attributes" class="example-container" cdkDrag>
<p class="mat-input-element-attribute">
<input matInput placeholder="{{attribute.name}}">
<input matInput placeholder="{{attribute.type}}">
</p>
</div>
</div>
</mat-list-item>
<mat-list-item>
<app-listboardelement [attributes]="element.attributes" [elements]="element.elements"></app-listboardelement>
</mat-list-item>
</mat-list>
</div>





The ts. method being called (the attribute looks alike)



isElement(drag : CdkDrag){
console.log("check " + (drag instanceof Element) + typeof drag + " , "+ typeof drag.data + ", "+ drag.data + " , " +(drag.data instanceof Element));
return (drag.data instanceof Element);
}


from the output I simply gets: "check false object , undefined, undefined , false"
From this I have tried to compare the dragged object with a class.. but I didn't have any luck.



Is there any way I can limit dragged object to certain lists dynamically? I know about [cdkDropListConnectedTo] but this gave me issues with the occuring recursion and the bindings. Any guidance would be appreciated



EDIT:
Added image for presentation of how it is displayed - but does not work properly;
enter image description here










share|improve this question





























    1















    I am currently using Angular 2 and a drag and drop module from https://material.angular.io/cdk/drag-drop/overview. I have made the drag and drop features work. I have two different types of class objects that i desire to be limited to their own types of drag and drop lists.



    This could very likely be solved with grouping the lists but since I am using recursion other issues came up...



    Currently I am having every lists inside the same group, meaning that anything can be dragged and dropped in every list (cdkDropListGroup, is positioned in a component before the recursion part is performed).



    I am trying to make the lists restricted to only accept either Element or Attribute (but not both), but I have no idea of how to do this...



    I have the following:



    Classes:



    export class Attribute {
    name: string;
    type: string;

    }

    export class Element {
    id: number;
    name: string;
    elements: Element
    attributes: Attribute;

    }


    HTML:



    <div > 
    Elements
    <div
    cdkDropList
    [cdkDropListData]="elements"
    class="example-list"
    (cdkDropListDropped)="drop($event)"
    [cdkDropListEnterPredicate]="isElement">
    <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewElement()">
    <img src="assets/img/IconPlus.png" class="elementListIcon"></div>
    <div *ngFor="let element of elements" class="example-box" cdkDrag>
    <mat-list>
    <mat-list-item>
    <mat-form-field appearance="standard dense" class="example-container">
    <input matInput placeholder="{{element.name}}">
    </mat-form-field>
    </mat-list-item>
    <mat-list-item>
    <div
    cdkDropList
    [cdkDropListData]="attributes"
    class="cdk-drag-list-attributes"
    (cdkDropListDropped)="drop($event)"
    [cdkDropListEnterPredicate]="isAttribute">
    <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewAttribute()">
    <img src="assets/img/IconPlusPurple.png" class="elementListIcon"></div>
    <div *ngFor="let attribute of attributes" class="example-container" cdkDrag>
    <p class="mat-input-element-attribute">
    <input matInput placeholder="{{attribute.name}}">
    <input matInput placeholder="{{attribute.type}}">
    </p>
    </div>
    </div>
    </mat-list-item>
    <mat-list-item>
    <app-listboardelement [attributes]="element.attributes" [elements]="element.elements"></app-listboardelement>
    </mat-list-item>
    </mat-list>
    </div>





    The ts. method being called (the attribute looks alike)



    isElement(drag : CdkDrag){
    console.log("check " + (drag instanceof Element) + typeof drag + " , "+ typeof drag.data + ", "+ drag.data + " , " +(drag.data instanceof Element));
    return (drag.data instanceof Element);
    }


    from the output I simply gets: "check false object , undefined, undefined , false"
    From this I have tried to compare the dragged object with a class.. but I didn't have any luck.



    Is there any way I can limit dragged object to certain lists dynamically? I know about [cdkDropListConnectedTo] but this gave me issues with the occuring recursion and the bindings. Any guidance would be appreciated



    EDIT:
    Added image for presentation of how it is displayed - but does not work properly;
    enter image description here










    share|improve this question



























      1












      1








      1


      1






      I am currently using Angular 2 and a drag and drop module from https://material.angular.io/cdk/drag-drop/overview. I have made the drag and drop features work. I have two different types of class objects that i desire to be limited to their own types of drag and drop lists.



      This could very likely be solved with grouping the lists but since I am using recursion other issues came up...



      Currently I am having every lists inside the same group, meaning that anything can be dragged and dropped in every list (cdkDropListGroup, is positioned in a component before the recursion part is performed).



      I am trying to make the lists restricted to only accept either Element or Attribute (but not both), but I have no idea of how to do this...



      I have the following:



      Classes:



      export class Attribute {
      name: string;
      type: string;

      }

      export class Element {
      id: number;
      name: string;
      elements: Element
      attributes: Attribute;

      }


      HTML:



      <div > 
      Elements
      <div
      cdkDropList
      [cdkDropListData]="elements"
      class="example-list"
      (cdkDropListDropped)="drop($event)"
      [cdkDropListEnterPredicate]="isElement">
      <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewElement()">
      <img src="assets/img/IconPlus.png" class="elementListIcon"></div>
      <div *ngFor="let element of elements" class="example-box" cdkDrag>
      <mat-list>
      <mat-list-item>
      <mat-form-field appearance="standard dense" class="example-container">
      <input matInput placeholder="{{element.name}}">
      </mat-form-field>
      </mat-list-item>
      <mat-list-item>
      <div
      cdkDropList
      [cdkDropListData]="attributes"
      class="cdk-drag-list-attributes"
      (cdkDropListDropped)="drop($event)"
      [cdkDropListEnterPredicate]="isAttribute">
      <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewAttribute()">
      <img src="assets/img/IconPlusPurple.png" class="elementListIcon"></div>
      <div *ngFor="let attribute of attributes" class="example-container" cdkDrag>
      <p class="mat-input-element-attribute">
      <input matInput placeholder="{{attribute.name}}">
      <input matInput placeholder="{{attribute.type}}">
      </p>
      </div>
      </div>
      </mat-list-item>
      <mat-list-item>
      <app-listboardelement [attributes]="element.attributes" [elements]="element.elements"></app-listboardelement>
      </mat-list-item>
      </mat-list>
      </div>





      The ts. method being called (the attribute looks alike)



      isElement(drag : CdkDrag){
      console.log("check " + (drag instanceof Element) + typeof drag + " , "+ typeof drag.data + ", "+ drag.data + " , " +(drag.data instanceof Element));
      return (drag.data instanceof Element);
      }


      from the output I simply gets: "check false object , undefined, undefined , false"
      From this I have tried to compare the dragged object with a class.. but I didn't have any luck.



      Is there any way I can limit dragged object to certain lists dynamically? I know about [cdkDropListConnectedTo] but this gave me issues with the occuring recursion and the bindings. Any guidance would be appreciated



      EDIT:
      Added image for presentation of how it is displayed - but does not work properly;
      enter image description here










      share|improve this question
















      I am currently using Angular 2 and a drag and drop module from https://material.angular.io/cdk/drag-drop/overview. I have made the drag and drop features work. I have two different types of class objects that i desire to be limited to their own types of drag and drop lists.



      This could very likely be solved with grouping the lists but since I am using recursion other issues came up...



      Currently I am having every lists inside the same group, meaning that anything can be dragged and dropped in every list (cdkDropListGroup, is positioned in a component before the recursion part is performed).



      I am trying to make the lists restricted to only accept either Element or Attribute (but not both), but I have no idea of how to do this...



      I have the following:



      Classes:



      export class Attribute {
      name: string;
      type: string;

      }

      export class Element {
      id: number;
      name: string;
      elements: Element
      attributes: Attribute;

      }


      HTML:



      <div > 
      Elements
      <div
      cdkDropList
      [cdkDropListData]="elements"
      class="example-list"
      (cdkDropListDropped)="drop($event)"
      [cdkDropListEnterPredicate]="isElement">
      <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewElement()">
      <img src="assets/img/IconPlus.png" class="elementListIcon"></div>
      <div *ngFor="let element of elements" class="example-box" cdkDrag>
      <mat-list>
      <mat-list-item>
      <mat-form-field appearance="standard dense" class="example-container">
      <input matInput placeholder="{{element.name}}">
      </mat-form-field>
      </mat-list-item>
      <mat-list-item>
      <div
      cdkDropList
      [cdkDropListData]="attributes"
      class="cdk-drag-list-attributes"
      (cdkDropListDropped)="drop($event)"
      [cdkDropListEnterPredicate]="isAttribute">
      <div type="button" text-align="right" class="btn btnNotInline" (click)="addNewAttribute()">
      <img src="assets/img/IconPlusPurple.png" class="elementListIcon"></div>
      <div *ngFor="let attribute of attributes" class="example-container" cdkDrag>
      <p class="mat-input-element-attribute">
      <input matInput placeholder="{{attribute.name}}">
      <input matInput placeholder="{{attribute.type}}">
      </p>
      </div>
      </div>
      </mat-list-item>
      <mat-list-item>
      <app-listboardelement [attributes]="element.attributes" [elements]="element.elements"></app-listboardelement>
      </mat-list-item>
      </mat-list>
      </div>





      The ts. method being called (the attribute looks alike)



      isElement(drag : CdkDrag){
      console.log("check " + (drag instanceof Element) + typeof drag + " , "+ typeof drag.data + ", "+ drag.data + " , " +(drag.data instanceof Element));
      return (drag.data instanceof Element);
      }


      from the output I simply gets: "check false object , undefined, undefined , false"
      From this I have tried to compare the dragged object with a class.. but I didn't have any luck.



      Is there any way I can limit dragged object to certain lists dynamically? I know about [cdkDropListConnectedTo] but this gave me issues with the occuring recursion and the bindings. Any guidance would be appreciated



      EDIT:
      Added image for presentation of how it is displayed - but does not work properly;
      enter image description here







      javascript html angular drag-and-drop angular-cdk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 17:09







      Skdy

















      asked Nov 25 '18 at 15:06









      SkdySkdy

      171112




      171112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can always check the drag-n-drop 'origin to destination' containers and take action accordingly, something like :



            drop(event: CdkDragDrop<string>) {
          // same container (just reorder items)
          if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
          } else {
          // from first list to second list
          if (event.previousContainer.id === 'cdk-drop-list-0' && event.container.id === 'cdk-drop-list-1') {
          // do something
          }
          // from second list to first list
          if (event.previousContainer.id === 'cdk-drop-list-1' && event.container.id === 'cdk-drop-list-0') {
          // do something
          }
          }
          }


          Hope this helps!






          share|improve this answer
























          • I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

            – Skdy
            Nov 25 '18 at 15:51











          • Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

            – freepowder
            Nov 25 '18 at 16:10











          • I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

            – Skdy
            Nov 25 '18 at 17:06











          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%2f53468815%2fangular-cdkdroplist-drag-element-restriction%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can always check the drag-n-drop 'origin to destination' containers and take action accordingly, something like :



            drop(event: CdkDragDrop<string>) {
          // same container (just reorder items)
          if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
          } else {
          // from first list to second list
          if (event.previousContainer.id === 'cdk-drop-list-0' && event.container.id === 'cdk-drop-list-1') {
          // do something
          }
          // from second list to first list
          if (event.previousContainer.id === 'cdk-drop-list-1' && event.container.id === 'cdk-drop-list-0') {
          // do something
          }
          }
          }


          Hope this helps!






          share|improve this answer
























          • I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

            – Skdy
            Nov 25 '18 at 15:51











          • Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

            – freepowder
            Nov 25 '18 at 16:10











          • I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

            – Skdy
            Nov 25 '18 at 17:06
















          0














          You can always check the drag-n-drop 'origin to destination' containers and take action accordingly, something like :



            drop(event: CdkDragDrop<string>) {
          // same container (just reorder items)
          if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
          } else {
          // from first list to second list
          if (event.previousContainer.id === 'cdk-drop-list-0' && event.container.id === 'cdk-drop-list-1') {
          // do something
          }
          // from second list to first list
          if (event.previousContainer.id === 'cdk-drop-list-1' && event.container.id === 'cdk-drop-list-0') {
          // do something
          }
          }
          }


          Hope this helps!






          share|improve this answer
























          • I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

            – Skdy
            Nov 25 '18 at 15:51











          • Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

            – freepowder
            Nov 25 '18 at 16:10











          • I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

            – Skdy
            Nov 25 '18 at 17:06














          0












          0








          0







          You can always check the drag-n-drop 'origin to destination' containers and take action accordingly, something like :



            drop(event: CdkDragDrop<string>) {
          // same container (just reorder items)
          if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
          } else {
          // from first list to second list
          if (event.previousContainer.id === 'cdk-drop-list-0' && event.container.id === 'cdk-drop-list-1') {
          // do something
          }
          // from second list to first list
          if (event.previousContainer.id === 'cdk-drop-list-1' && event.container.id === 'cdk-drop-list-0') {
          // do something
          }
          }
          }


          Hope this helps!






          share|improve this answer













          You can always check the drag-n-drop 'origin to destination' containers and take action accordingly, something like :



            drop(event: CdkDragDrop<string>) {
          // same container (just reorder items)
          if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
          } else {
          // from first list to second list
          if (event.previousContainer.id === 'cdk-drop-list-0' && event.container.id === 'cdk-drop-list-1') {
          // do something
          }
          // from second list to first list
          if (event.previousContainer.id === 'cdk-drop-list-1' && event.container.id === 'cdk-drop-list-0') {
          // do something
          }
          }
          }


          Hope this helps!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 15:42









          freepowderfreepowder

          2976




          2976













          • I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

            – Skdy
            Nov 25 '18 at 15:51











          • Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

            – freepowder
            Nov 25 '18 at 16:10











          • I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

            – Skdy
            Nov 25 '18 at 17:06



















          • I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

            – Skdy
            Nov 25 '18 at 15:51











          • Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

            – freepowder
            Nov 25 '18 at 16:10











          • I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

            – Skdy
            Nov 25 '18 at 17:06

















          I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

          – Skdy
          Nov 25 '18 at 15:51





          I appreciate the response, but I am currently working with list generated from recursion, so (currently) no id's are set. Previously i did generate an ID but I was unable to make the links or such dynamic... Is it possible to do this example of yours towards an Array of IDs?

          – Skdy
          Nov 25 '18 at 15:51













          Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

          – freepowder
          Nov 25 '18 at 16:10





          Would you mind to share stackblitz code? have couple of ideas on how to sort out this thing but hard to check without been able to reproduce correctly on my end and just guessing. Thanks!

          – freepowder
          Nov 25 '18 at 16:10













          I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

          – Skdy
          Nov 25 '18 at 17:06





          I gotta be honest, I have never used stackblitz but I gave it a try and threw in the code / formated it as much as possible. Included dependencies but errors occur upon cdk library usages... If you could bypass that error the code "Should" work to an extend.. I can post an image of how it works locally atm. stackblitz.com/edit/angular-srbezs?embed=1&file=src/app/…

          – Skdy
          Nov 25 '18 at 17:06




















          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%2f53468815%2fangular-cdkdroplist-drag-element-restriction%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

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen