dk-tree-form.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. Component({
  12. properties: {
  13. treeList: Object,
  14. depth: {
  15. type: Number,
  16. value: 0
  17. },
  18. itemListKey: { //子list key名
  19. type: String,
  20. value: ""
  21. },
  22. parentIdKey: { //父级的key名
  23. type: String,
  24. value: ""
  25. },
  26. idKey: { // id 的key名
  27. type: String,
  28. value: ""
  29. },
  30. nameKey: { //显示的名字的key
  31. type: String,
  32. value: ""
  33. },
  34. ischekboxFlag: { //是否显示cheakbox
  35. type: Boolean,
  36. value: false
  37. },
  38. isNameImgFlag: { //是否显示名字前图片
  39. type: Boolean,
  40. value: true
  41. },
  42. isLastClickFlag: { //控制是否只可以点击末级
  43. type: Boolean,
  44. value: false
  45. },
  46. },
  47. data: {
  48. imageUrl: config.image_url + '/static/img/',
  49. },
  50. ready() {
  51. // const {treeList}=this.properties;
  52. const { treeList } = this.data;
  53. const { itemListKey } = this.data;
  54. const { parentIdKey } = this.data;
  55. for (let i in treeList) {
  56. if (treeList[i][itemListKey]) {
  57. if(treeList[i][parentIdKey]){
  58. treeList[i].open = false;
  59. }else{
  60. treeList[i].open = true;
  61. }
  62. tranverse(treeList[i][itemListKey],itemListKey);
  63. }
  64. treeList[i].selected = false;
  65. }
  66. this.setData({
  67. tree: treeList
  68. })
  69. },
  70. methods: {
  71. //设置控件信息
  72. setDataFromPage(data) {
  73. const { itemListKey } = this.data;
  74. const { parentIdKey } = this.data;
  75. let treeList = data
  76. for (let i in treeList) {
  77. if (treeList[i][itemListKey]) {
  78. if(treeList[i][parentIdKey]){
  79. treeList[i].open = false;
  80. }else{
  81. treeList[i].open = true;
  82. }
  83. tranverse(treeList[i][itemListKey],itemListKey);
  84. }
  85. treeList[i].selected = false;
  86. }
  87. this.setData({
  88. tree: treeList
  89. })
  90. },
  91. //修改折叠状态
  92. changeOpen(tree, id,itemListKey,idKey) {
  93. for (let i = 0; i < tree.length; i += 1) {
  94. if (tree[i][idKey] === id) {
  95. tree[i].open = !tree[i].open;
  96. }
  97. if (tree[i].hasOwnProperty(itemListKey) && tree[i][itemListKey].length !== 0) {
  98. this.changeOpen(tree[i][itemListKey], id, itemListKey);
  99. }
  100. }
  101. return;
  102. },
  103. onchange(e) {
  104. const { treeList } = this.data;
  105. const { itemListKey } = this.data;
  106. const { idKey } = this.data;
  107. const { id } = e.currentTarget.dataset;
  108. this.changeOpen(treeList, id, itemListKey,idKey);
  109. this.triggerEvent('change', id, { bubbles: true, composed: true });
  110. this.setData({
  111. tree: treeList
  112. })
  113. },
  114. change(e) {
  115. const id = e.detail;
  116. const { treeList } = this.data;
  117. const { itemListKey } = this.data;
  118. const { idKey } = this.data;
  119. this.changeOpen(treeList, id,itemListKey,idKey);
  120. this.setData({
  121. tree: treeList
  122. })
  123. },
  124. // 点击名字
  125. clickName(e){
  126. const { idKey } = this.data;
  127. const { nameKey } = this.data;
  128. const { itemListKey } = this.data;
  129. if(this.data.isLastClickFlag){ //开启只点击末级
  130. if(e.currentTarget.dataset.item[itemListKey]){ //还有子list 说明就不是末级
  131. return
  132. }
  133. }
  134. this.triggerEvent('clickName', {id:e.currentTarget.dataset.item[idKey],name:e.currentTarget.dataset.item[nameKey]} );
  135. },
  136. //点击控件里的控件 回调
  137. subclickName(e){
  138. this.triggerEvent('clickName', {id:e.detail.id,name:e.detail.name} );
  139. },
  140. click(e) {
  141. const t = this.selectAllComponents('#treeSelect');
  142. t.forEach(item => {
  143. item.click(e);
  144. })
  145. const { idKey } = this.data;
  146. let id = '';
  147. if (e.detail) {
  148. id = e.detail[idKey];
  149. }
  150. const { treeList } = this.data;
  151. this.setStatus(treeList, id,idKey);
  152. this.setData({
  153. tree: treeList
  154. })
  155. },
  156. handleClick(e) {
  157. const t = this.selectAllComponents('#treeSelect');
  158. t.forEach(item => {
  159. item.click(e);
  160. })
  161. // const {id}=e.currentTarget.dataset;
  162. const { id, value } = e.currentTarget.dataset;
  163. const { treeList } = this.data;
  164. const { idKey } = this.data;
  165. // const value = this.getData(treeList, id)
  166. this.setStatus(treeList, id,idKey);
  167. this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
  168. this.setData({
  169. tree: treeList
  170. })
  171. },
  172. //切换选中状态
  173. setStatus(tree, id,idKey) {
  174. for (let i = 0; i < tree.length; i += 1) {
  175. if (tree[i][idKey] == id) {
  176. tree[i].selected = true;
  177. } else {
  178. tree[i].selected = false;
  179. }
  180. if (tree[i][itemListKey]) {
  181. this.setStatus(tree[i][itemListKey], id,idKey);
  182. }
  183. }
  184. return;
  185. },
  186. //获取选中项信息
  187. getData(tree, id,idKey) {
  188. for (let i = 0; i < tree.length; i += 1) {
  189. if (tree[i][idKey] === id) {
  190. return tree[i].name;
  191. }
  192. if (tree[i][itemListKey]) {
  193. this.getData(tree[i][itemListKey], id,idKey);
  194. }
  195. }
  196. return '';
  197. },
  198. }
  199. })