Last active
October 31, 2022 17:44
-
-
Save blackmiaool/ccb74bc87eeeb71c864aeceaf197bdb4 to your computer and use it in GitHub Desktop.
Revisions
-
blackmiaool renamed this gist
Jun 27, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
blackmiaool created this gist
Jun 27, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ <template> <div ref="wrapper"> <div :key="tableKey"> <slot></slot> </div> </div> </template> <script> /* only for el-table must be used as: <SortableTable> <el-table ...> ... </el-table> </SortableTable> */ import sortable from "sortablejs"; export default { name: "SortableTable", data() { return { tableKey: 0 }; }, methods: { makeTableSortAble() { const table = this.$children[0].$el.querySelector( ".el-table__body-wrapper tbody" ); sortable.create(table, { handle: ".handle", animation: 150, onEnd: ({ newIndex, oldIndex }) => { this.keepWrapperHeight(true); this.tableKey = Math.random(); const arr = this.$children[0].data; const targetRow = arr.splice(oldIndex, 1)[0]; arr.splice(newIndex, 0, targetRow); } }); }, keepWrapperHeight(keep) { const wrapper = this.$refs.wrapper; if (keep) { wrapper.style.minHeight = `${wrapper.clientHeight}px`; } else { wrapper.style.minHeight = `auto`; } } }, mounted() { this.makeTableSortAble(); }, watch: { tableKey() { this.$nextTick(() => { this.makeTableSortAble(); this.keepWrapperHeight(false); }); } } }; </script>