NestedDraggable.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <draggable tag="div"
  3. :list="lists"
  4. :group="{ name: 'docs-nested' }"
  5. :animation="150"
  6. :disabled="disabled"
  7. @sort="handleClick('sort')">
  8. <div v-for="detail in lists" :key="detail.id" class="docs-group">
  9. <div class="item">
  10. <div class="dashed"></div>
  11. <div class="header">
  12. <div class="tip"><Icon type="ios-folder-outline" /></div>
  13. <div class="title">{{ detail.title }}</div>
  14. </div>
  15. <div class="info">
  16. <Icon type="md-create" @click="handleClick('edit', detail)"/>
  17. <Icon type="md-add" @click="handleClick('add', detail)"/>
  18. <Icon type="md-trash" @click="handleClick('delete', detail)"/>
  19. </div>
  20. </div>
  21. <nested-draggable
  22. v-if="typeof detail.children === 'object' && detail.children !== null"
  23. :lists="detail.children"
  24. :isChildren="true"
  25. :disabled="disabled"
  26. @change="handleClick"/>
  27. </div>
  28. </draggable>
  29. </template>
  30. <style lang="scss" scoped>
  31. .docs-group {
  32. cursor: move;
  33. .docs-group {
  34. padding-left: 14px;
  35. background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDAgNzkuMTYwNDUxLCAyMDE3LzA1LzA2LTAxOjA4OjIxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+LUNEtwAAAEtJREFUSIntzzEVwAAMQkFSKfi3FKzQqQ5oJm5h5P3ZXQMYkrgwtk+OPo8kSzo7bGFcC+NaGNfCuBbGtTCuhXEtzB+SHAAGAEm/7wv2LKvDNoBjfgAAAABJRU5ErkJggg==) no-repeat -2px -9px;
  36. margin-left: 12px;
  37. border-left: 1px dotted #ddd;
  38. }
  39. .item {
  40. padding: 4px 0;
  41. background-color: #ffffff;
  42. border: solid 1px #ffffff;
  43. line-height: 24px;
  44. position: relative;
  45. .dashed {
  46. position: absolute;
  47. margin: 0;
  48. padding: 0;
  49. top: 16px;
  50. right: 0;
  51. left: 20px;
  52. height: 2px;
  53. border-width: 0 0 1px 0;
  54. border-bottom: dashed 1px #eee;
  55. }
  56. .header {
  57. display: inline-block;
  58. position: relative;
  59. background: #fff;
  60. padding: 0 8px;
  61. cursor: pointer;
  62. .tip {
  63. display: inline-block;
  64. position: relative;
  65. > i {
  66. display: inline-block;
  67. }
  68. }
  69. .title {
  70. display: inline-block;
  71. border-bottom: 1px solid transparent;
  72. cursor: pointer;
  73. padding: 0 4px;
  74. color: #555555;
  75. }
  76. }
  77. .info {
  78. position: absolute;
  79. background: #fff;
  80. padding-left: 12px;
  81. color: #666;
  82. right: 0;
  83. top: 5px;
  84. > i {
  85. padding: 0 2px;
  86. transition: all 0.2s;
  87. cursor: pointer;
  88. &:hover {
  89. transform: scale(1.2);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. </style>
  96. <script>
  97. import draggable from "vuedraggable";
  98. export default {
  99. name: "NestedDraggable",
  100. props: {
  101. lists: {
  102. required: true,
  103. type: Array
  104. },
  105. isChildren: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. disabled: {
  110. type: Boolean,
  111. default: false,
  112. }
  113. },
  114. data() {
  115. return {
  116. listSortData: '',
  117. }
  118. },
  119. components: {
  120. draggable
  121. },
  122. mounted() {
  123. this.listSortData = this.getSort(this.lists);
  124. },
  125. methods: {
  126. getSort(lists, parentid = 0) {
  127. let sortData = "";
  128. lists.forEach((item) => {
  129. sortData+= item.id + ":" + parentid + ";" + this.getSort(item.children, item.id);
  130. });
  131. return sortData;
  132. },
  133. handleClick(act, detail) {
  134. if (act == 'sort') {
  135. if (!this.isChildren) {
  136. let tempSortData = this.getSort(this.lists);
  137. if (tempSortData != this.listSortData) {
  138. this.listSortData = tempSortData;
  139. this.$emit("change", act, tempSortData)
  140. }
  141. }
  142. return;
  143. }
  144. this.$emit("change", act, detail)
  145. }
  146. }
  147. };
  148. </script>