tree-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. middleValue: []
  73. };
  74. },
  75. mounted() {
  76. this.getAccountId();
  77. },
  78. methods: {
  79. ...mapGetters(['userInfo']),
  80. getAccountId() {
  81. postAction('/ctop/projectMember/participateListByMediaId', {
  82. userId: this.userInfo().id,
  83. type: 'kuaishou'
  84. }).then(result => {
  85. if (result.result.length) {
  86. this.spinning = false;
  87. this.treeData = result.result.map(item => {
  88. return {
  89. key: item.projectId + 'projectId',
  90. title: item.projectName + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + item.advertiserName,
  91. children: item.accountList
  92. ? item.accountList.map(i => {
  93. return {
  94. key: i.accountId,
  95. title:
  96. (i.userName.length === 2
  97. ? i.userName + '\xa0\xa0\xa0\xa0\xa0'
  98. : i.userName.length === 3
  99. ? i.userName + '\xa0\xa0\xa0\xa0'
  100. : i.userName)
  101. + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'
  102. + i.accountId
  103. + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'
  104. + i.authName
  105. };
  106. }) : []
  107. };
  108. });
  109. this.updateTreeData = [...this.treeData];
  110. }
  111. else {
  112. this.spinning = false;
  113. this.leftTreeNoData = true;
  114. }
  115. });
  116. },
  117. handleDelTree(list) {
  118. const defaultTypeOption = JSON.parse(JSON.stringify(this.applyTypeOption));
  119. const defaultCheckKey = JSON.parse(JSON.stringify(this.checkedKeys));
  120. const len = defaultTypeOption.length;
  121. const keysLen = defaultCheckKey.length;
  122. for(let i = len - 1; i >= 0; i--) {
  123. if(defaultTypeOption[i].key === list.key) {
  124. defaultTypeOption.splice(i, 1);
  125. }
  126. }
  127. this.applyTypeOption = defaultTypeOption;
  128. this.selectRightData = new Set(defaultTypeOption);
  129. const txt = this.handleDelFindId(this.treeData, list.key);
  130. const lastIndex = defaultCheckKey.findIndex(item => item === txt);
  131. let value = null;
  132. if (lastIndex > -1) {
  133. value = defaultCheckKey.findIndex(item => item === txt);
  134. for(let i = keysLen - 1; i >= 0; i--) {
  135. if(defaultCheckKey[i] === list.key) {
  136. defaultCheckKey.splice(i, 1);
  137. }
  138. }
  139. }
  140. else {
  141. value = defaultCheckKey.findIndex(item => item === list.key);
  142. }
  143. defaultCheckKey.splice(value, 1);
  144. this.checkedKeys = defaultCheckKey;
  145. this.$emit('applyType', this.applyTypeOption);
  146. },
  147. onChange(e) {
  148. const value = e.target.value;
  149. this.treeData = value !== '' ? this.getNewTreeData(this.updateTreeData, value) : [...this.updateTreeData];
  150. if(value !== '') {
  151. this.middleValue = this.checkedKeys.filter(item=>typeof item === 'number');
  152. }
  153. else {
  154. this.middleValue = [];
  155. }
  156. if (!this.treeData.length) {
  157. this.spinning = false;
  158. this.leftTreeNoData = true;
  159. }
  160. else {
  161. this.leftTreeNoData = false;
  162. }
  163. },
  164. handleDelFindId(treeData, value) {
  165. if (!treeData) {
  166. return null;
  167. }
  168. let node = null;
  169. let children = null;
  170. let text = '';
  171. for (let i = 0; i < treeData.length; i++) {
  172. node = treeData[i];
  173. if (node.children) {
  174. children = node.children;
  175. text = node.key;
  176. for (let j = 0; j < children.length; j++) {
  177. if (children[j].key === value) {
  178. return text;
  179. }
  180. }
  181. }
  182. }
  183. },
  184. // 根据关键词查询所有相关的tree节点
  185. getNewTreeData(treeData, value) {
  186. if (!treeData) {
  187. return null;
  188. }
  189. let newTreeData = new Array();
  190. let node = null;
  191. let children = null;
  192. let text = '';
  193. for (let i = 0; i < treeData.length; i++) { // 多个根节点开始遍历
  194. node = treeData[i];
  195. if (node.children) {
  196. children = node.children;
  197. }
  198. text = node.title;
  199. if (text.indexOf(value) > -1) {
  200. newTreeData.push(node);
  201. continue;
  202. }
  203. else {
  204. if (children) {
  205. let newNodes = this.getNewTreeData(node.children, value);
  206. if (newNodes.length > 0) {
  207. node.children = newNodes;
  208. newTreeData.push(node);
  209. }
  210. }
  211. }
  212. }
  213. return newTreeData;
  214. },
  215. onExpand(expandedKeys) {
  216. this.expandedKeys = expandedKeys;
  217. this.autoExpandParent = false;
  218. },
  219. // 去重的方法
  220. Es6duplicate(arr, type) {
  221. if (!arr.length){
  222. return arr;
  223. }
  224. else {
  225. if (type) {
  226. let obj = {};
  227. let newArr = arr.reduce((cur, next) => {
  228. obj[next.key] ? '' : obj[next.key] = true && cur.push(next);
  229. return cur;
  230. }, []);
  231. return newArr;
  232. }
  233. else {
  234. return Array.from(new Set(arr));
  235. }
  236. }
  237. },
  238. onCheck(checkedKeys) {
  239. this.nodes(this.treeData, checkedKeys);
  240. const filterCheckKey = this.checkedKeys.filter(item => typeof item === 'number');
  241. let filterCheckKeySet = new Set(filterCheckKey.concat(this.middleValue));
  242. let selectRightDataSet = new Set(this.selectRightData);
  243. let difference = new Set([...selectRightDataSet].filter(item => !filterCheckKeySet.has(item.key)));
  244. this.selectRightData.delete(...difference);
  245. this.applyTypeOption = this.Es6duplicate([...this.selectRightData], 'key');
  246. const changeSelectData = this.applyTypeOption.map(item => item.key);
  247. this.checkedKeys = changeSelectData;
  248. if (this.applyTypeOption.length > 100) {
  249. const lastOptionKey = this.applyTypeOption[99].key;
  250. const lastIndex = changeSelectData.findIndex(item => item === lastOptionKey);
  251. this.checkedKeys = changeSelectData.slice(0, lastIndex + 1);
  252. this.applyTypeOption = [...this.selectRightData].slice(0, 100);
  253. }
  254. this.$emit('applyType', this.applyTypeOption);
  255. },
  256. nodes(data, checkedKeys) {
  257. data.forEach(item => {
  258. if (item.children) {
  259. this.nodes(item.children, checkedKeys);
  260. }
  261. else {
  262. if (checkedKeys.indexOf(item.key) !== -1) {
  263. this.selectRightData.add(item);
  264. }
  265. else {
  266. this.selectRightData.delete(item);
  267. }
  268. }
  269. });
  270. }
  271. }
  272. };
  273. </script>
  274. <style lang="less" scoped>
  275. @import 'tree-select';
  276. </style>