sideTree.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div class="sideTree" v-loading="load">
  3. <div class="tree-top">
  4. <el-switch
  5. :width="35"
  6. v-model="switchValue"
  7. active-text="包含下级"
  8. size="mini">
  9. </el-switch>
  10. </div>
  11. <el-tree :data="data" :props="defaultProps" node-key="id" highlight-current :expand-on-click-node="false" :default-expand-all="attrs.defaultExpandAll" @node-click="handleNodeClick"></el-tree>
  12. </div>
  13. </template>
  14. <script lang="ts">
  15. /*
  16. config:{
  17. attr:{
  18. retConfig:{}, //返回值配置
  19. label:'',
  20. children:'',
  21. defaultExpandAll: true/false // 是否默认展开所有节点
  22. resType:'' //请求返回数据所在位置
  23. },
  24. request:{
  25. url:'',
  26. method:'',
  27. data:'',
  28. params:''
  29. }
  30. }
  31. */
  32. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  33. import VueViews from '@/benyun/compVue/VueViews'
  34. @Component
  35. export default class SideTree extends VueViews {
  36. load = false;
  37. value:any={}
  38. switchValue = true;
  39. data:Array<any>=[]
  40. config:any={}
  41. get defaultProps(){
  42. let obj = {
  43. children: 'children',
  44. label: 'label'
  45. }
  46. if(this.attrs.label){
  47. obj.label = this.attrs.label
  48. }
  49. if(this.attrs.children){
  50. obj.children = this.attrs.children
  51. }
  52. return obj;
  53. }
  54. get requestConfig(){
  55. return this.config.request
  56. }
  57. created(){
  58. if(this.propConfig){
  59. this.setConfig(this.propConfig)
  60. }
  61. if(this.requestConfig){
  62. this.request()
  63. }
  64. }
  65. setData(data:Array<any>){
  66. this.data=data;
  67. }
  68. handleNodeClick(data:any){
  69. this.value={}
  70. let newData = this.getChildData([data]);
  71. if(this.attrs.retConfig){
  72. if(this.switchValue){
  73. for(const item of newData){
  74. for(const key in this.attrs.retConfig){
  75. if(!this.value[key]) this.value[key] = [];
  76. this.value[key].push(item[this.attrs.retConfig[key]])
  77. }
  78. }
  79. }else{
  80. for(const key in this.attrs.retConfig){
  81. if(!this.value[key]) this.value[key] = [];
  82. this.value[key].push(data[this.attrs.retConfig[key]])
  83. }
  84. }
  85. }
  86. this.value['isContain'] = this.switchValue;
  87. this.onChange();
  88. }
  89. onChange(){
  90. this.$emit('onChange',this.value)
  91. }
  92. //获取下级数据
  93. getChildData(data:Array<any>):Array<any>{
  94. let d:Array<any> = [];
  95. for(const item of data){
  96. d.push(item);
  97. if(item.children && item.children.length > 0){
  98. d = d.concat(this.getChildData(item.children))
  99. }
  100. }
  101. return d;
  102. }
  103. request(){
  104. this.data = []
  105. this.load = true;
  106. if(this.requestConfig){
  107. this.requestHandle({
  108. ...this.requestConfig,
  109. success:(res:any) => {
  110. let t = this.attrs.resType?this.attrs.resType:'data'
  111. if(res && res[t]){
  112. this.setData(res[t])
  113. }
  114. this.load = false
  115. },
  116. fail:() => {
  117. this.load = false;
  118. }
  119. })
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped lang="scss">
  125. .tree-top{
  126. width: 100%;
  127. display: flex;
  128. justify-content: flex-end;
  129. padding-bottom: 8px;
  130. }
  131. </style>
  132. <style lang="scss">
  133. .tree-top{
  134. .el-switch__label *{
  135. font-size: 12px !important;
  136. }
  137. }
  138. </style>