JTreeSelect.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <a-tree-select
  3. allowClear
  4. labelInValue
  5. :getPopupContainer="(node) => node.parentNode"
  6. style="width: 100%"
  7. :disabled="disabled"
  8. :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
  9. :placeholder="placeholder"
  10. :loadData="asyncLoadTreeData"
  11. :value="treeValue"
  12. :treeData="treeData"
  13. :multiple="multiple"
  14. @change="onChange"
  15. @search="onSearch">
  16. </a-tree-select>
  17. </template>
  18. <script>
  19. /*
  20. * 异步树加载组件 通过传入表名 显示字段 存储字段 加载一个树控件
  21. * <j-tree-select dict="aa_tree_test,aad,id" pid-field="pid" ></j-tree-select>
  22. * */
  23. import { getAction } from '@/api/manage'
  24. export default {
  25. name: 'JTreeSelect',
  26. props: {
  27. value:{
  28. type: String,
  29. required: false
  30. },
  31. placeholder:{
  32. type: String,
  33. default: '请选择',
  34. required: false
  35. },
  36. dict:{
  37. type: String,
  38. default: '',
  39. required: false
  40. },
  41. pidField:{
  42. type: String,
  43. default: 'pid',
  44. required: false
  45. },
  46. pidValue:{
  47. type: String,
  48. default: '',
  49. required: false
  50. },
  51. disabled:{
  52. type:Boolean,
  53. default:false,
  54. required:false
  55. },
  56. hasChildField:{
  57. type: String,
  58. default: '',
  59. required: false
  60. },
  61. condition:{
  62. type:String,
  63. default:'',
  64. required:false
  65. },
  66. // 是否支持多选
  67. multiple: {
  68. type: Boolean,
  69. default: false,
  70. },
  71. loadTriggleChange:{
  72. type: Boolean,
  73. default: false,
  74. required:false
  75. }
  76. },
  77. data () {
  78. return {
  79. treeValue: null,
  80. treeData:[],
  81. url:"/sys/dict/loadTreeData",
  82. view:'/sys/dict/loadDictItem/',
  83. tableName:"",
  84. text:"",
  85. code:"",
  86. }
  87. },
  88. watch: {
  89. value () {
  90. this.loadItemByCode()
  91. },
  92. dict(){
  93. this.initDictInfo()
  94. this.loadRoot();
  95. }
  96. },
  97. created(){
  98. this.validateProp().then(()=>{
  99. this.initDictInfo()
  100. this.loadRoot()
  101. this.loadItemByCode()
  102. })
  103. },
  104. methods: {
  105. loadItemByCode(){
  106. if(!this.value || this.value=="0"){
  107. this.treeValue = null
  108. }else{
  109. getAction(`${this.view}${this.dict}`,{key:this.value}).then(res=>{
  110. if(res.success){
  111. let values = this.value.split(',')
  112. this.treeValue = res.result.map((item, index) => ({
  113. key: values[index],
  114. value: values[index],
  115. label: item
  116. }))
  117. this.onLoadTriggleChange(res.result[0]);
  118. }
  119. })
  120. }
  121. },
  122. onLoadTriggleChange(text){
  123. //只有单选才会触发
  124. if(!this.multiple && this.loadTriggleChange){
  125. this.$emit('change', this.value,text)
  126. }
  127. },
  128. initDictInfo(){
  129. let arr = this.dict.split(",")
  130. this.tableName = arr[0]
  131. this.text = arr[1]
  132. this.code = arr[2]
  133. },
  134. asyncLoadTreeData (treeNode) {
  135. return new Promise((resolve) => {
  136. if (treeNode.$vnode.children) {
  137. resolve()
  138. return
  139. }
  140. let pid = treeNode.$vnode.key
  141. let param = {
  142. pid:pid,
  143. tableName:this.tableName,
  144. text:this.text,
  145. code:this.code,
  146. pidField:this.pidField,
  147. hasChildField:this.hasChildField,
  148. condition:this.condition
  149. }
  150. getAction(this.url,param).then(res=>{
  151. if(res.success){
  152. for(let i of res.result){
  153. i.value = i.key
  154. if(i.leaf==false){
  155. i.isLeaf=false
  156. }else if(i.leaf==true){
  157. i.isLeaf=true
  158. }
  159. }
  160. this.addChildren(pid,res.result,this.treeData)
  161. this.treeData = [...this.treeData]
  162. }
  163. resolve()
  164. })
  165. })
  166. },
  167. addChildren(pid,children,treeArray){
  168. if(treeArray && treeArray.length>0){
  169. for(let item of treeArray){
  170. if(item.key == pid){
  171. if(!children || children.length==0){
  172. item.isLeaf=true
  173. }else{
  174. item.children = children
  175. }
  176. break
  177. }else{
  178. this.addChildren(pid,children,item.children)
  179. }
  180. }
  181. }
  182. },
  183. loadRoot(){
  184. let param = {
  185. pid:this.pidValue,
  186. tableName:this.tableName,
  187. text:this.text,
  188. code:this.code,
  189. pidField:this.pidField,
  190. hasChildField:this.hasChildField,
  191. condition:this.condition
  192. }
  193. getAction(this.url,param).then(res=>{
  194. if(res.success && res.result){
  195. for(let i of res.result){
  196. i.value = i.key
  197. if(i.leaf==false){
  198. i.isLeaf=false
  199. }else if(i.leaf==true){
  200. i.isLeaf=true
  201. }
  202. }
  203. this.treeData = [...res.result]
  204. }else{
  205. console.log("数根节点查询结果-else",res)
  206. }
  207. })
  208. },
  209. onChange(value){
  210. if(!value){
  211. this.$emit('change', '');
  212. this.treeValue = null
  213. } else if (value instanceof Array) {
  214. this.$emit('change', value.map(item => item.value).join(','))
  215. this.treeValue = value
  216. } else {
  217. this.$emit('change', value.value,value.label)
  218. this.treeValue = value
  219. }
  220. },
  221. onSearch(value){
  222. console.log(value)
  223. },
  224. getCurrTreeData(){
  225. return this.treeData
  226. },
  227. validateProp(){
  228. let mycondition = this.condition
  229. return new Promise((resolve,reject)=>{
  230. if(!mycondition){
  231. resolve();
  232. }else{
  233. try {
  234. let test=JSON.parse(mycondition);
  235. if(typeof test == 'object' && test){
  236. resolve()
  237. }else{
  238. this.$message.error("组件JTreeSelect-condition传值有误,需要一个json字符串!")
  239. reject()
  240. }
  241. } catch(e) {
  242. this.$message.error("组件JTreeSelect-condition传值有误,需要一个json字符串!")
  243. reject()
  244. }
  245. }
  246. })
  247. }
  248. },
  249. //2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 这个牛逼
  250. model: {
  251. prop: 'value',
  252. event: 'change'
  253. }
  254. }
  255. </script>