tree-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <div class="tree-select-content">
  3. <a-input-search class="input-search" placeholder="请输入账户名或ID" @change="onChange" />
  4. <div class="tree-select-class">
  5. <div class="select-class-left">
  6. <div class="class-left-header">选择分享的账户名称</div>
  7. <div class="class-left-content">
  8. <template>
  9. <a-tree
  10. v-model="checkedKeys"
  11. checkable
  12. :expanded-keys="expandedKeys"
  13. :auto-expand-parent="autoExpandParent"
  14. :selected-keys="selectedKeys"
  15. :tree-data="treeData"
  16. @check="onCheck"
  17. @expand="onExpand"
  18. />
  19. </template>
  20. <div class="example">
  21. <a-spin :spinning="spinning"/>
  22. <span v-if="leftTreeNoData">暂无数据</span>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="select-class-right">
  27. <div class="class-left-header">
  28. 已选 ({{ applyTypeOption.length ? applyTypeOption.length : 0}}/100)
  29. <a-popover placement="topRight">
  30. <template slot="content">
  31. <p>1、输入账户超出推送范围,请检查是否同主体账户</p>
  32. <p>2、有效账户将不会重复推送,请登录该账户查看使用</p>
  33. </template>
  34. <span><a-icon type="question-circle" /></span>
  35. </a-popover>
  36. </div>
  37. <div v-if="applyTypeOption.length" class="class-left-content">
  38. <div class="apply-right-tree" v-for="item in applyTypeOption" :key="item.key">
  39. <p class="left-content-p">
  40. <a-tooltip placement="left">
  41. <template slot="title">
  42. <span>{{ item.title }}</span>
  43. </template>
  44. {{ item.title }}
  45. </a-tooltip>
  46. </p>
  47. <a-icon type="delete" class="delete-type-class" @click="handleDelTree(item)" />
  48. </div>
  49. </div>
  50. <div v-else class="no-data-class">暂无数据</div>
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import {mapGetters} from 'vuex';
  57. import {postAction} from '@/api/manage';
  58. export default {
  59. name: 'tree-select',
  60. data() {
  61. return {
  62. expandedKeys: [],
  63. autoExpandParent: true,
  64. checkedKeys: [],
  65. selectedKeys: [],
  66. treeData: [],
  67. updateTreeData: [],
  68. applyTypeOption: [],
  69. selectRightData: new Set(),
  70. spinning: true,
  71. leftTreeNoData: false
  72. };
  73. },
  74. mounted() {
  75. this.getAccountId();
  76. },
  77. methods: {
  78. ...mapGetters(['userInfo']),
  79. getAccountId() {
  80. postAction('/ctop/projectMember/participateListByMediaId', {
  81. userId: this.userInfo().id,
  82. type: 'kuaishou'
  83. }).then(result => {
  84. if (result.result.length) {
  85. this.spinning = false;
  86. this.treeData = result.result.map(item => {
  87. return {
  88. key: item.projectId + 'projectId',
  89. title: item.projectName + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + item.advertiserName,
  90. children: item.accountList
  91. ? item.accountList.map(i => {
  92. return {
  93. key: i.accountId,
  94. title:
  95. (i.userName.length === 2
  96. ? i.userName + '\xa0\xa0\xa0\xa0\xa0'
  97. : i.userName.length === 3
  98. ? i.userName + '\xa0\xa0\xa0\xa0'
  99. : i.userName)
  100. + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'
  101. + i.accountId
  102. + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'
  103. + i.authName
  104. };
  105. }) : []
  106. };
  107. });
  108. this.updateTreeData = [...this.treeData];
  109. }
  110. else {
  111. this.spinning = false;
  112. this.leftTreeNoData = true;
  113. }
  114. });
  115. },
  116. handleDelTree(list) {
  117. const defaultTypeOption = JSON.parse(JSON.stringify(this.applyTypeOption));
  118. const defaultCheckKey = JSON.parse(JSON.stringify(this.checkedKeys));
  119. const len = defaultTypeOption.length;
  120. const keysLen = defaultCheckKey.length;
  121. for(let i = len - 1; i >= 0; i--) {
  122. if(defaultTypeOption[i].key === list.key) {
  123. defaultTypeOption.splice(i, 1);
  124. }
  125. }
  126. this.applyTypeOption = defaultTypeOption;
  127. this.selectRightData = new Set(defaultTypeOption);
  128. const txt = this.handleDelFindId(this.treeData, list.key);
  129. const lastIndex = defaultCheckKey.findIndex(item => item === txt);
  130. let value = null;
  131. if (lastIndex > -1) {
  132. value = defaultCheckKey.findIndex(item => item === txt);
  133. for(let i = keysLen - 1; i >= 0; i--) {
  134. if(defaultCheckKey[i] === list.key) {
  135. defaultCheckKey.splice(i, 1);
  136. }
  137. }
  138. }
  139. else {
  140. value = defaultCheckKey.findIndex(item => item === list.key);
  141. }
  142. defaultCheckKey.splice(value, 1);
  143. this.checkedKeys = defaultCheckKey;
  144. },
  145. onChange(e) {
  146. const value = e.target.value;
  147. this.treeData = value !== '' ? this.getNewTreeData(this.updateTreeData, value) : [...this.updateTreeData];
  148. if (!this.treeData.length) {
  149. this.spinning = false;
  150. this.leftTreeNoData = true;
  151. }
  152. else {
  153. this.leftTreeNoData = false;
  154. }
  155. },
  156. handleDelFindId(treeData, value) {
  157. if (!treeData) {
  158. return null;
  159. }
  160. let node = null;
  161. let children = null;
  162. let text = '';
  163. for (let i = 0; i < treeData.length; i++) {
  164. node = treeData[i];
  165. if (node.children) {
  166. children = node.children;
  167. text = node.key;
  168. for (let j = 0; j < children.length; j++) {
  169. if (children[j].key === value) {
  170. return text;
  171. }
  172. }
  173. }
  174. }
  175. },
  176. // 根据关键词查询所有相关的tree节点
  177. getNewTreeData(treeData, value) {
  178. if (!treeData) {
  179. return null;
  180. }
  181. let newTreeData = new Array();
  182. let node = null;
  183. let children = null;
  184. let text = '';
  185. for (let i = 0; i < treeData.length; i++) { // 多个根节点开始遍历
  186. node = treeData[i];
  187. if (node.children) {
  188. children = node.children;
  189. }
  190. text = node.title;
  191. if (text.indexOf(value) > -1) {
  192. newTreeData.push(node);
  193. continue;
  194. }
  195. else {
  196. if (children) {
  197. let newNodes = this.getNewTreeData(node.children, value);
  198. if (newNodes.length > 0) {
  199. node.children = newNodes;
  200. newTreeData.push(node);
  201. }
  202. }
  203. }
  204. }
  205. return newTreeData;
  206. },
  207. onExpand(expandedKeys) {
  208. this.expandedKeys = expandedKeys;
  209. this.autoExpandParent = false;
  210. },
  211. // 去重的方法
  212. Es6duplicate(arr, type) {
  213. if (!arr.length){
  214. return arr;
  215. }
  216. else {
  217. if (type) {
  218. let obj = {};
  219. let newArr = arr.reduce((cur, next) => {
  220. obj[next.key] ? '' : obj[next.key] = true && cur.push(next);
  221. return cur;
  222. }, []);
  223. return newArr;
  224. }
  225. else {
  226. return Array.from(new Set(arr));
  227. }
  228. }
  229. },
  230. onCheck(checkedKeys) {
  231. this.checkedKeys = checkedKeys;
  232. this.nodes(this.treeData, checkedKeys);
  233. this.applyTypeOption = this.Es6duplicate([...this.selectRightData], 'key');
  234. const changeSelectData = this.applyTypeOption.map(item => item.key);
  235. this.checkedKeys = changeSelectData;
  236. if (this.applyTypeOption.length > 100) {
  237. const lastOptionKey = this.applyTypeOption[99].key;
  238. const lastIndex = changeSelectData.findIndex(item => item === lastOptionKey);
  239. this.checkedKeys = changeSelectData.slice(0, lastIndex + 1);
  240. this.applyTypeOption = [...this.selectRightData].slice(0, 100);
  241. }
  242. this.$emit('applyType', this.applyTypeOption);
  243. },
  244. nodes(data, checkedKeys) {
  245. if (!checkedKeys.length) {
  246. this.selectRightData = new Set();
  247. return;
  248. }
  249. data.forEach(item => {
  250. if (item.children) {
  251. this.nodes(item.children, checkedKeys);
  252. }
  253. else {
  254. if (checkedKeys.indexOf(item.key) !== -1) {
  255. this.selectRightData.add(item);
  256. }
  257. else {
  258. this.selectRightData.delete(item);
  259. }
  260. }
  261. });
  262. }
  263. }
  264. };
  265. </script>
  266. <style lang="less" scoped>
  267. @import 'tree-select';
  268. </style>