user-select.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view style="width: 100%;"
  3. @tap="open"
  4. >
  5. <u--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--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. show: false,
  41. labels:'',
  42. data: [],
  43. treeList: []
  44. };
  45. },
  46. props: {
  47. limit: Number,
  48. value: String,
  49. size: String,
  50. placeholder: {
  51. type: String,
  52. default: () => { return '请选择用户' }
  53. },
  54. readonly: {
  55. type: Boolean,
  56. default: () => { return false }
  57. },
  58. checkOnlyLeaf: {
  59. type: Boolean,
  60. default: () => { return false }
  61. },
  62. showRadio: {
  63. type: Boolean,
  64. default: () => { return false }
  65. },
  66. showCheckBox: {
  67. type: Boolean,
  68. default: () => { return true }
  69. },
  70. disabled: {
  71. type: Boolean,
  72. default: () => { return false }
  73. },
  74. props: {
  75. type: Object,
  76. default: () => {
  77. return {
  78. children: 'children',
  79. label: 'label'
  80. }
  81. }
  82. }
  83. },
  84. mounted() {
  85. userService.treeData().then((data)=>{
  86. this.data = data
  87. this.setTreeList(this.data)
  88. let labelArra = []
  89. if(this.value){
  90. let keys = this.value.split(',')
  91. keys.forEach((id) => {
  92. this.treeList.forEach((node) => {
  93. if (id === node.id) {
  94. labelArra.push(node.label)
  95. }
  96. })
  97. })
  98. this.labels = labelArra.join(',')
  99. }
  100. })
  101. },
  102. methods:{
  103. open () {
  104. this.show = true;
  105. if(this.value){
  106. this.$nextTick(()=>{
  107. let keys = this.value.split(',')
  108. this.$refs.userTree.setCheckedKeys(keys);
  109. })
  110. }
  111. },
  112. setTreeList (datas) { // 遍历树 获取id数组
  113. for (var i in datas) {
  114. this.treeList.push(datas[i])
  115. if (datas[i].children) {
  116. this.setTreeList(datas[i].children)
  117. }
  118. }
  119. },
  120. selectUsers () {
  121. let ids = this.$refs.userTree.getCheckedNodes().filter((item)=>{
  122. return item.type === 'user'
  123. }).map((item)=>{
  124. return item.id
  125. }).join(",");
  126. let names = this.$refs.userTree.getCheckedNodes().filter((item)=>{
  127. return item.type === 'user'
  128. }).map((item)=>{
  129. return item.label
  130. }).join(",");
  131. this.labels = names
  132. this.$emit('input', ids)
  133. this.show = false
  134. }
  135. }
  136. }
  137. </script>