Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'beforeAdded' prop and example #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions example/components/two-lists-before-added.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<template>
<div class="row">
<div class="col-3">
<h3>Draggable 1</h3>
<draggable
class="list-group"
:list="list1"
group="people"
@change="log"
itemKey="name"
>
<template #item="{ element, index }">
<div class="list-group-item">{{ element.name }} {{ index }}</div>
</template>
</draggable>
</div>

<div class="col-3">
<h3>Draggable 2</h3>
<draggable
class="list-group"
:list="list2"
group="people"
@change="log"
itemKey="name"
:beforeAdded="handleBeforeAdded"
>
<template #item="{ element, index }">
<div class="list-group-item">{{ element.name }} {{ index }}</div>
</template>
</draggable>
</div>

<rawDisplayer class="col-3" :value="list1" title="List 1" />

<rawDisplayer class="col-3" :value="list2" title="List 2" />
</div>
</template>
<script>
import draggable from "@/vuedraggable";

export default {
name: "two-lists-before-added",
display: "Two Lists Before Added",
order: 1.5,
components: {
draggable
},
data() {
return {
list1: [
{ name: "John", id: 1 },
{ name: "Joao", id: 2 },
{ name: "Jean", id: 3 },
{ name: "Gerard", id: 4 }
],
list2: [
{ name: "Juan", id: 5 },
{ name: "Edgard", id: 6 },
{ name: "Johnson", id: 7 }
]
};
},
methods: {
handleBeforeAdded(element) {
return {
...element,
name: element.name + " (added)",
age: 18
};
},
log: function(evt) {
window.console.log(evt);
}
}
};
</script>
15 changes: 14 additions & 1 deletion src/vuedraggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const props = {
type: Object,
required: false,
default: null
},
beforeAdded: {
type: Function,
default: null
}
};

Expand Down Expand Up @@ -238,12 +242,21 @@ const draggableComponent = defineComponent({
},

onDragAdd(evt) {
const element = evt.item._underlying_vm_;
let element = evt.item._underlying_vm_;
if (element === undefined) {
return;
}
removeNode(evt.item);
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);

// handle element before add
if (this.beforeAdded) {
const newElement = this.beforeAdded(element);
if (newElement !== undefined) {
element = newElement;
}
}

// @ts-ignore
this.spliceList(newIndex, 0, element);
const added = { element, newIndex };
Expand Down