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

feature: add drag select component #1249

Merged
merged 6 commits into from
Oct 24, 2018
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Understanding and learning this knowledge in advance will greatly help the use o
- Avatar Upload
- Back To Top
- Drag Dialog
- Drag Select
- Drag Kanban
- Drag List
- SplitPane
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
- 头像上传
- 返回顶部
- 拖拽Dialog
- 拖拽Select
- 拖拽看板
- 列表拖拽
- SplitPane
Expand Down
61 changes: 61 additions & 0 deletions src/components/DragSelect/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<template>
<el-select v-model="selectVal" v-bind="$attrs" class="drag-select" multiple>
<slot/>
</el-select>
</template>

<script>
import Sortable from 'sortablejs'

export default {
name: 'DragSelect',
props: {
value: {
type: Array,
required: true
}
},
computed: {
selectVal: {
get() {
return [...this.value]
},
set(val) {
this.$emit('input', [...val])
}
}
},
mounted() {
this.setSort()
},
methods: {
setSort() {
const el = document.querySelectorAll('.el-select__tags > span')[0]
this.sortable = Sortable.create(el, {
ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
setData: function(dataTransfer) {
dataTransfer.setData('Text', '')
// to avoid Firefox bug
// Detail see : https://github.com/RubaXa/Sortable/issues/1012
},
onEnd: evt => {
const targetRow = this.value.splice(evt.oldIndex, 1)[0]
this.value.splice(evt.newIndex, 0, targetRow)
}
})
}
}
}
</script>

<style scoped>
.drag-select >>> .sortable-ghost{
opacity: .8;
color: #fff!important;
background: #42b983!important;
}

.drag-select >>> .el-tag{
cursor: pointer;
}
</style>
1 change: 1 addition & 0 deletions src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
componentMixin: 'Mixin',
backToTop: 'BackToTop',
dragDialog: 'Drag Dialog',
dragSelect: 'Drag Select',
dragKanban: 'Drag Kanban',
charts: 'Charts',
keyboardChart: 'Keyboard Chart',
Expand Down
1 change: 1 addition & 0 deletions src/lang/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
componentMixin: 'Mixin',
backToTop: 'Ir arriba',
dragDialog: 'Drag Dialog',
dragSelect: 'Drag Select',
dragKanban: 'Drag Kanban',
charts: 'Gráficos',
keyboardChart: 'Keyboard Chart',
Expand Down
1 change: 1 addition & 0 deletions src/lang/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
componentMixin: '小组件',
backToTop: '返回顶部',
dragDialog: '拖拽 Dialog',
dragSelect: '拖拽 Select',
dragKanban: '可拖拽看板',
charts: '图表',
keyboardChart: '键盘图表',
Expand Down
6 changes: 6 additions & 0 deletions src/router/modules/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const componentsRouter = {
name: 'DragDialogDemo',
meta: { title: 'dragDialog' }
},
{
path: 'drag-select',
component: () => import('@/views/components-demo/dragSelect'),
name: 'DragSelectDemo',
meta: { title: 'dragSelect' }
},
{
path: 'dnd-list',
component: () => import('@/views/components-demo/dndList'),
Expand Down
43 changes: 43 additions & 0 deletions src/views/components-demo/dragSelect.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<template>
<div class="components-container">

<el-drag-select v-model="value" style="width:500px;" multiple placeholder="请选择">
<el-option v-for="item in options" :label="item.label" :value="item.value" :key="item.value" />
</el-drag-select>

<div style="margin-top:30px;">
<el-tag v-for="item of value" :key="item" style="margin-right:15px;">{{ item }}</el-tag>
</div>

</div>
</template>

<script>
import ElDragSelect from '@/components/DragSelect' // base on element-ui

export default {
name: 'DragSelectDemo',
components: { ElDragSelect },
data() {
return {
value: ['Apple', 'Banana', 'Orange'],
options: [{
value: 'Apple',
label: 'Apple'
}, {
value: 'Banana',
label: 'Banana'
}, {
value: 'Orange',
label: 'Orange'
}, {
value: 'Pear',
label: 'Pear'
}, {
value: 'Strawberry',
label: 'Strawberry'
}]
}
}
}
</script>