ba-tree-picker.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. emits: ["select-change"],
  5. name: "ba-tree-picker",
  6. props: {
  7. valueKey: {
  8. type: String,
  9. default: "id"
  10. },
  11. textKey: {
  12. type: String,
  13. default: "name"
  14. },
  15. childrenKey: {
  16. type: String,
  17. default: "children"
  18. },
  19. localdata: {
  20. type: Array,
  21. default: function() {
  22. return [];
  23. }
  24. },
  25. localTreeList: {
  26. //在已经格式化好的数据
  27. type: Array,
  28. default: function() {
  29. return [];
  30. }
  31. },
  32. selectedData: {
  33. type: Array,
  34. default: function() {
  35. return [];
  36. }
  37. },
  38. title: {
  39. type: String,
  40. default: ""
  41. },
  42. multiple: {
  43. // 是否可以多选
  44. type: Boolean,
  45. default: true
  46. },
  47. selectParent: {
  48. //是否可以选父级
  49. type: Boolean,
  50. default: true
  51. },
  52. confirmColor: {
  53. // 确定按钮颜色
  54. type: String,
  55. default: ""
  56. // #0055ff
  57. },
  58. cancelColor: {
  59. // 取消按钮颜色
  60. type: String,
  61. default: ""
  62. // #757575
  63. },
  64. titleColor: {
  65. // 标题颜色
  66. type: String,
  67. default: ""
  68. //
  69. },
  70. switchColor: {
  71. // 节点切换图标颜色
  72. type: String,
  73. default: ""
  74. // #666
  75. },
  76. border: {
  77. // 是否有分割线
  78. type: Boolean,
  79. default: false
  80. }
  81. },
  82. data() {
  83. return {
  84. showDialog: false,
  85. treeList: []
  86. };
  87. },
  88. computed: {},
  89. methods: {
  90. _show() {
  91. this.showDialog = true;
  92. },
  93. _hide() {
  94. this.showDialog = false;
  95. },
  96. _cancel() {
  97. this._hide();
  98. this.$emit("cancel", "");
  99. },
  100. _confirm() {
  101. let selectedList = [];
  102. let selectedNames;
  103. let currentLevel = -1;
  104. this.treeList.forEach((item, index) => {
  105. if (currentLevel >= 0 && item.level > currentLevel)
  106. ;
  107. else {
  108. if (item.checkStatus === 2) {
  109. currentLevel = item.level;
  110. selectedList.push(item.id);
  111. selectedNames = selectedNames ? selectedNames + " / " + item.name : item.name;
  112. } else {
  113. currentLevel = -1;
  114. }
  115. }
  116. });
  117. this._hide();
  118. this.$emit("select-change", selectedList, selectedNames);
  119. },
  120. //格式化原数据(原数据为tree结构)
  121. _formatTreeData(list = [], level = 0, parentItem, isShowChild = true) {
  122. let nextIndex = 0;
  123. let parentId = -1;
  124. let initCheckStatus = 0;
  125. if (parentItem) {
  126. nextIndex = this.treeList.findIndex((item) => item.id === parentItem.id) + 1;
  127. parentId = parentItem.id;
  128. if (!this.multiple) {
  129. initCheckStatus = 0;
  130. } else
  131. initCheckStatus = parentItem.checkStatus == 2 ? 2 : 0;
  132. }
  133. list.forEach((item) => {
  134. let isLastLevel = true;
  135. if (item && item[this.childrenKey]) {
  136. let children = item[this.childrenKey];
  137. if (Array.isArray(children) && children.length > 0) {
  138. isLastLevel = false;
  139. }
  140. }
  141. let itemT = {
  142. id: item[this.valueKey],
  143. name: item[this.textKey],
  144. level,
  145. isLastLevel,
  146. isShow: isShowChild,
  147. isShowChild: false,
  148. checkStatus: initCheckStatus,
  149. orCheckStatus: 0,
  150. parentId,
  151. children: item[this.childrenKey],
  152. childCount: item[this.childrenKey] ? item[this.childrenKey].length : 0,
  153. childCheckCount: 0,
  154. childCheckPCount: 0
  155. };
  156. if (this.selectedData.indexOf(itemT.id) >= 0) {
  157. itemT.checkStatus = 2;
  158. itemT.orCheckStatus = 2;
  159. itemT.childCheckCount = itemT.children ? itemT.children.length : 0;
  160. this._onItemParentSelect(itemT, nextIndex);
  161. }
  162. this.treeList.splice(nextIndex, 0, itemT);
  163. nextIndex++;
  164. });
  165. },
  166. // 节点打开、关闭切换
  167. _onItemSwitch(item, index) {
  168. if (item.isLastLevel === true) {
  169. return;
  170. }
  171. item.isShowChild = !item.isShowChild;
  172. if (item.children) {
  173. this._formatTreeData(item.children, item.level + 1, item);
  174. item.children = void 0;
  175. } else {
  176. this._onItemChildSwitch(item, index);
  177. }
  178. },
  179. _onItemChildSwitch(item, index) {
  180. const firstChildIndex = index + 1;
  181. if (firstChildIndex > 0)
  182. for (var i = firstChildIndex; i < this.treeList.length; i++) {
  183. let itemChild = this.treeList[i];
  184. if (itemChild.level > item.level) {
  185. if (item.isShowChild) {
  186. if (itemChild.parentId === item.id) {
  187. itemChild.isShow = item.isShowChild;
  188. if (!itemChild.isShow) {
  189. itemChild.isShowChild = false;
  190. }
  191. }
  192. } else {
  193. itemChild.isShow = item.isShowChild;
  194. itemChild.isShowChild = false;
  195. }
  196. } else {
  197. return;
  198. }
  199. }
  200. },
  201. // 节点选中、取消选中
  202. _onItemSelect(item, index) {
  203. if (!this.multiple) {
  204. item.checkStatus = item.checkStatus == 0 ? 2 : 0;
  205. this.treeList.forEach((v, i) => {
  206. if (i != index) {
  207. this.treeList[i].checkStatus = 0;
  208. } else {
  209. this.treeList[i].checkStatus = 2;
  210. }
  211. });
  212. let selectedList = [];
  213. let selectedNames;
  214. selectedList.push(item.id);
  215. selectedNames = item.name;
  216. this._hide();
  217. this.$emit("select-change", selectedList, selectedNames);
  218. return;
  219. }
  220. let oldCheckStatus = item.checkStatus;
  221. switch (oldCheckStatus) {
  222. case 0:
  223. item.checkStatus = 2;
  224. item.childCheckCount = item.childCount;
  225. item.childCheckPCount = 0;
  226. break;
  227. case 1:
  228. case 2:
  229. item.checkStatus = 0;
  230. item.childCheckCount = 0;
  231. item.childCheckPCount = 0;
  232. break;
  233. }
  234. this._onItemChildSelect(item, index);
  235. this._onItemParentSelect(item, index, oldCheckStatus);
  236. },
  237. _onItemChildSelect(item, index) {
  238. if (item.childCount && item.childCount > 0) {
  239. index++;
  240. while (index < this.treeList.length && this.treeList[index].level > item.level) {
  241. let itemChild = this.treeList[index];
  242. itemChild.checkStatus = item.checkStatus;
  243. if (itemChild.checkStatus == 2) {
  244. itemChild.childCheckCount = itemChild.childCount;
  245. itemChild.childCheckPCount = 0;
  246. } else if (itemChild.checkStatus == 0) {
  247. itemChild.childCheckCount = 0;
  248. itemChild.childCheckPCount = 0;
  249. }
  250. index++;
  251. }
  252. }
  253. },
  254. _onItemParentSelect(item, index, oldCheckStatus) {
  255. const parentIndex = this.treeList.findIndex((itemP) => itemP.id == item.parentId);
  256. if (parentIndex >= 0) {
  257. let itemParent = this.treeList[parentIndex];
  258. itemParent.childCheckCount;
  259. let oldCheckStatusParent = itemParent.checkStatus;
  260. if (oldCheckStatus == 1) {
  261. itemParent.childCheckPCount -= 1;
  262. } else if (oldCheckStatus == 2) {
  263. itemParent.childCheckCount -= 1;
  264. }
  265. if (item.checkStatus == 1) {
  266. itemParent.childCheckPCount += 1;
  267. } else if (item.checkStatus == 2) {
  268. itemParent.childCheckCount += 1;
  269. }
  270. if (itemParent.childCheckCount <= 0 && itemParent.childCheckPCount <= 0) {
  271. itemParent.childCheckCount = 0;
  272. itemParent.childCheckPCount = 0;
  273. itemParent.checkStatus = 0;
  274. } else if (itemParent.childCheckCount >= itemParent.childCount) {
  275. itemParent.childCheckCount = itemParent.childCount;
  276. itemParent.childCheckPCount = 0;
  277. itemParent.checkStatus = 2;
  278. } else {
  279. itemParent.checkStatus = 1;
  280. }
  281. this._onItemParentSelect(itemParent, parentIndex, oldCheckStatusParent);
  282. }
  283. },
  284. // 重置数据
  285. _reTreeList() {
  286. this.treeList.forEach((v, i) => {
  287. this.treeList[i].checkStatus = v.orCheckStatus;
  288. });
  289. },
  290. _initTree() {
  291. this.treeList = [];
  292. this._formatTreeData(this.localdata);
  293. }
  294. },
  295. watch: {
  296. localdata() {
  297. this._initTree();
  298. },
  299. localTreeList() {
  300. this.treeList = this.localTreeList;
  301. }
  302. },
  303. mounted() {
  304. this._initTree();
  305. }
  306. };
  307. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  308. return {
  309. a: $data.showDialog ? 1 : "",
  310. b: common_vendor.o((...args) => $options._cancel && $options._cancel(...args)),
  311. c: $props.cancelColor,
  312. d: common_vendor.o((...args) => $options._cancel && $options._cancel(...args)),
  313. e: common_vendor.t($props.title),
  314. f: $props.titleColor,
  315. g: common_vendor.t($props.multiple ? "确定" : ""),
  316. h: $props.confirmColor,
  317. i: common_vendor.o((...args) => $options._confirm && $options._confirm(...args)),
  318. j: common_vendor.f($data.treeList, (item, index, i0) => {
  319. return common_vendor.e({
  320. a: !item.isLastLevel && item.isShowChild
  321. }, !item.isLastLevel && item.isShowChild ? {
  322. b: $props.switchColor
  323. } : !item.isLastLevel && !item.isShowChild ? {
  324. d: $props.switchColor
  325. } : {
  326. e: $props.switchColor
  327. }, {
  328. c: !item.isLastLevel && !item.isShowChild,
  329. f: common_vendor.o(($event) => $options._onItemSwitch(item, index), index),
  330. g: common_vendor.t(item.name + (item.childCount ? "(" + item.childCount + ")" : "")),
  331. h: $props.selectParent ? true : item.isLastLevel
  332. }, ($props.selectParent ? true : item.isLastLevel) ? common_vendor.e({
  333. i: item.checkStatus == 1
  334. }, item.checkStatus == 1 ? {
  335. j: $props.confirmColor,
  336. k: !$props.multiple ? 1 : "",
  337. l: $props.confirmColor
  338. } : item.checkStatus == 2 ? {
  339. n: $props.confirmColor,
  340. o: !$props.multiple ? 1 : "",
  341. p: $props.confirmColor
  342. } : {
  343. q: !$props.multiple ? 1 : "",
  344. r: $props.confirmColor
  345. }, {
  346. m: item.checkStatus == 2
  347. }) : {}, {
  348. s: common_vendor.o(($event) => $options._onItemSelect(item, index), index),
  349. t: common_vendor.s({
  350. paddingLeft: item.level * 30 + "rpx"
  351. }),
  352. v: item.isShow ? 1 : "",
  353. w: index
  354. });
  355. }),
  356. k: $props.border === true ? 1 : "",
  357. l: $data.showDialog ? 1 : ""
  358. };
  359. }
  360. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-6e9e5013"], ["__file", "F:/兴元/开门柜项目/平台端管理系统小程序/src/components/ba-tree-picker/ba-tree-picker.vue"]]);
  361. wx.createComponent(Component);