{"id":10512,"date":"2024-06-06T11:44:21","date_gmt":"2024-06-06T11:44:21","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10512"},"modified":"2024-06-06T11:44:21","modified_gmt":"2024-06-06T11:44:21","slug":"vue-draggable-in-draggable-and-how-to-get-parents-element","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/vue\/vue-draggable-in-draggable-and-how-to-get-parents-element","title":{"rendered":"Vue Draggable in Draggable &#038; How to Get Parent&#8217;s Element"},"content":{"rendered":"<p>If you&#8217;re using the vuedraggable library for drag-and-drop functionality within Vue.js, and you want to access the parent element of a draggable item, you can achieve this by handling the change event emitted by the vuedraggable component. The change event provides information about the dragged item and its new position, including its old and new parent elements.<\/p>\n<p>Here&#8217;s an example of how you can accomplish this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">&lt;template&gt;\r\n &lt;div&gt;\r\n   &lt;h2&gt;Parent Container&lt;\/h2&gt;\r\n   &lt;draggable v-model=\"list\" @change=\"handleDragChange\"&gt;\r\n     &lt;div v-for=\"(item, index) in list\" :key=\"index\"&gt;\r\n       &lt;!-- Display draggable items here --&gt;\r\n       &lt;div&gt;{{ item }}&lt;\/div&gt;\r\n     &lt;\/div&gt;\r\n   &lt;\/draggable&gt;\r\n &lt;\/div&gt;\r\n&lt;\/template&gt;\r\n&lt;script&gt;\r\nimport draggable from 'vuedraggable';\r\n\r\nexport default {\r\n components: {\r\n   draggable\r\n },\r\n data() {\r\n   return {\r\n     list: ['Item 1', 'Item 2', 'Item 3']\r\n   };\r\n },\r\n\r\n\r\n methods: {\r\n   handleDragChange(event) {\r\n     \/\/ Access the dragged item\r\n     const draggedItem = event.moved.element;\r\n\r\n     \/\/ Access the old parent element\r\n     const oldParentElement = event.moved.oldIndex &gt; -1 ? event.moved.oldContainer : null;\r\n\r\n     \/\/ Access the new parent element\r\n     const newParentElement = event.moved.newIndex &gt; -1 ? event.moved.newContainer : null;\r\n\r\n     console.log('Dragged Item:', draggedItem);\r\n     console.log('Old Parent:', oldParentElement);\r\n     console.log('New Parent:', newParentElement);\r\n   }\r\n }\r\n};\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>&nbsp;<br \/>\n<strong>In this example:<\/strong><\/p>\n<ul>\n<li>We have a parent container with draggable items rendered inside the vuedraggable component.<\/li>\n<li>We&#8217;re listening for the change event emitted by the vuedraggable component and calling the handleDragChange method when a drag-and-drop operation occurs.<\/li>\n<li>Inside the handleDragChange method, we access the dragged item (event.moved.element), the old parent element (event.moved.oldContainer), and the new parent element (event.moved.newContainer).<\/li>\n<li>We then log these values to the console for demonstration purposes, but you can perform any necessary actions based on this information.<\/li>\n<\/ul>\n<p>This way, you can access both the dragged item and its old and new parent elements when using the vuedraggable library in Vue.js.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re using the vuedraggable library for drag-and-drop functionality within Vue.js, and you want to access the parent element of a draggable item, you can achieve this by handling the change event emitted by the vuedraggable component. The change event provides information about the dragged item and its new position, including its old and new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10513,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-10512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vue"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10512"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10512"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10512\/revisions"}],"predecessor-version":[{"id":10515,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10512\/revisions\/10515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10513"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}