dk-tree-form.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const tranverse = (e,itemListKey) => {
  2. for (let i in e) {
  3. if (e[i][itemListKey]) {
  4. e[i].open = false;
  5. tranverse(e[i][itemListKey],itemListKey)
  6. }
  7. e[i].selected = false;
  8. }
  9. }
  10. Component({
  11. properties: {
  12. treeList: Object,
  13. depth: {
  14. type: Number,
  15. value: 0
  16. },
  17. itemListKey: { //子list key名
  18. type: String,
  19. value: ""
  20. },
  21. parentIdKey: { //父级的key名
  22. type: String,
  23. value: ""
  24. },
  25. idKey: { // id 的key名
  26. type: String,
  27. value: ""
  28. },
  29. nameKey: { //显示的名字的key
  30. type: String,
  31. value: ""
  32. },
  33. ischekboxFlag: { //是否显示cheakbox
  34. type: Boolean,
  35. value: false
  36. },
  37. isNameImgFlag: { //是否显示名字前图片
  38. type: Boolean,
  39. value: true
  40. },
  41. isLastClickFlag: { //控制是否只可以点击末级
  42. type: Boolean,
  43. value: false
  44. },
  45. },
  46. data: {},
  47. ready() {
  48. // const {treeList}=this.properties;
  49. const { treeList } = this.data;
  50. const { itemListKey } = this.data;
  51. const { parentIdKey } = this.data;
  52. for (let i in treeList) {
  53. if (treeList[i][itemListKey]) {
  54. if(treeList[i][parentIdKey]){
  55. treeList[i].open = false;
  56. }else{
  57. treeList[i].open = true;
  58. }
  59. tranverse(treeList[i][itemListKey],itemListKey);
  60. }
  61. treeList[i].selected = false;
  62. }
  63. this.setData({
  64. tree: treeList
  65. })
  66. },
  67. methods: {
  68. //设置控件信息
  69. setDataFromPage(data) {
  70. const { itemListKey } = this.data;
  71. const { parentIdKey } = this.data;
  72. let treeList = data
  73. for (let i in treeList) {
  74. if (treeList[i][itemListKey]) {
  75. if(treeList[i][parentIdKey]){
  76. treeList[i].open = false;
  77. }else{
  78. treeList[i].open = true;
  79. }
  80. tranverse(treeList[i][itemListKey],itemListKey);
  81. }
  82. treeList[i].selected = false;
  83. }
  84. this.setData({
  85. tree: treeList
  86. })
  87. },
  88. //修改折叠状态
  89. changeOpen(tree, id,itemListKey,idKey) {
  90. for (let i = 0; i < tree.length; i += 1) {
  91. if (tree[i][idKey] === id) {
  92. tree[i].open = !tree[i].open;
  93. }
  94. if (tree[i].hasOwnProperty(itemListKey) && tree[i][itemListKey].length !== 0) {
  95. this.changeOpen(tree[i][itemListKey], id, itemListKey);
  96. }
  97. }
  98. return;
  99. },
  100. onchange(e) {
  101. console.log("onchange",e)
  102. const { treeList } = this.data;
  103. const { itemListKey } = this.data;
  104. const { idKey } = this.data;
  105. const { id } = e.currentTarget.dataset;
  106. this.changeOpen(treeList, id, itemListKey,idKey);
  107. this.triggerEvent('change', id, { bubbles: true, composed: true });
  108. this.setData({
  109. tree: treeList
  110. })
  111. },
  112. change(e) {
  113. console.log("change",e)
  114. const id = e.detail;
  115. const { treeList } = this.data;
  116. const { itemListKey } = this.data;
  117. const { idKey } = this.data;
  118. this.changeOpen(treeList, id,itemListKey,idKey);
  119. this.setData({
  120. tree: treeList
  121. })
  122. },
  123. // 点击名字
  124. clickName(e){
  125. const { idKey } = this.data;
  126. const { nameKey } = this.data;
  127. const { itemListKey } = this.data;
  128. if(this.data.isLastClickFlag){ //开启只点击末级
  129. if(e.currentTarget.dataset.item[itemListKey]){ //还有子list 说明就不是末级
  130. return
  131. }
  132. }
  133. this.triggerEvent('clickName', {id:e.currentTarget.dataset.item[idKey],name:e.currentTarget.dataset.item[nameKey]} );
  134. },
  135. //点击控件里的控件 回调
  136. subclickName(e){
  137. this.triggerEvent('clickName', {id:e.detail.id,name:e.detail.name} );
  138. },
  139. click(e) {
  140. console.log("click")
  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. })