tree.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div class="list">
  3. <div class="item" v-for="item in props.list" :key="item[configurationItem.value]" v-show="!item.isHide">
  4. <div class="title">
  5. <div class="checkbox-box">
  6. <van-checkbox icon-size="16px" shape="square" @click.stop="checkChange(item)" v-model="item.checked">
  7. <span style="font-size: 15px;">
  8. {{ item[configurationItem.label]}}
  9. </span>
  10. </van-checkbox>
  11. </div>
  12. <div @click.stop="itemClick(item)" :class="item.first?'arrow':'arrowlast'">
  13. <van-icon v-if="item[configurationItem.children] && item[configurationItem.children].length" :name="item.isShowChildren ? 'arrow-up' : 'arrow-down'" />
  14. </div>
  15. </div>
  16. <div class="tree" v-show="item.first||item.isShowChildren">
  17. <tree
  18. :isLink="data.isLink"
  19. v-if="item.children && item.children.length"
  20. :list="item.children"
  21. :listObj="props.listObj"
  22. :isFirstFloor="false"
  23. :multiple="data.multiple"
  24. @confirm="onConfirm"
  25. :defaultId="defaultId">
  26. </tree>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script setup>
  32. import { reactive, watch } from 'vue'
  33. import tree from './tree.vue'
  34. const emits = defineEmits(["change","confirm"])
  35. const props = defineProps({
  36. // 是否是第一层
  37. isFirstFloor: {
  38. type: Boolean,
  39. default() {
  40. return true;
  41. },
  42. },
  43. // 树形结构
  44. list: {
  45. type: Array,
  46. default() {
  47. return [];
  48. },
  49. },
  50. multiple: {
  51. type: Boolean,
  52. default() {
  53. return false;
  54. },
  55. },
  56. // 树形扁平化数据
  57. listObj: {
  58. type: Object,
  59. default() {
  60. return {};
  61. },
  62. },
  63. // 配置项 默认为 label, value, children
  64. configurationItem: {
  65. type: Object,
  66. default() {
  67. return {
  68. label: "label",
  69. value: "value",
  70. children: "children",
  71. };
  72. },
  73. },
  74. // 单选默认值
  75. defaultId : String
  76. })
  77. const data = reactive({
  78. firstLoad: true,
  79. checkboxValue1: [],
  80. showList: [],
  81. isLink: true,
  82. multiple: true,
  83. isOutData: true, // 需要将数据抛出
  84. })
  85. watch(() => props.list, () => {
  86. if (data.firstLoad) {
  87. outDataBuffer();
  88. data.firstLoad = false;
  89. }
  90. // 判断 是第一层树 且 不是进行显示隐藏操作时,进行数据的抛出
  91. if (props.isFirstFloor && data.isOutData) {
  92. if(data.multiple){
  93. outCheckedData();
  94. }
  95. }
  96. }, { deep: true })
  97. // 展开
  98. const itemClick = (item) => {
  99. outDataBuffer();
  100. item.isShowChildren = !item.isShowChildren
  101. }
  102. // 数据抛出缓冲(在list数据变化时,不想抛出选择的数据时,调用该方法)
  103. const outDataBuffer = () => {
  104. data.isOutData = false;
  105. setTimeout(() => {
  106. data.isOutData = true;
  107. }, 500);
  108. }
  109. // 获取选中对象
  110. const getCheckData = (list) => {
  111. let deptList = [];
  112. list.forEach((itm) => {
  113. if (itm.checked) {
  114. deptList.push(itm);
  115. }
  116. if (itm.children && itm.children.length) {
  117. deptList = deptList.concat(getCheckData(itm.children));
  118. }
  119. });
  120. return deptList;
  121. }
  122. // 单项checked改变
  123. const checkChange = (item) => {
  124. // 多选
  125. if (data.multiple) {
  126. // item.checked = !item.checked
  127. if (data.isLink) {
  128. // 展开所有可以展开的节点
  129. if (item.checked) {
  130. expandAll(item);
  131. }
  132. // 勾选子级
  133. if (item.children && item.children.length) {
  134. checkChidren(item.children, item.checked);
  135. outCheckedData();
  136. }
  137. }
  138. return
  139. }
  140. // 单选
  141. if(item.children && item.children.length) return
  142. toggleAllSelectData(props.list)
  143. outCheckedData();
  144. }
  145. // 获取全部可选择数据,进行全选/取消
  146. const toggleAllSelectData = (list) => {
  147. list.forEach((itm) => {
  148. itm.checked = false
  149. if (itm.children && itm.children.length) {
  150. toggleAllSelectData(itm.children)
  151. }
  152. });
  153. }
  154. // 展开所有可以展开的节点
  155. const expandAll = (item) => {
  156. if (item.children?.length) {
  157. item.isShowChildren = true
  158. item.children.forEach(itm => {
  159. expandAll(itm);
  160. })
  161. }
  162. }
  163. // 根据父级统一取消勾选或勾选
  164. const checkChidren = (list, isChecked) => {
  165. list.forEach((itm) => {
  166. itm.checked = isChecked
  167. if (itm.children && itm.children.length) {
  168. checkChidren(itm.children, isChecked);
  169. }
  170. });
  171. }
  172. // 抛出选中的数据
  173. const outCheckedData = () => {
  174. const checkedList = getCheckData(props.list);
  175. emits("change", checkedList);
  176. onConfirm(checkedList)
  177. }
  178. const onConfirm = (e) => {
  179. emits("confirm", e);
  180. }
  181. defineExpose({
  182. itemClick,
  183. outDataBuffer,
  184. getCheckData,
  185. checkChange,
  186. expandAll,
  187. // checkParent,
  188. checkChidren,
  189. outCheckedData,
  190. })
  191. </script>
  192. <style lang="scss" scoped>
  193. .list {
  194. .item {
  195. margin-bottom: 10px;
  196. .title {
  197. display: flex;
  198. align-items: center;
  199. justify-content: space-between;
  200. margin-bottom: 10px;
  201. .checkbox-box {
  202. display: flex;
  203. align-items: center;
  204. cursor: pointer;
  205. padding: 10px 0;
  206. }
  207. .arrow{
  208. width: 80px;
  209. display: flex;
  210. justify-content: flex-end;
  211. }
  212. }
  213. .tree {
  214. margin-left: 50px;
  215. }
  216. }
  217. .arrow{
  218. display: none !important;
  219. }
  220. }
  221. </style>