Skip to content

Commit 1a345a7

Browse files
authored
feature: add drag select component (#1249)
1 parent ec58373 commit 1a345a7

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Understanding and learning this knowledge in advance will greatly help the use o
118118
- Avatar Upload
119119
- Back To Top
120120
- Drag Dialog
121+
- Drag Select
121122
- Drag Kanban
122123
- Drag List
123124
- SplitPane

README.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
- 头像上传
131131
- 返回顶部
132132
- 拖拽Dialog
133+
- 拖拽Select
133134
- 拖拽看板
134135
- 列表拖拽
135136
- SplitPane

src/components/DragSelect/index.vue

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<template>
2+
<el-select v-model="selectVal" v-bind="$attrs" class="drag-select" multiple>
3+
<slot/>
4+
</el-select>
5+
</template>
6+
7+
<script>
8+
import Sortable from 'sortablejs'
9+
10+
export default {
11+
name: 'DragSelect',
12+
props: {
13+
value: {
14+
type: Array,
15+
required: true
16+
}
17+
},
18+
computed: {
19+
selectVal: {
20+
get() {
21+
return [...this.value]
22+
},
23+
set(val) {
24+
this.$emit('input', [...val])
25+
}
26+
}
27+
},
28+
mounted() {
29+
this.setSort()
30+
},
31+
methods: {
32+
setSort() {
33+
const el = document.querySelectorAll('.el-select__tags > span')[0]
34+
this.sortable = Sortable.create(el, {
35+
ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
36+
setData: function(dataTransfer) {
37+
dataTransfer.setData('Text', '')
38+
// to avoid Firefox bug
39+
// Detail see : https://github.com/RubaXa/Sortable/issues/1012
40+
},
41+
onEnd: evt => {
42+
const targetRow = this.value.splice(evt.oldIndex, 1)[0]
43+
this.value.splice(evt.newIndex, 0, targetRow)
44+
}
45+
})
46+
}
47+
}
48+
}
49+
</script>
50+
51+
<style scoped>
52+
.drag-select >>> .sortable-ghost{
53+
opacity: .8;
54+
color: #fff!important;
55+
background: #42b983!important;
56+
}
57+
58+
.drag-select >>> .el-tag{
59+
cursor: pointer;
60+
}
61+
</style>

src/lang/en.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default {
2222
componentMixin: 'Mixin',
2323
backToTop: 'BackToTop',
2424
dragDialog: 'Drag Dialog',
25+
dragSelect: 'Drag Select',
2526
dragKanban: 'Drag Kanban',
2627
charts: 'Charts',
2728
keyboardChart: 'Keyboard Chart',

src/lang/es.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default {
2222
componentMixin: 'Mixin',
2323
backToTop: 'Ir arriba',
2424
dragDialog: 'Drag Dialog',
25+
dragSelect: 'Drag Select',
2526
dragKanban: 'Drag Kanban',
2627
charts: 'Gráficos',
2728
keyboardChart: 'Keyboard Chart',

src/lang/zh.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default {
2222
componentMixin: '小组件',
2323
backToTop: '返回顶部',
2424
dragDialog: '拖拽 Dialog',
25+
dragSelect: '拖拽 Select',
2526
dragKanban: '可拖拽看板',
2627
charts: '图表',
2728
keyboardChart: '键盘图表',

src/router/modules/components.js

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ const componentsRouter = {
7878
name: 'DragDialogDemo',
7979
meta: { title: 'dragDialog' }
8080
},
81+
{
82+
path: 'drag-select',
83+
component: () => import('@/views/components-demo/dragSelect'),
84+
name: 'DragSelectDemo',
85+
meta: { title: 'dragSelect' }
86+
},
8187
{
8288
path: 'dnd-list',
8389
component: () => import('@/views/components-demo/dndList'),
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<template>
2+
<div class="components-container">
3+
4+
<el-drag-select v-model="value" style="width:500px;" multiple placeholder="请选择">
5+
<el-option v-for="item in options" :label="item.label" :value="item.value" :key="item.value" />
6+
</el-drag-select>
7+
8+
<div style="margin-top:30px;">
9+
<el-tag v-for="item of value" :key="item" style="margin-right:15px;">{{ item }}</el-tag>
10+
</div>
11+
12+
</div>
13+
</template>
14+
15+
<script>
16+
import ElDragSelect from '@/components/DragSelect' // base on element-ui
17+
18+
export default {
19+
name: 'DragSelectDemo',
20+
components: { ElDragSelect },
21+
data() {
22+
return {
23+
value: ['Apple', 'Banana', 'Orange'],
24+
options: [{
25+
value: 'Apple',
26+
label: 'Apple'
27+
}, {
28+
value: 'Banana',
29+
label: 'Banana'
30+
}, {
31+
value: 'Orange',
32+
label: 'Orange'
33+
}, {
34+
value: 'Pear',
35+
label: 'Pear'
36+
}, {
37+
value: 'Strawberry',
38+
label: 'Strawberry'
39+
}]
40+
}
41+
}
42+
}
43+
</script>

0 commit comments

Comments
 (0)