tree-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. this.$emit('applyType', this.applyTypeOption);
  145. },
  146. onChange(e) {
  147. const value = e.target.value;
  148. this.treeData = value !== '' ? this.getNewTreeData(this.updateTreeData, value) : [...this.updateTreeData];
  149. if (!this.treeData.length) {
  150. this.spinning = false;
  151. this.leftTreeNoData = true;
  152. }
  153. else {
  154. this.leftTreeNoData = false;
  155. }
  156. },
  157. handleDelFindId(treeData, value) {
  158. if (!treeData) {
  159. return null;
  160. }
  161. let node = null;
  162. let children = null;
  163. let text = '';
  164. for (let i = 0; i < treeData.length; i++) {
  165. node = treeData[i];
  166. if (node.children) {
  167. children = node.children;
  168. text = node.key;
  169. for (let j = 0; j < children.length; j++) {
  170. if (children[j].key === value) {
  171. return text;
  172. }
  173. }
  174. }
  175. }
  176. },
  177. // 根据关键词查询所有相关的tree节点
  178. getNewTreeData(treeData, value) {
  179. if (!treeData) {
  180. return null;
  181. }
  182. let newTreeData = new Array();
  183. let node = null;
  184. let children = null;
  185. let text = '';
  186. for (let i = 0; i < treeData.length; i++) { // 多个根节点开始遍历
  187. node = treeData[i];
  188. if (node.children) {
  189. children = node.children;
  190. }
  191. text = node.title;
  192. if (text.indexOf(value) > -1) {
  193. newTreeData.push(node);
  194. continue;
  195. }
  196. else {
  197. if (children) {
  198. let newNodes = this.getNewTreeData(node.children, value);
  199. if (newNodes.length > 0) {
  200. node.children = newNodes;
  201. newTreeData.push(node);
  202. }
  203. }
  204. }
  205. }
  206. return newTreeData;
  207. },
  208. onExpand(expandedKeys) {
  209. this.expandedKeys = expandedKeys;
  210. this.autoExpandParent = false;
  211. },
  212. // 去重的方法
  213. Es6duplicate(arr, type) {
  214. if (!arr.length){
  215. return arr;
  216. }
  217. else {
  218. if (type) {
  219. let obj = {};
  220. let newArr = arr.reduce((cur, next) => {
  221. obj[next.key] ? '' : obj[next.key] = true && cur.push(next);
  222. return cur;
  223. }, []);
  224. return newArr;
  225. }
  226. else {
  227. return Array.from(new Set(arr));
  228. }
  229. }
  230. },
  231. onCheck(checkedKeys) {
  232. // this.checkedKeys = checkedKeys;
  233. this.nodes(this.treeData, checkedKeys);
  234. const filterCheckKey = this.checkedKeys.filter(item=>typeof item ==='number');
  235. let filterCheckKeySet = new Set(filterCheckKey);
  236. let selectRightDataSet = new Set(this.selectRightData);
  237. let difference = new Set([...selectRightDataSet].filter((item) => !filterCheckKeySet.has(item.key)));
  238. this.selectRightData.delete(...difference);
  239. this.applyTypeOption = this.Es6duplicate([...this.selectRightData], 'key');
  240. const changeSelectData = this.applyTypeOption.map(item => item.key);
  241. this.checkedKeys = changeSelectData;
  242. if (this.applyTypeOption.length > 100) {
  243. const lastOptionKey = this.applyTypeOption[99].key;
  244. const lastIndex = changeSelectData.findIndex(item => item === lastOptionKey);
  245. this.checkedKeys = changeSelectData.slice(0, lastIndex + 1);
  246. this.applyTypeOption = [...this.selectRightData].slice(0, 100);
  247. }
  248. this.$emit('applyType', this.applyTypeOption);
  249. },
  250. nodes(data, checkedKeys) {
  251. data.forEach(item => {
  252. if (item.children) {
  253. this.nodes(item.children, checkedKeys);
  254. }
  255. else {
  256. if (checkedKeys.indexOf(item.key) !== -1) {
  257. this.selectRightData.add(item);
  258. }
  259. else {
  260. this.selectRightData.delete(item);
  261. }
  262. }
  263. });
  264. }
  265. }
  266. };
  267. </script>
  268. <style lang="less" scoped>
  269. @import 'tree-select';
  270. </style>