searchTerm.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div>
  3. <!-- <div
  4. class="test_box"
  5. contenteditable="true"
  6. v-html="innerText"
  7. @input="handleInput"
  8. @focus="isChange = false"
  9. ></div>-->
  10. <div class="source" @click="getfouce">
  11. <div class="select">
  12. <transition-group name="flip-list">
  13. <span
  14. v-for="(item,index) in items"
  15. :key="item.conceptId"
  16. draggable="true"
  17. class="items"
  18. @dragstart="dragstart(item)"
  19. @dragenter="dragenter(item)"
  20. @dragend="dragend(item)"
  21. >
  22. {{item.conceptName}}
  23. <i class="el-icon-close" @click.stop="delTag(index)"></i>
  24. </span>
  25. </transition-group>
  26. <input
  27. class="inp"
  28. @mousedown.stop
  29. ref="inputVal"
  30. @blur="unblur"
  31. v-model="value"
  32. @input="handleInput"
  33. />
  34. </div>
  35. <ul class="list" v-if="conceptList.length>0 && onshow">
  36. <li
  37. @mousedown.prevent
  38. class="item"
  39. @click="getTag(item)"
  40. v-for="(item,index) in conceptList"
  41. :key="index"
  42. >
  43. {{item.conceptName}}
  44. <img src />
  45. </li>
  46. </ul>
  47. </div>
  48. </div>
  49. </template>
  50. <script>
  51. import api from '@api/knowledgeTree.js';
  52. export default {
  53. name: 'searchTerm',
  54. props: ['type'],
  55. data() {
  56. return {
  57. conceptList: [],
  58. oldNum: 0,
  59. newNum: 0,
  60. value: '',
  61. items: [],
  62. listL: [],
  63. onshow: false
  64. };
  65. },
  66. watch: {
  67. items(newVal, oldVal) {
  68. // TO DO
  69. let arr = [];
  70. newVal.forEach(item => {
  71. arr.push(item.conceptId);
  72. });
  73. this.$emit('updata:list',arr)
  74. }
  75. },
  76. methods: {
  77. handleInput(event) {
  78. const param = {
  79. excludedConceptIds: [],
  80. libType: this.type,
  81. name: this.value
  82. };
  83. api
  84. .searchConcept(param)
  85. .then(res => {
  86. if (res.data.code == '0') {
  87. const data = res.data.data;
  88. this.conceptList = data;
  89. this.onshow = true;
  90. }
  91. })
  92. .catch(error => {
  93. console.log(error);
  94. });
  95. },
  96. getfouce() {
  97. this.$refs.inputVal.focus();
  98. },
  99. getTag(item) {
  100. var v = this.items.some(el => {
  101. return el.conceptName == item.conceptName;
  102. });
  103. if (v) {
  104. return;
  105. }
  106. this.items.push(item);
  107. this.onshow = true;
  108. },
  109. delTag(index) {
  110. this.items.splice(index, 1);
  111. },
  112. unblur() {
  113. this.onshow = false;
  114. this.value = '';
  115. },
  116. shuffle() {
  117. this.items = _.shuffle(this.items);
  118. },
  119. // 记录初始信息
  120. dragstart(value) {
  121. this.oldNum = value;
  122. },
  123. // 做最终操作
  124. dragend(value) {
  125. if (this.oldNum != this.newNum) {
  126. let oldIndex = this.items.indexOf(this.oldNum);
  127. let newIndex = this.items.indexOf(this.newNum);
  128. let newItems = [...this.items];
  129. console.log(oldIndex);
  130. // 删除老的节点
  131. newItems.splice(oldIndex, 1);
  132. // 在列表中目标位置增加新的节点
  133. newItems.splice(newIndex, 0, this.oldNum);
  134. // this.items一改变,transition-group就起了作用
  135. this.items = [...newItems];
  136. }
  137. },
  138. // 记录移动过程中信息
  139. dragenter: function(value) {
  140. this.newNum = value;
  141. }
  142. }
  143. };
  144. </script>
  145. <style lang="less" scoped>
  146. .address-placeholder {
  147. line-height: 27px;
  148. height: 27px;
  149. color: #a2a2a2;
  150. position: absolute;
  151. left: 16px;
  152. top: 0;
  153. opacity: 0.7;
  154. font-size: 12px;
  155. }
  156. .test_box {
  157. width: 300px;
  158. min-height: 40px;
  159. max-height: 300px;
  160. outline: 0;
  161. border: 1px solid #dcdfe6;
  162. font-size: 14px;
  163. line-height: 40px;
  164. padding: 0 16px;
  165. word-wrap: break-word;
  166. overflow-x: hidden;
  167. overflow-y: auto;
  168. border-radius: 4px;
  169. margin-top: 6px;
  170. }
  171. .list {
  172. width: 318px;
  173. height: 200px;
  174. position: absolute;
  175. z-index: 1001;
  176. border: 1px solid #e4e7ed;
  177. border-radius: 4px;
  178. background-color: #fff;
  179. -webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  180. box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  181. -webkit-box-sizing: border-box;
  182. box-sizing: border-box;
  183. margin: 5px 0;
  184. overflow-y: auto;
  185. .item {
  186. padding: 0 10px;
  187. }
  188. .item:hover {
  189. background: #f5f7fa;
  190. }
  191. }
  192. .source {
  193. width: 300px;
  194. min-height: 40px;
  195. outline: 0;
  196. border: 1px solid #dcdfe6;
  197. font-size: 14px;
  198. line-height: 40px;
  199. padding: 0 16px;
  200. word-wrap: break-word;
  201. overflow-x: hidden;
  202. overflow-y: auto;
  203. border-radius: 4px;
  204. margin-top: 6px;
  205. .select {
  206. .items {
  207. height: 30px;
  208. line-height: 30px;
  209. font-size: 12px;
  210. border-radius: 5px;
  211. padding: 3px 4px;
  212. background: #48c5d7;
  213. margin: 0 5px;
  214. display: inline-block;
  215. }
  216. .inp {
  217. width: 80px;
  218. height: 30px;
  219. border: none;
  220. }
  221. }
  222. }
  223. </style>