NestedDraggable.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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"><img :src="detail.icon"/></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. > img {
  66. display: inline-block;
  67. width: 14px;
  68. height: 14px;
  69. margin-top: 5px;
  70. vertical-align: top;
  71. }
  72. }
  73. .title {
  74. display: inline-block;
  75. border-bottom: 1px solid transparent;
  76. cursor: pointer;
  77. padding: 0 3px;
  78. color: #555555;
  79. }
  80. }
  81. .info {
  82. position: absolute;
  83. background: #fff;
  84. padding-left: 12px;
  85. color: #666;
  86. right: 0;
  87. top: 5px;
  88. > i {
  89. padding: 0 2px;
  90. transition: all 0.2s;
  91. cursor: pointer;
  92. &:hover {
  93. transform: scale(1.2);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. </style>
  100. <script>
  101. import draggable from "vuedraggable";
  102. export default {
  103. name: "NestedDraggable",
  104. props: {
  105. lists: {
  106. required: true,
  107. type: Array
  108. },
  109. isChildren: {
  110. type: Boolean,
  111. default: false,
  112. },
  113. disabled: {
  114. type: Boolean,
  115. default: false,
  116. }
  117. },
  118. data() {
  119. return {
  120. listSortData: '',
  121. }
  122. },
  123. components: {
  124. draggable
  125. },
  126. mounted() {
  127. this.listSortData = this.getSort(this.lists);
  128. },
  129. methods: {
  130. getSort(lists, parentid = 0) {
  131. let sortData = "";
  132. lists.forEach((item) => {
  133. sortData+= item.id + ":" + parentid + ";" + this.getSort(item.children, item.id);
  134. });
  135. return sortData;
  136. },
  137. handleClick(act, detail) {
  138. if (act == 'sort') {
  139. if (this.isChildren) {
  140. this.$emit("change", act, detail);
  141. } else {
  142. let tempSortData = this.getSort(this.lists);
  143. if (tempSortData != this.listSortData) {
  144. this.listSortData = tempSortData;
  145. this.$emit("change", act, tempSortData);
  146. }
  147. }
  148. return;
  149. }
  150. this.$emit("change", act, detail)
  151. }
  152. }
  153. };
  154. </script>