dk-tree.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. const { treeList } = this.data;
  98. const { itemListKey } = this.data;
  99. const { idKey } = this.data;
  100. const { id } = e.currentTarget.dataset;
  101. this.changeOpen(treeList, id, itemListKey,idKey);
  102. this.triggerEvent('change', id, { bubbles: true, composed: true });
  103. this.setData({
  104. tree: treeList
  105. })
  106. },
  107. change(e) {
  108. const id = e.detail;
  109. const { treeList } = this.data;
  110. const { itemListKey } = this.data;
  111. const { idKey } = this.data;
  112. this.changeOpen(treeList, id,itemListKey,idKey);
  113. this.setData({
  114. tree: treeList
  115. })
  116. },
  117. // 点击名字
  118. clickName(e){
  119. const { idKey } = this.data;
  120. const { nameKey } = this.data;
  121. this.triggerEvent('clickName', {id:e.currentTarget.dataset.item[idKey],name:e.currentTarget.dataset.item[nameKey]} );
  122. },
  123. //点击控件里的控件 回调
  124. subclickName(e){
  125. this.triggerEvent('clickName', {id:e.detail.id,name:e.detail.name} );
  126. },
  127. click(e) {
  128. const t = this.selectAllComponents('#treeSelect');
  129. t.forEach(item => {
  130. item.click(e);
  131. })
  132. const { idKey } = this.data;
  133. let id = '';
  134. if (e.detail) {
  135. id = e.detail[idKey];
  136. }
  137. const { treeList } = this.data;
  138. this.setStatus(treeList, id,idKey);
  139. this.setData({
  140. tree: treeList
  141. })
  142. },
  143. handleClick(e) {
  144. const t = this.selectAllComponents('#treeSelect');
  145. t.forEach(item => {
  146. item.click(e);
  147. })
  148. // const {id}=e.currentTarget.dataset;
  149. const { id, value } = e.currentTarget.dataset;
  150. const { treeList } = this.data;
  151. const { idKey } = this.data;
  152. // const value = this.getData(treeList, id)
  153. this.setStatus(treeList, id,idKey);
  154. this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
  155. this.setData({
  156. tree: treeList
  157. })
  158. },
  159. //切换选中状态
  160. setStatus(tree, id,idKey) {
  161. for (let i = 0; i < tree.length; i += 1) {
  162. if (tree[i][idKey] == id) {
  163. tree[i].selected = true;
  164. } else {
  165. tree[i].selected = false;
  166. }
  167. if (tree[i][itemListKey]) {
  168. this.setStatus(tree[i][itemListKey], id,idKey);
  169. }
  170. }
  171. return;
  172. },
  173. //获取选中项信息
  174. getData(tree, id,idKey) {
  175. for (let i = 0; i < tree.length; i += 1) {
  176. if (tree[i][idKey] === id) {
  177. return tree[i].name;
  178. }
  179. if (tree[i][itemListKey]) {
  180. this.getData(tree[i][itemListKey], id,idKey);
  181. }
  182. }
  183. return '';
  184. },
  185. }
  186. })