dk-tree-form-more.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. const config = require('@/config/config.js')
  2. const tranverse = (e, itemListKey) => {
  3. for (let i in e) {
  4. if (e[i][itemListKey]) {
  5. e[i].open = false;
  6. tranverse(e[i][itemListKey], itemListKey)
  7. }
  8. e[i].selected = false;
  9. }
  10. }
  11. const tranverseTrue = (e, itemListKey) => {
  12. for (let i in e) {
  13. if (e[i][itemListKey]) {
  14. e[i].open = true;
  15. tranverseTrue(e[i][itemListKey], itemListKey)
  16. }
  17. e[i].selected = true;
  18. }
  19. }
  20. Component({
  21. properties: {
  22. treeList: Object,
  23. depth: {
  24. type: Number,
  25. value: 0
  26. },
  27. itemListKey: { //子list key名
  28. type: String,
  29. value: ""
  30. },
  31. parentIdKey: { //父级的key名
  32. type: String,
  33. value: ""
  34. },
  35. idKey: { // id 的key名
  36. type: String,
  37. value: ""
  38. },
  39. flgValidOnFlag: { //默认显示启用
  40. type: Boolean,
  41. value: true
  42. },
  43. nameKey: { //显示的名字的key
  44. type: String,
  45. value: ""
  46. },
  47. flgValidKey: { //启用停用的key
  48. type: String,
  49. value: ""
  50. },
  51. ischekboxFlag: { //是否显示cheakbox
  52. type: Boolean,
  53. value: false
  54. },
  55. flgValid: {
  56. type: Boolean,
  57. value: true
  58. },
  59. swipeDisabled: {
  60. type: Boolean,
  61. value: false
  62. },
  63. isNameImgFlag: { //是否显示名字前图片
  64. type: Boolean,
  65. value: true
  66. },
  67. service: {
  68. type: String,
  69. },
  70. isLastClickFlag: { //控制是否只可以点击末级
  71. type: Boolean,
  72. value: false
  73. },
  74. },
  75. data: {
  76. imageUrl: config.image_url + '/static/img/',
  77. },
  78. ready() {
  79. // const {treeList}=this.properties;
  80. const { treeList } = this.data;
  81. const { itemListKey } = this.data;
  82. const { parentIdKey } = this.data;
  83. for (let i in treeList) {
  84. if (treeList[i][itemListKey]) {
  85. if (treeList[i][parentIdKey]) {
  86. treeList[i].open = false;
  87. } else {
  88. treeList[i].open = true;
  89. }
  90. tranverse(treeList[i][itemListKey], itemListKey);
  91. }
  92. treeList[i].selected = false;
  93. }
  94. this.setData({
  95. tree: treeList
  96. })
  97. },
  98. methods: {
  99. //停用启用方法
  100. onSwipeBind(e) {
  101. this.triggerEvent('onSwipe', e);
  102. },
  103. subonSwipeBind_(e) {
  104. this.triggerEvent('onSwipe', e.detail);
  105. },
  106. // onSwipeBind_(e) {
  107. // this.triggerEvent('onSwipe_', e);
  108. // },
  109. //设置控件信息
  110. setDataFromPage(data) {
  111. const { itemListKey } = this.data;
  112. const { parentIdKey } = this.data;
  113. let treeList = data
  114. for (let i in treeList) {
  115. if (treeList[i][itemListKey]) {
  116. if (treeList[i][parentIdKey]) {
  117. treeList[i].open = false;
  118. } else {
  119. treeList[i].open = true;
  120. }
  121. tranverse(treeList[i][itemListKey], itemListKey);
  122. }
  123. treeList[i].selected = false;
  124. }
  125. this.setData({
  126. tree: treeList
  127. })
  128. },
  129. //设置控件信息
  130. setDataFromPageTrue(data) {
  131. const { itemListKey } = this.data;
  132. const { parentIdKey } = this.data;
  133. let treeList = data
  134. for (let i in treeList) {
  135. if (treeList[i][itemListKey]) {
  136. if (treeList[i][parentIdKey]) {
  137. treeList[i].open = true;
  138. } else {
  139. treeList[i].open = true;
  140. }
  141. tranverseTrue(treeList[i][itemListKey], itemListKey);
  142. }
  143. treeList[i].selected = true;
  144. }
  145. this.setData({
  146. tree: treeList
  147. })
  148. },
  149. //修改折叠状态
  150. changeOpen(tree, id, itemListKey, idKey) {
  151. for (let i = 0; i < tree.length; i += 1) {
  152. if (tree[i][idKey] === id) {
  153. tree[i].open = !tree[i].open;
  154. }
  155. if (tree[i].hasOwnProperty(itemListKey) && tree[i][itemListKey].length !== 0) {
  156. this.changeOpen(tree[i][itemListKey], id, itemListKey);
  157. }
  158. }
  159. return;
  160. },
  161. onchange(e) {
  162. const { treeList } = this.data;
  163. const { itemListKey } = this.data;
  164. const { idKey } = this.data;
  165. const { id } = e.currentTarget.dataset;
  166. this.changeOpen(treeList, id, itemListKey, idKey);
  167. this.triggerEvent('change', id, { bubbles: true, composed: true });
  168. this.setData({
  169. tree: treeList
  170. })
  171. },
  172. change(e) {
  173. const id = e.detail;
  174. const { treeList } = this.data;
  175. const { itemListKey } = this.data;
  176. const { idKey } = this.data;
  177. this.changeOpen(treeList, id, itemListKey, idKey);
  178. this.setData({
  179. tree: treeList
  180. })
  181. },
  182. // 点击名字
  183. clickName(e) {
  184. const { idKey } = this.data;
  185. const { nameKey } = this.data;
  186. const { itemListKey } = this.data;
  187. if (this.data.isLastClickFlag) { //开启只点击末级
  188. if (e.currentTarget.dataset.item[itemListKey]) { //还有子list 说明就不是末级
  189. return
  190. }
  191. }
  192. this.triggerEvent('clickName', { id: e.currentTarget.dataset.item[idKey], name: e.currentTarget.dataset.item[nameKey], item: e.currentTarget.dataset.item });
  193. },
  194. //点击控件里的控件 回调
  195. subclickName(e) {
  196. this.triggerEvent('clickName', { id: e.detail.id, name: e.detail.name, item: e.detail.item });
  197. },
  198. click(e) {
  199. const t = this.selectAllComponents('#treeSelect');
  200. t.forEach(item => {
  201. item.click(e);
  202. })
  203. const { idKey } = this.data;
  204. let id = '';
  205. if (e.detail) {
  206. id = e.detail[idKey];
  207. }
  208. const { treeList } = this.data;
  209. this.setStatus(treeList, id, idKey);
  210. this.setData({
  211. tree: treeList
  212. })
  213. },
  214. handleClick(e) {
  215. const t = this.selectAllComponents('#treeSelect');
  216. t.forEach(item => {
  217. item.click(e);
  218. })
  219. // const {id}=e.currentTarget.dataset;
  220. const { id, value } = e.currentTarget.dataset;
  221. const { treeList } = this.data;
  222. const { idKey } = this.data;
  223. // const value = this.getData(treeList, id)
  224. this.setStatus(treeList, id, idKey);
  225. this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
  226. this.setData({
  227. tree: treeList
  228. })
  229. },
  230. //切换选中状态
  231. setStatus(tree, id, idKey) {
  232. for (let i = 0; i < tree.length; i += 1) {
  233. if (tree[i][idKey] == id) {
  234. tree[i].selected = true;
  235. } else {
  236. tree[i].selected = false;
  237. }
  238. if (tree[i][itemListKey]) {
  239. this.setStatus(tree[i][itemListKey], id, idKey);
  240. }
  241. }
  242. return;
  243. },
  244. //获取选中项信息
  245. getData(tree, id, idKey) {
  246. for (let i = 0; i < tree.length; i += 1) {
  247. if (tree[i][idKey] === id) {
  248. return tree[i].name;
  249. }
  250. if (tree[i][itemListKey]) {
  251. this.getData(tree[i][itemListKey], id, idKey);
  252. }
  253. }
  254. return '';
  255. },
  256. },
  257. })