user-select-radio.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view style="width: 100%;"
  3. @tap="open"
  4. >
  5. <!-- <u&#45;&#45;input-->
  6. <!-- v-model="labels"-->
  7. <!-- suffixIcon="arrow-right"-->
  8. <!-- suffixIconStyle="color: #909399"-->
  9. <!-- disabled-->
  10. <!-- disabledColor="#ffffff"-->
  11. <!-- :placeholder="placeholder"-->
  12. <!-- border="none"-->
  13. <!-- ></u&#45;&#45;input>-->
  14. <u-action-sheet
  15. :show="show"
  16. @close="show = false"
  17. >
  18. <view class="cu-bar bg-white">
  19. <view class="action text-blue" @tap="show=false">取消</view>
  20. <view class="action text-green" @tap="selectUsers">确定</view>
  21. </view>
  22. <view>
  23. <ly-tree :tree-data="data"
  24. :props="props"
  25. node-key="id"
  26. :checkOnClickNode ="true"
  27. :showRadio="showRadio"
  28. :show-checkbox ="showCheckBox"
  29. :checkOnlyLeaf = "checkOnlyLeaf"
  30. ref="userTree" />
  31. </view>
  32. </u-action-sheet>
  33. </view>
  34. </template>
  35. <script>
  36. import userService from "@/api/sys/userService"
  37. export default {
  38. data() {
  39. return {
  40. index: '',
  41. show: false,
  42. labels:'',
  43. type:'',
  44. data: [],
  45. treeList: []
  46. };
  47. },
  48. props: {
  49. limit: Number,
  50. value: String,
  51. size: String,
  52. placeholder: {
  53. type: String,
  54. default: () => { return '请选择用户' }
  55. },
  56. readonly: {
  57. type: Boolean,
  58. default: () => { return false }
  59. },
  60. checkOnlyLeaf: {
  61. type: Boolean,
  62. default: () => { return true }
  63. },
  64. showRadio: {
  65. type: Boolean,
  66. default: () => { return true }
  67. },
  68. showCheckBox: {
  69. type: Boolean,
  70. default: () => { return false }
  71. },
  72. disabled: {
  73. type: Boolean,
  74. default: () => { return false }
  75. },
  76. props: {
  77. type: Object,
  78. default: () => {
  79. return {
  80. children: 'children',
  81. label: 'label'
  82. }
  83. }
  84. }
  85. },
  86. mounted() {
  87. userService.treeDataRadio({type: 'ydd'}).then((data)=>{
  88. // 遍历数据结构中的每个部门,为其子集添加父级的 label
  89. data[0].children.forEach(department => {
  90. this.addParentLabel(department, department.label, department.id);
  91. });
  92. this.data = data
  93. this.setTreeList(this.data)
  94. let labelArra = []
  95. if(this.value){
  96. let keys = this.value.split(',')
  97. keys.forEach((id) => {
  98. this.treeList.forEach((node) => {
  99. if (id === node.id) {
  100. labelArra.push(node.label)
  101. }
  102. })
  103. })
  104. this.labels = labelArra.join(',')
  105. }
  106. })
  107. },
  108. methods:{
  109. open (index,type) {
  110. this.index = index
  111. this.show = true;
  112. this.type = type;
  113. if(this.value){
  114. this.$nextTick(()=>{
  115. let keys = this.value.split(',')
  116. this.$refs.userTree.setCheckedKeys(keys);
  117. })
  118. }
  119. },
  120. setTreeList (datas) { // 遍历树 获取id数组
  121. for (var i in datas) {
  122. this.treeList.push(datas[i])
  123. if (datas[i].children) {
  124. this.setTreeList(datas[i].children)
  125. }
  126. }
  127. },
  128. selectUsers() {
  129. let user = this.$refs.userTree.getCheckedNodes().find(item => {
  130. return item.type === 'user';
  131. });
  132. // 如果没有选择任何用户,则显示提示信息
  133. if (!user) {
  134. uni.showToast({
  135. title: '请至少选择一条数据',
  136. icon: 'none',
  137. duration: 2000
  138. });
  139. } else {
  140. // 提取用户的 ID 和父级 label,并形成新的对象数组
  141. let usersWithParentLabel = {
  142. id: user.id,
  143. label: user.label,
  144. parentLabel: user.parentLabel, // 获取父级 label,如果不存在则设置为 null
  145. parentId: user.parentId
  146. };
  147. // 将用户信息传递给其他地方
  148. this.$emit('input', usersWithParentLabel,this.index,this.type);
  149. // 在本地更新标签显示
  150. let names = user.label;
  151. this.labels = names;
  152. this.show = false;
  153. }
  154. },
  155. // 给数据结构中的子集添加父级的 label
  156. addParentLabel(data, parentLabel,parentId) {
  157. if (data.children) {
  158. data.children.forEach(child => {
  159. // 如果子集是用户节点,则添加父级的 label
  160. if (child.type === 'user') {
  161. child.parentLabel = parentLabel;
  162. child.parentId = parentId;
  163. }
  164. // 递归调用以处理子集的子集
  165. this.addParentLabel(child, parentLabel);
  166. });
  167. }
  168. }
  169. }
  170. }
  171. </script>