user-select.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view>
  3. <view class="action arrow" @tap="showModal" :disabled="disabled" data-target="bottomModal">
  4. <view class="upicker uni-input ellipsis-description">{{labels?labels:'请选择'}}</view>
  5. </view>
  6. <view class="cu-modal bottom-modal" style="min-height: 200upx;" :class="modalName=='bottomModal'?'show':''">
  7. <view class="cu-dialog">
  8. <view class="cu-bar bg-white">
  9. <view class="action text-blue" @tap="hideModal">取消</view>
  10. <view class="action text-green" @tap="selectUsers">确定</view>
  11. </view>
  12. <view>
  13. <ly-tree :tree-data="data"
  14. :props="props"
  15. node-key="id"
  16. :checkOnClickNode ="true"
  17. :showRadio="showRadio"
  18. :show-checkbox ="showCheckBox"
  19. :checkOnlyLeaf = "checkOnlyLeaf"
  20. ref="userTree" />
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import userService from "@/api/sys/userService"
  28. export default {
  29. data() {
  30. return {
  31. data: [],
  32. modalName: null,
  33. labels: null,
  34. treeList: []
  35. };
  36. },
  37. props: {
  38. limit: Number,
  39. value: String,
  40. size: String,
  41. readonly: {
  42. type: Boolean,
  43. default: () => { return false }
  44. },
  45. checkOnlyLeaf: {
  46. type: Boolean,
  47. default: () => { return false }
  48. },
  49. showRadio: {
  50. type: Boolean,
  51. default: () => { return false }
  52. },
  53. showCheckBox: {
  54. type: Boolean,
  55. default: () => { return true }
  56. },
  57. disabled: {
  58. type: Boolean,
  59. default: () => { return false }
  60. },
  61. props: {
  62. type: Object,
  63. default: () => {
  64. return {
  65. children: 'children',
  66. label: 'label'
  67. }
  68. }
  69. }
  70. },
  71. mounted() {
  72. userService.treeData().then(({data})=>{
  73. this.data = data
  74. this.setTreeList(this.data)
  75. let labelArra = []
  76. if(this.value){
  77. let keys = this.value.split(',')
  78. keys.forEach((id) => {
  79. this.treeList.forEach((node) => {
  80. if (id === node.id) {
  81. labelArra.push(node.label)
  82. }
  83. })
  84. })
  85. this.labels = labelArra.join(',')
  86. this.$refs.userTree.setCheckedKeys(keys);
  87. }
  88. })
  89. },
  90. methods:{
  91. setTreeList (datas) { // 遍历树 获取id数组
  92. for (var i in datas) {
  93. this.treeList.push(datas[i])
  94. if (datas[i].children) {
  95. this.setTreeList(datas[i].children)
  96. }
  97. }
  98. },
  99. selectUsers () {
  100. let ids = this.$refs.userTree.getCheckedNodes().filter((item)=>{
  101. return item.type === 'user'
  102. }).map((item)=>{
  103. return item.id
  104. }).join(",");
  105. let names = this.$refs.userTree.getCheckedNodes().filter((item)=>{
  106. return item.type === 'user'
  107. }).map((item)=>{
  108. return item.label
  109. }).join(",");
  110. this.labels = names
  111. this.$emit('input', ids)
  112. this.hideModal()
  113. },
  114. showModal(e) {
  115. if(this.disabled){
  116. return
  117. }
  118. this.modalName = e.currentTarget.dataset.target
  119. },
  120. hideModal(e) {
  121. this.modalName = null
  122. }
  123. }
  124. }
  125. </script>