dk-tree-form.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. },
  42. data: {},
  43. ready() {
  44. // const {treeList}=this.properties;
  45. const { treeList } = this.data;
  46. const { itemListKey } = this.data;
  47. const { parentIdKey } = this.data;
  48. for (let i in treeList) {
  49. if (treeList[i][itemListKey]) {
  50. if(treeList[i][parentIdKey]){
  51. treeList[i].open = false;
  52. }else{
  53. treeList[i].open = true;
  54. }
  55. tranverse(treeList[i][itemListKey],itemListKey);
  56. }
  57. treeList[i].selected = false;
  58. }
  59. this.setData({
  60. tree: treeList
  61. })
  62. },
  63. methods: {
  64. //设置控件信息
  65. setDataFromPage(data) {
  66. const { itemListKey } = this.data;
  67. const { parentIdKey } = this.data;
  68. let treeList = data
  69. for (let i in treeList) {
  70. if (treeList[i][itemListKey]) {
  71. if(treeList[i][parentIdKey]){
  72. treeList[i].open = false;
  73. }else{
  74. treeList[i].open = true;
  75. }
  76. tranverse(treeList[i][itemListKey],itemListKey);
  77. }
  78. treeList[i].selected = false;
  79. }
  80. this.setData({
  81. tree: treeList
  82. })
  83. },
  84. //修改折叠状态
  85. changeOpen(tree, id,itemListKey,idKey) {
  86. for (let i = 0; i < tree.length; i += 1) {
  87. if (tree[i][idKey] === id) {
  88. tree[i].open = !tree[i].open;
  89. }
  90. if (tree[i].hasOwnProperty(itemListKey) && tree[i][itemListKey].length !== 0) {
  91. this.changeOpen(tree[i][itemListKey], id, itemListKey);
  92. }
  93. }
  94. return;
  95. },
  96. onchange(e) {
  97. console.log("onchange",e)
  98. const { treeList } = this.data;
  99. const { itemListKey } = this.data;
  100. const { idKey } = this.data;
  101. const { id } = e.currentTarget.dataset;
  102. this.changeOpen(treeList, id, itemListKey,idKey);
  103. this.triggerEvent('change', id, { bubbles: true, composed: true });
  104. this.setData({
  105. tree: treeList
  106. })
  107. },
  108. change(e) {
  109. console.log("change",e)
  110. const id = e.detail;
  111. const { treeList } = this.data;
  112. const { itemListKey } = this.data;
  113. const { idKey } = this.data;
  114. this.changeOpen(treeList, id,itemListKey,idKey);
  115. this.setData({
  116. tree: treeList
  117. })
  118. },
  119. // 点击名字
  120. clickName(e){
  121. const { idKey } = this.data;
  122. const { nameKey } = this.data;
  123. this.triggerEvent('clickName', {id:e.currentTarget.dataset.item[idKey],name:e.currentTarget.dataset.item[nameKey]} );
  124. },
  125. //点击控件里的控件 回调
  126. subclickName(e){
  127. this.triggerEvent('clickName', {id:e.detail.id,name:e.detail.name} );
  128. },
  129. click(e) {
  130. console.log("click")
  131. const t = this.selectAllComponents('#treeSelect');
  132. t.forEach(item => {
  133. item.click(e);
  134. })
  135. const { idKey } = this.data;
  136. let id = '';
  137. if (e.detail) {
  138. id = e.detail[idKey];
  139. }
  140. const { treeList } = this.data;
  141. this.setStatus(treeList, id,idKey);
  142. this.setData({
  143. tree: treeList
  144. })
  145. },
  146. handleClick(e) {
  147. const t = this.selectAllComponents('#treeSelect');
  148. t.forEach(item => {
  149. item.click(e);
  150. })
  151. // const {id}=e.currentTarget.dataset;
  152. const { id, value } = e.currentTarget.dataset;
  153. const { treeList } = this.data;
  154. const { idKey } = this.data;
  155. // const value = this.getData(treeList, id)
  156. this.setStatus(treeList, id,idKey);
  157. this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
  158. this.setData({
  159. tree: treeList
  160. })
  161. },
  162. //切换选中状态
  163. setStatus(tree, id,idKey) {
  164. for (let i = 0; i < tree.length; i += 1) {
  165. if (tree[i][idKey] == id) {
  166. tree[i].selected = true;
  167. } else {
  168. tree[i].selected = false;
  169. }
  170. if (tree[i][itemListKey]) {
  171. this.setStatus(tree[i][itemListKey], id,idKey);
  172. }
  173. }
  174. return;
  175. },
  176. //获取选中项信息
  177. getData(tree, id,idKey) {
  178. for (let i = 0; i < tree.length; i += 1) {
  179. if (tree[i][idKey] === id) {
  180. return tree[i].name;
  181. }
  182. if (tree[i][itemListKey]) {
  183. this.getData(tree[i][itemListKey], id,idKey);
  184. }
  185. }
  186. return '';
  187. },
  188. }
  189. })